[macruby-changes] [3826] MacRuby/trunk/string.c

source_changes at macosforge.org source_changes at macosforge.org
Thu Mar 18 22:13:52 PDT 2010


Revision: 3826
          http://trac.macosforge.org/projects/ruby/changeset/3826
Author:   lsansonetti at apple.com
Date:     2010-03-18 22:13:51 -0700 (Thu, 18 Mar 2010)
Log Message:
-----------
added support in #<< for codepoints (only if the receiver is UTF-8 for now)

Modified Paths:
--------------
    MacRuby/trunk/string.c

Modified: MacRuby/trunk/string.c
===================================================================
--- MacRuby/trunk/string.c	2010-03-19 05:13:17 UTC (rev 3825)
+++ MacRuby/trunk/string.c	2010-03-19 05:13:51 UTC (rev 3826)
@@ -2104,9 +2104,32 @@
 	    return self;
     }
 
-    // TODO: handle codepoint
+    if (RSTR(self)->encoding == rb_encodings[ENCODING_UTF8]) {
+	const int bytelen = U8_LENGTH(codepoint);
+	if (bytelen <= 0) {
+	    goto out_of_range;
+	}
+	uint8_t *buf = (uint8_t *)malloc(bytelen);
+	int offset = 0;
+	UBool error = false;
+	U8_APPEND(buf, offset, bytelen, codepoint, error);
+	if (error) {
+	    free(buf);
+	    goto out_of_range;
+	}
+	str_concat_bytes(RSTR(self), (const char *)buf, bytelen);
+	free(buf);
+    }
+    else {
+	rb_raise(rb_eArgError,
+		"receiver encoding `%s' not supported for codepoint insertion",
+		RSTRING_PTR(rb_inspect((VALUE)RSTR(self)->encoding)));
+    }
 
     return self;
+
+out_of_range:
+    rb_raise(rb_eArgError, "codepoint %ld out of range", codepoint);
 }
 
 /*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100318/19f9c699/attachment-0001.html>


More information about the macruby-changes mailing list