[macruby-changes] [3684] MacRuby/branches/icu

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 3 13:40:20 PST 2010


Revision: 3684
          http://trac.macosforge.org/projects/ruby/changeset/3684
Author:   lsansonetti at apple.com
Date:     2010-03-03 13:40:18 -0800 (Wed, 03 Mar 2010)
Log Message:
-----------
added regexp quoting

Modified Paths:
--------------
    MacRuby/branches/icu/encoding.h
    MacRuby/branches/icu/re.cpp
    MacRuby/branches/icu/string.c

Modified: MacRuby/branches/icu/encoding.h
===================================================================
--- MacRuby/branches/icu/encoding.h	2010-03-03 20:37:56 UTC (rev 3683)
+++ MacRuby/branches/icu/encoding.h	2010-03-03 21:40:18 UTC (rev 3684)
@@ -301,6 +301,7 @@
 long rb_str_chars_len(VALUE str);
 UChar rb_str_get_uchar(VALUE str, long pos);
 void rb_str_append_uchar(VALUE str, UChar c);
+void rb_str_append_uchars(VALUE str, UChar *chars, long len);
 unsigned long rb_str_hash_uchars(const UChar *chars, long chars_len);
 long rb_uchar_strtol(UniChar *chars, long chars_len, long pos,
 	long *end_offset);

Modified: MacRuby/branches/icu/re.cpp
===================================================================
--- MacRuby/branches/icu/re.cpp	2010-03-03 20:37:56 UTC (rev 3683)
+++ MacRuby/branches/icu/re.cpp	2010-03-03 21:40:18 UTC (rev 3684)
@@ -1607,8 +1607,83 @@
 VALUE
 rb_reg_quote(VALUE pat)
 {
-    // TODO
-    return pat;
+    UChar *chars = NULL;
+    long chars_len = 0;
+    bool need_free = false;
+    VALUE result;
+
+    rb_str_get_uchars(pat, &chars, &chars_len, &need_free);
+
+    long pos = 0;
+    for (; pos < chars_len; pos++) {
+	switch (chars[pos]) {
+	    case '[': case ']': case '{': case '}':
+	    case '(': case ')': case '|': case '-':
+	    case '*': case '.': case '\\': case '?':
+	    case '+': case '^': case '$': case ' ':
+	    case '#': case '\t': case '\f': case '\v':
+	    case '\n': case '\r':
+		goto meta_found;
+	} 
+    }
+
+    result = rb_unicode_str_new(chars, chars_len);
+    goto bail;
+
+meta_found:
+    result = rb_unicode_str_new(NULL, (chars_len * 2) + 1);
+
+    // Copy up to metacharacter.
+    rb_str_append_uchars(result, &chars[0], pos);
+
+    for (; pos < chars_len; pos++) {
+	UChar c = chars[pos];
+	switch (c) {
+	    case '[': case ']': case '{': case '}':
+	    case '(': case ')': case '|': case '-':
+	    case '*': case '.': case '\\': case '?':
+	    case '+': case '^': case '$': case '#':
+		rb_str_append_uchar(result, '\\');
+		break;
+
+	    case ' ':
+		rb_str_append_uchar(result, '\\');
+		rb_str_append_uchar(result, ' ');
+		continue;
+
+	    case '\t':
+		rb_str_append_uchar(result, '\\');
+		rb_str_append_uchar(result, 't');
+		continue;
+
+	    case '\n':
+		rb_str_append_uchar(result, '\\');
+		rb_str_append_uchar(result, 'n');
+		continue;
+
+	    case '\r':
+		rb_str_append_uchar(result, '\\');
+		rb_str_append_uchar(result, 'r');
+		continue;
+
+	    case '\f':
+		rb_str_append_uchar(result, '\\');
+		rb_str_append_uchar(result, 'f');
+		continue;
+
+	    case '\v':
+		rb_str_append_uchar(result, '\\');
+		rb_str_append_uchar(result, 'v');
+		continue;
+	}
+	rb_str_append_uchar(result, c);
+    }
+
+bail:
+    if (need_free) {
+	free(chars);
+    }
+    return result;
 }
 
 void

Modified: MacRuby/branches/icu/string.c
===================================================================
--- MacRuby/branches/icu/string.c	2010-03-03 20:37:56 UTC (rev 3683)
+++ MacRuby/branches/icu/string.c	2010-03-03 21:40:18 UTC (rev 3684)
@@ -4826,6 +4826,21 @@
     }	
 }
 
+void
+rb_str_append_uchars(VALUE str, UChar *chars, long len)
+{
+    assert(chars != NULL && len >= 0);
+
+    if (len > 0) {
+	if (RSTR(str)) {
+	    str_concat_uchars(RSTR(str), chars, len);
+	}
+	else {
+	    CFStringAppendCharacters((CFMutableStringRef)str, chars, len);
+	}
+    }
+}
+
 long
 rb_str_chars_len(VALUE str)
 {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100303/dc138363/attachment-0001.html>


More information about the macruby-changes mailing list