[macruby-changes] [4189] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Tue Jun 1 20:43:39 PDT 2010


Revision: 4189
          http://trac.macosforge.org/projects/ruby/changeset/4189
Author:   lsansonetti at apple.com
Date:     2010-06-01 20:43:36 -0700 (Tue, 01 Jun 2010)
Log Message:
-----------
ruby strings now respond to #to_data which returns an NSData object wrapping the internal storage

Modified Paths:
--------------
    MacRuby/trunk/NSString.m
    MacRuby/trunk/spec/macruby/core/string_spec.rb
    MacRuby/trunk/string.c

Modified: MacRuby/trunk/NSString.m
===================================================================
--- MacRuby/trunk/NSString.m	2010-06-02 03:07:08 UTC (rev 4188)
+++ MacRuby/trunk/NSString.m	2010-06-02 03:43:36 UTC (rev 4189)
@@ -414,4 +414,5 @@
     rb_objc_define_method(rb_cString, "ascii_only?", rstr_only, 0);
     rb_objc_define_method(rb_cString, "bytes", rstr_only, 0);
     rb_objc_define_method(rb_cString, "each_byte", rstr_only, 0);
+    rb_objc_define_method(rb_cString, "to_data", rstr_only, 0);
 }

Modified: MacRuby/trunk/spec/macruby/core/string_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/core/string_spec.rb	2010-06-02 03:07:08 UTC (rev 4188)
+++ MacRuby/trunk/spec/macruby/core/string_spec.rb	2010-06-02 03:43:36 UTC (rev 4189)
@@ -44,6 +44,44 @@
     a.foo = 42
     a.foo.should == 42
   end
+
+  it "responds to #to_data and returns an NSData object wrapping the internal storage" do
+    s = 'foo'.force_encoding(Encoding::ASCII)
+    o = s.to_data
+    o.kind_of?(NSData).should == true
+    o.length.should == 3
+    ptr = o.bytes
+    ptr.class.should == Pointer
+    ptr[0].should == 102
+    ptr[1].should == 111
+    ptr[2].should == 111
+
+    s = 'はい'.force_encoding(Encoding::UTF_8)
+    o = s.to_data
+    o.kind_of?(NSData).should == true
+    o.length.should == 6
+    ptr = o.bytes
+    ptr.class.should == Pointer
+    ptr[0].should == 227
+    ptr[1].should == 129
+    ptr[2].should == 175
+    ptr[3].should == 227
+    ptr[4].should == 129
+    ptr[5].should == 132
+
+    s = File.read('/bin/cat').force_encoding(Encoding::BINARY)
+    o = s.to_data
+    o.kind_of?(NSData).should == true
+    o.length.should == s.size
+    s_bytes = s.bytes.to_a
+    ptr = o.bytes
+    ptr.class.should == Pointer
+    i = 0; c = s_bytes.size
+    while i < c
+      ptr[i].should == s_bytes[i]
+      i += 1000
+    end
+  end
 end
 
 describe "An NSString object" do
@@ -68,4 +106,18 @@
     require 'yaml'
     NSString.stringWithString("ok").to_yaml.should == "--- ok\n"
   end
+
+  [[:bytesize, []],
+   [:getbyte, [1]],
+   [:setbyte, [0, 42]],
+   [:force_encoding, [Encoding::ASCII]],
+   [:valid_encoding?, []],
+   [:ascii_only?, []],
+   [:bytes, []],
+   [:each_byte, []],
+   [:to_data, []]].each do |msg, args|
+    it "responds to ##{msg} but raises an exception" do
+      lambda { NSString.stringWithString('test').send(msg, *args) }.should raise_error(ArgumentError)
+    end
+  end
 end

Modified: MacRuby/trunk/string.c
===================================================================
--- MacRuby/trunk/string.c	2010-06-02 03:07:08 UTC (rev 4188)
+++ MacRuby/trunk/string.c	2010-06-02 03:43:36 UTC (rev 4189)
@@ -1763,6 +1763,22 @@
 
 /*
  *  call-seq:
+ *     str.to_data => NSData
+ *
+ *  returns an NSData object wrapping the receiver's internal storage.
+ */
+
+static VALUE
+rstr_to_data(VALUE self, SEL sel)
+{
+    CFDataRef data = CFDataCreate(NULL, (const UInt8 *)RSTR(self)->data.bytes,
+	    RSTR(self)->length_in_bytes); 
+    CFMakeCollectable(data);
+    return (VALUE)data;
+}
+
+/*
+ *  call-seq:
  *     str.force_encoding(encoding)   => str
  *
  *  Changes the encoding to +encoding+ and returns self.
@@ -5726,6 +5742,7 @@
     rb_objc_define_method(rb_cRubyString, "bytesize", rstr_bytesize, 0);
     rb_objc_define_method(rb_cRubyString, "getbyte", rstr_getbyte, 1);
     rb_objc_define_method(rb_cRubyString, "setbyte", rstr_setbyte, 2);
+    rb_objc_define_method(rb_cRubyString, "to_data", rstr_to_data, 0);
     rb_objc_define_method(rb_cRubyString, "force_encoding",
 	    rstr_force_encoding, 1);
     rb_objc_define_method(rb_cRubyString, "valid_encoding?",
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100601/5f8f5b2c/attachment.html>


More information about the macruby-changes mailing list