]> gitweb.factorcode.org Git - factor.git/commitdiff
more slices in regexps, change unit tests
authorDoug Coleman <doug.coleman@gmail.com>
Sun, 23 Nov 2008 00:07:57 +0000 (18:07 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 23 Nov 2008 00:07:57 +0000 (18:07 -0600)
basis/regexp/regexp-tests.factor
basis/regexp/regexp.factor

index 4878b67d0f089100e0846149551fc8dcfaf89770..d01f0c55483644739994d614441c6f4b20143e23 100644 (file)
@@ -326,26 +326,26 @@ IN: regexp-tests
 ! "a(?#bcdefg)bcd" <regexp> "abcdefg" over first-match
 ! "a(?:bcdefg)" <regexp> "abcdefg" over first-match
 
-[ { 0 1 } ] [ "ac" "a(?!b)" <regexp> first-match ] unit-test
+[ "a" ] [ "ac" "a(?!b)" <regexp> first-match >string ] unit-test
 [ f ] [ "ab" "a(?!b)" <regexp> first-match ] unit-test
 
 ! "a(?<=b)" <regexp> "caba" over first-match
 
-[ { 0 1 } ] [ "ab" "a(?=b)(?=b)" <regexp> first-match ] unit-test
-[ { 1 2 } ] [ "ba" "a(?<=b)(?<=b)" <regexp> first-match ] unit-test
-[ { 1 2 } ] [ "cab" "a(?=b)(?<=c)" <regexp> first-match ] unit-test
+[ "a" ] [ "ab" "a(?=b)(?=b)" <regexp> first-match >string ] unit-test
+[ "a" ] [ "ba" "a(?<=b)(?<=b)" <regexp> first-match >string ] unit-test
+[ "a" ] [ "cab" "a(?=b)(?<=c)" <regexp> first-match >string ] unit-test
 
 ! capture group 1: "aaaa"  2: ""
 ! "aaaa" "(a*)(a*)" <regexp> match*
 ! "aaaa" "(a*)(a+)" <regexp> match*
 
-[ { 0 2 } ] [ "ab" "(a|ab)(bc)?" <regexp> first-match ] unit-test
-[ { 0 3 } ] [ "abc" "(a|ab)(bc)?" <regexp> first-match ] unit-test
+[ "ab" ] [ "ab" "(a|ab)(bc)?" <regexp> first-match >string ] unit-test
+[ "abc" ] [ "abc" "(a|ab)(bc)?" <regexp> first-match >string ] unit-test
 
-[ { 0 2 } ] [ "ab" "(ab|a)(bc)?" <regexp> first-match ] unit-test
-[ { 0 3 } ] [ "abc" "(ab|a)(bc)?" <regexp> first-match ] unit-test
+[ "ab" ] [ "ab" "(ab|a)(bc)?" <regexp> first-match >string ] unit-test
+[ "abc" ] [ "abc" "(ab|a)(bc)?" <regexp> first-match >string ] unit-test
 
-[ { 23 24 } ] [ "aaaaaaaaaaaaaaaaaaaaaaab" "((a*)*b)*b" <regexp> first-match ] unit-test
+[ "b" ] [ "aaaaaaaaaaaaaaaaaaaaaaab" "((a*)*b)*b" <regexp> first-match >string ] unit-test
 
 [ t ] [ "a:b" ".+:?" <regexp> matches? ] unit-test
 
index 6e13ba8db29e0d941a55b44d1410f81ae508ce1b..32c3695f32ed803fb19753f9c423e1c6bfb3fe50 100644 (file)
@@ -25,11 +25,14 @@ IN: regexp
         [ ]
     } cleave ;
 
-: match ( string regexp -- pair )
-    <dfa-traverser> do-match return-match ;
+: (match) ( string regexp -- dfa-traverser )
+    <dfa-traverser> do-match ; inline
 
-: match* ( string regexp -- pair captured-groups )
-    <dfa-traverser> do-match [ return-match ] [ captured-groups>> ] bi ;
+: match ( string regexp -- slice/f )
+    (match) return-match ;
+
+: match* ( string regexp -- slice/f captured-groups )
+    (match) [ return-match ] [ captured-groups>> ] bi ;
 
 : matches? ( string regexp -- ? )
     dupd match
@@ -50,7 +53,7 @@ IN: regexp
     ] if ;
 
 : first-match ( string regexp -- pair/f )
-    0 swap match-range dup [ 2array ] [ 2drop f ] if ;
+    dupd 0 swap match-range rot over [ <slice> ] [ 3drop f ] if ;
 
 : re-cut ( string regexp -- end/f start )
     dupd first-match
@@ -66,9 +69,7 @@ IN: regexp
 
 : next-match ( string regexp -- end/f match/f )
     dupd first-match dup
-    [ [ second tail-slice ] keep ]
-    [ 2drop f f ]
-    if ;
+    [ [ length 1+ tail-slice ] keep ] [ 2drop f f ] if ;
 
 : all-matches ( string regexp -- seq )
     [ dup ] swap '[ _ next-match ] [ ] produce nip ;