[macruby-changes] [1717] MacRuby/branches/experimental/string.c

source_changes at macosforge.org source_changes at macosforge.org
Wed Jun 3 20:36:29 PDT 2009


Revision: 1717
          http://trac.macosforge.org/projects/ruby/changeset/1717
Author:   lsansonetti at apple.com
Date:     2009-06-03 20:36:28 -0700 (Wed, 03 Jun 2009)
Log Message:
-----------
implemented String#bytesize and String#each_byte

Modified Paths:
--------------
    MacRuby/branches/experimental/string.c

Modified: MacRuby/branches/experimental/string.c
===================================================================
--- MacRuby/branches/experimental/string.c	2009-06-04 03:17:23 UTC (rev 1716)
+++ MacRuby/branches/experimental/string.c	2009-06-04 03:36:28 UTC (rev 1717)
@@ -390,8 +390,10 @@
 static VALUE
 rb_str_bytesize(VALUE str, SEL sel)
 {
-    // TODO
-    abort();
+    // TODO Not super accurate...
+    CFStringEncoding encoding = CFStringGetSmallestEncoding((CFStringRef)str);
+    long size = CFStringGetMaximumSizeForEncoding(RSTRING_LEN(str), encoding);
+    return LONG2NUM(size);
 }
 
 /*
@@ -3837,21 +3839,34 @@
 static VALUE
 rb_str_each_byte(VALUE str, SEL sel)
 {
-#if 0 // TODO
-    long n, i;
-    char *ptr;
-
     RETURN_ENUMERATOR(str, 0, 0);
 
-    n = RSTRING_BYTELEN(str);
-    ptr = RSTRING_BYTEPTR(str);
-    for (i=0; i<n; i++) {
-	rb_yield(INT2FIX(ptr[i] & 0xff));
+    long n = RSTRING_LEN(str);
+    if (n == 0) {
+	return str;
+    }
+
+    CFStringEncoding encoding = CFStringGetSmallestEncoding((CFStringRef)str);
+    const long buflen = CFStringGetMaximumSizeForEncoding(n, encoding);
+    UInt8 *buffer = (UInt8 *)alloca(buflen + 1);
+    long used_buflen = 0;
+
+    CFStringGetBytes((CFStringRef)str,
+	    CFRangeMake(0, n),
+	    encoding,
+	    0,
+	    false,
+	    buffer,
+	    sizeof buffer,
+	    &used_buflen);
+
+    long i;
+    for (i = 0; i < used_buflen; i++) {
+	rb_yield(INT2FIX(buffer[i]));
 	RETURN_IF_BROKEN();
     }
+
     return str;
-#endif
-    abort();
 }
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090603/3044742c/attachment.html>


More information about the macruby-changes mailing list