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

source_changes at macosforge.org source_changes at macosforge.org
Sat Dec 18 00:08:37 PST 2010


Revision: 5052
          http://trac.macosforge.org/projects/ruby/changeset/5052
Author:   vincent.isambart at gmail.com
Date:     2010-12-18 00:08:32 -0800 (Sat, 18 Dec 2010)
Log Message:
-----------
simpler and better rstr_reverse_bang

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

Modified: MacRuby/trunk/string.c
===================================================================
--- MacRuby/trunk/string.c	2010-12-18 04:38:44 UTC (rev 5051)
+++ MacRuby/trunk/string.c	2010-12-18 08:08:32 UTC (rev 5052)
@@ -4956,47 +4956,22 @@
 {
     rstr_modify(str);
 
-    if (str_try_making_data_uchars(RSTR(str))) {
-	const long len = BYTES_TO_UCHARS(RSTR(str)->length_in_bytes);
-	if (len <= 1) {
-	    return str;
-	}
-	bool has_lead = false;
-	for (long i = 0; i < (len / 2); i++) {
-	    UChar c = RSTR(str)->data.uchars[i];
-	    if (U16_IS_LEAD(c)) {
-		has_lead = true;
-	    }
-	    RSTR(str)->data.uchars[i] = RSTR(str)->data.uchars[len - i - 1];
-	    RSTR(str)->data.uchars[len - i - 1] = c;
-	}
-	if (has_lead) {
-	    // if the string contained surrogates,
-	    // we have to put them back in the correct order
-	    for (long i = 0; i < len - 1; ++i) {
-		UChar c = RSTR(str)->data.uchars[i];
-		if (U16_IS_TRAIL(c)) {
-		    UChar next = RSTR(str)->data.uchars[i+1];
-		    if (U16_IS_LEAD(next)) {
-			RSTR(str)->data.uchars[i] = next;
-			RSTR(str)->data.uchars[i+1] = c;
-		    }
-		}
-	    }
-	}
+    if (RSTR(str)->length_in_bytes <= 1) {
+	return str;
     }
-    else {
-	const long len = RSTR(str)->length_in_bytes;
-	if (len <= 1) {
-	    return str;
-	}
-	for (long i = 0; i < (len / 2); i++) {
-	    char c = RSTR(str)->data.bytes[i];
-	    RSTR(str)->data.bytes[i] = RSTR(str)->data.bytes[len - i - 1];
-	    RSTR(str)->data.bytes[len - i - 1] = c; 
-	}
-    }
 
+    str_make_data_binary(RSTR(str));
+    char *new_bytes = xmalloc(RSTR(str)->length_in_bytes);
+    __block long pos = RSTR(str)->length_in_bytes;
+    str_each_char(RSTR(str), ^(UChar32 c, const char* char_start, long char_len, bool *stop) {
+	pos -= char_len;
+	memcpy(&new_bytes[pos], char_start, char_len);
+    });
+    assert(pos == 0);
+
+    RSTR(str)->capacity_in_bytes = RSTR(str)->length_in_bytes;
+    GC_WB(&RSTR(str)->data.bytes, new_bytes);
+
     // we modify it directly so the information stored
     // in the facultative flags might be outdated
     str_unset_facultative_flags(RSTR(str));
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101218/edeae178/attachment.html>


More information about the macruby-changes mailing list