Revision
815
Author
eloy.de.enige@gmail.com
Date
2009-02-27 04:44:55 -0800 (Fri, 27 Feb 2009)

Log Message

Use NSString#length for String#length which correctly returns the length of a string with multibyte characters. Patch by Vincent Isambart.

Modified Paths

Diff

Modified: MacRuby/trunk/insns.def (814 => 815)


--- MacRuby/trunk/insns.def	2009-02-25 22:24:26 UTC (rev 814)
+++ MacRuby/trunk/insns.def	2009-02-27 12:44:55 UTC (rev 815)
@@ -1980,10 +1980,7 @@
 {
     if (!SPECIAL_CONST_P(recv) &&
 	BASIC_OP_UNREDEFINED_P(BOP_LENGTH)) {
-	if (HEAP_CLASS_OF(recv) == rb_cCFString) {
-	    val = rb_str_length(recv);
-	}
-	else if (HEAP_CLASS_OF(recv) == rb_cCFArray) {
+	if (HEAP_CLASS_OF(recv) == rb_cCFArray) {
 	    val = LONG2NUM(RARRAY_LEN(recv));
 	}
 	else if (HEAP_CLASS_OF(recv) == rb_cCFHash) {

Modified: MacRuby/trunk/test-macruby/cases/string_test.rb (814 => 815)


--- MacRuby/trunk/test-macruby/cases/string_test.rb	2009-02-25 22:24:26 UTC (rev 814)
+++ MacRuby/trunk/test-macruby/cases/string_test.rb	2009-02-27 12:44:55 UTC (rev 815)
@@ -22,7 +22,7 @@
 @end
 EOS
       File.open('/tmp/_test_string.m', 'w') { |io| io.write(s) }
-      system("gcc /tmp/_test_string.m -bundle -o #{bundle} -framework Foundation -fobjc-gc-only") or exit 1
+      system("gcc /tmp/_test_string.m -bundle -o #{bundle} -arch i386 -arch x86_64 -framework Foundation -fobjc-gc-only") or exit 1
     end
     require 'dl'; DL.dlopen(bundle)
   end
@@ -43,6 +43,12 @@
     end
   end
   
+  it "should return the correct length for multibyte characters" do
+    assert_equal(3, '恂恄恆'.length)
+    assert_equal(2, 'a恂'.length)
+    assert_equal(1, 'a'.length)
+  end
+  
   #
   # Tests for UTF-16 surrogate pairs
   # These tests should be fixed in the future.