more slices in regexps, change unit tests

db4
Doug Coleman 2008-11-22 18:07:57 -06:00
parent a5c4463e34
commit 86546552d3
2 changed files with 18 additions and 17 deletions

View File

@ -326,26 +326,26 @@ IN: regexp-tests
! "a(?#bcdefg)bcd" <regexp> "abcdefg" over first-match ! "a(?#bcdefg)bcd" <regexp> "abcdefg" over first-match
! "a(?:bcdefg)" <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 [ f ] [ "ab" "a(?!b)" <regexp> first-match ] unit-test
! "a(?<=b)" <regexp> "caba" over first-match ! "a(?<=b)" <regexp> "caba" over first-match
[ { 0 1 } ] [ "ab" "a(?=b)(?=b)" <regexp> first-match ] unit-test [ "a" ] [ "ab" "a(?=b)(?=b)" <regexp> first-match >string ] unit-test
[ { 1 2 } ] [ "ba" "a(?<=b)(?<=b)" <regexp> first-match ] unit-test [ "a" ] [ "ba" "a(?<=b)(?<=b)" <regexp> first-match >string ] unit-test
[ { 1 2 } ] [ "cab" "a(?=b)(?<=c)" <regexp> first-match ] unit-test [ "a" ] [ "cab" "a(?=b)(?<=c)" <regexp> first-match >string ] unit-test
! capture group 1: "aaaa" 2: "" ! capture group 1: "aaaa" 2: ""
! "aaaa" "(a*)(a*)" <regexp> match* ! "aaaa" "(a*)(a*)" <regexp> match*
! "aaaa" "(a*)(a+)" <regexp> match* ! "aaaa" "(a*)(a+)" <regexp> match*
[ { 0 2 } ] [ "ab" "(a|ab)(bc)?" <regexp> first-match ] unit-test [ "ab" ] [ "ab" "(a|ab)(bc)?" <regexp> first-match >string ] unit-test
[ { 0 3 } ] [ "abc" "(a|ab)(bc)?" <regexp> first-match ] unit-test [ "abc" ] [ "abc" "(a|ab)(bc)?" <regexp> first-match >string ] unit-test
[ { 0 2 } ] [ "ab" "(ab|a)(bc)?" <regexp> first-match ] unit-test [ "ab" ] [ "ab" "(ab|a)(bc)?" <regexp> first-match >string ] unit-test
[ { 0 3 } ] [ "abc" "(ab|a)(bc)?" <regexp> first-match ] 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 [ t ] [ "a:b" ".+:?" <regexp> matches? ] unit-test

View File

@ -25,11 +25,14 @@ IN: regexp
[ ] [ ]
} cleave ; } cleave ;
: match ( string regexp -- pair ) : (match) ( string regexp -- dfa-traverser )
<dfa-traverser> do-match return-match ; <dfa-traverser> do-match ; inline
: match* ( string regexp -- pair captured-groups ) : match ( string regexp -- slice/f )
<dfa-traverser> do-match [ return-match ] [ captured-groups>> ] bi ; (match) return-match ;
: match* ( string regexp -- slice/f captured-groups )
(match) [ return-match ] [ captured-groups>> ] bi ;
: matches? ( string regexp -- ? ) : matches? ( string regexp -- ? )
dupd match dupd match
@ -50,7 +53,7 @@ IN: regexp
] if ; ] if ;
: first-match ( string regexp -- pair/f ) : 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 ) : re-cut ( string regexp -- end/f start )
dupd first-match dupd first-match
@ -66,9 +69,7 @@ IN: regexp
: next-match ( string regexp -- end/f match/f ) : next-match ( string regexp -- end/f match/f )
dupd first-match dup dupd first-match dup
[ [ second tail-slice ] keep ] [ [ length 1+ tail-slice ] keep ] [ 2drop f f ] if ;
[ 2drop f f ]
if ;
: all-matches ( string regexp -- seq ) : all-matches ( string regexp -- seq )
[ dup ] swap '[ _ next-match ] [ ] produce nip ; [ dup ] swap '[ _ next-match ] [ ] produce nip ;