Revision
3816
Author
lsansonetti@apple.com
Date
2010-03-18 19:10:21 -0700 (Thu, 18 Mar 2010)

Log Message

added support for reverse regexps (naive but working)

Modified Paths

Diff

Modified: MacRuby/trunk/re.cpp (3815 => 3816)


--- MacRuby/trunk/re.cpp	2010-03-19 02:09:47 UTC (rev 3815)
+++ MacRuby/trunk/re.cpp	2010-03-19 02:10:21 UTC (rev 3816)
@@ -553,10 +553,6 @@
 int
 rb_reg_search(VALUE re, VALUE str, int pos, bool reverse)
 {
-    if (reverse) {
-	rb_raise(rb_eRuntimeError, "reverse searching is not implemented yet");
-    }
-
     const long len = rb_str_chars_len(str);
     if (pos > len || pos < 0) {
 	rb_backref_set(Qnil);
@@ -576,8 +572,24 @@
 		u_errorName(status));
     }
 
-    if (!matcher->find(pos, status)) {
+    if (reverse) {
+	const int orig = pos;
+	while (pos >= 0) {
+	    if (matcher->find(pos, status)) {
+		if (matcher->start(status) <= orig) {
+		    break;
+		}
+	    }
+	    pos--;
+	}
+	if (pos < 0) {
+	    // No match.
+	    goto no_match;
+	}
+    }
+    else if (!matcher->find(pos, status)) {
 	// No match.
+no_match:
 	rb_backref_set(Qnil);
 	delete matcher;
 	delete unistr;