From 8836b2a73b9513472a0a43bf68130744755781b2 Mon Sep 17 00:00:00 2001
From: Daniel Ehrenberg <littledan@Macintosh-122.local>
Date: Tue, 10 Mar 2009 19:17:25 -0500
Subject: [PATCH] matches? works as expected for reversed regexps

---
 basis/regexp/regexp-tests.factor | 12 ++++++------
 basis/regexp/regexp.factor       | 11 +++++++----
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/basis/regexp/regexp-tests.factor b/basis/regexp/regexp-tests.factor
index fa907011fd..f7d3dae3f3 100644
--- a/basis/regexp/regexp-tests.factor
+++ b/basis/regexp/regexp-tests.factor
@@ -211,8 +211,8 @@ IN: regexp-tests
 [ f ] [ "aaaxb" "a+ab" <regexp> matches? ] unit-test
 [ t ] [ "aaacb" "a+cb" <regexp> matches? ] unit-test
 
-[ 3 ] [ "aaacb" "a*" <regexp> match-index-head ] unit-test
-[ 2 ] [ "aaacb" "aa?" <regexp> match-index-head ] unit-test
+[ "aaa" ] [ "aaacb" "a*" <regexp> match-head >string ] unit-test
+[ "aa" ] [ "aaacb" "aa?" <regexp> match-head >string ] unit-test
 
 [ t ] [ "aaa" R/ AAA/i matches? ] unit-test
 [ f ] [ "aax" R/ AAA/i matches? ] unit-test
@@ -310,8 +310,8 @@ IN: regexp-tests
 [ "a" ] [ "ba" "(?<=b)(?<=b)a" <regexp> match-head >string ] unit-test
 [ "a" ] [ "cab" "(?<=c)a(?=b)" <regexp> match-head >string ] unit-test
 
-[ 3 ] [ "foobar" "foo(?=bar)" <regexp> match-index-head ] unit-test
-[ f ] [ "foobxr" "foo(?=bar)" <regexp> match-index-head ] unit-test
+[ 3 ] [ "foobar" "foo(?=bar)" <regexp> match-head length ] unit-test
+[ f ] [ "foobxr" "foo(?=bar)" <regexp> match-head ] unit-test
 
 ! Bug in parsing word
 [ t ] [ "a" R' a' matches? ] unit-test
@@ -424,8 +424,8 @@ IN: regexp-tests
 [ 1 ] [ "a\r" R/ a$/m count-matches ] unit-test
 [ 1 ] [ "a\r\n" R/ a$/m count-matches ] unit-test
 
-[ f ] [ "foobxr" "foo\\z" <regexp> match-index-head ] unit-test
-[ 3 ] [ "foo" "foo\\z" <regexp> match-index-head ] unit-test
+[ f ] [ "foobxr" "foo\\z" <regexp> match-head ] unit-test
+[ 3 ] [ "foo" "foo\\z" <regexp> match-head length ] unit-test
 
 ! [ t ] [ "foo" "\\bfoo\\b" <regexp> matches? ] unit-test
 ! [ t ] [ "afoob" "\\Bfoo\\B" <regexp> matches? ] unit-test
diff --git a/basis/regexp/regexp.factor b/basis/regexp/regexp.factor
index aacd888ccb..94bbc2af58 100644
--- a/basis/regexp/regexp.factor
+++ b/basis/regexp/regexp.factor
@@ -44,14 +44,17 @@ M: lookbehind question>quot ! Returns ( index string -- ? )
     ! and that string is a string.
     dup dfa>> execute( index string regexp -- i/f ) ;
 
-: match-index-head ( string regexp -- index/f )
-    [ 0 ] 2dip [ check-string ] dip match-index-from ;
+GENERIC: end/start ( string regexp -- end start )
+M: regexp end/start drop length 0 ;
+M: reverse-regexp end/start drop length 1- -1 swap ;
 
 PRIVATE>
 
 : matches? ( string regexp -- ? )
-    dupd match-index-head
-    [ swap length = ] [ drop f ] if* ;
+    [ end/start ] 2keep
+    [ check-string ] dip
+    match-index-from
+    [ swap = ] [ drop f ] if* ;
 
 <PRIVATE