From bcd75e97d3a2b8a74295394b6a991c0958da61a2 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 22 Nov 2008 20:01:25 -0600 Subject: [PATCH] fix match-all, re-split --- basis/regexp/regexp-tests.factor | 14 +++++++++++++- basis/regexp/regexp.factor | 14 ++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/basis/regexp/regexp-tests.factor b/basis/regexp/regexp-tests.factor index d01f0c5548..c0252b2ff4 100644 --- a/basis/regexp/regexp-tests.factor +++ b/basis/regexp/regexp-tests.factor @@ -1,5 +1,5 @@ USING: regexp tools.test kernel sequences regexp.parser -regexp.traversal eval ; +regexp.traversal eval strings ; IN: regexp-tests \ must-infer @@ -350,3 +350,15 @@ IN: regexp-tests [ t ] [ "a:b" ".+:?" matches? ] unit-test [ 1 ] [ "hello" ".+?" match length ] unit-test + +[ { "1" "2" "3" "4" } ] +[ "1ABC2DEF3GHI4" R/ [A-Z]+/ re-split [ >string ] map ] unit-test + +[ { "1" "2" "3" "4" } ] +[ "1ABC2DEF3GHI4JK" R/ [A-Z]+/ re-split [ >string ] map ] unit-test + +[ { "ABC" "DEF" "GHI" } ] +[ "1ABC2DEF3GHI4" R/ [A-Z]+/ all-matches [ >string ] map ] unit-test + +[ "1.2.3.4" ] +[ "1ABC2DEF3GHI4JK" R/ [A-Z]+/ "." re-replace ] unit-test diff --git a/basis/regexp/regexp.factor b/basis/regexp/regexp.factor index 32c3695f32..66bc39415b 100644 --- a/basis/regexp/regexp.factor +++ b/basis/regexp/regexp.factor @@ -3,7 +3,7 @@ USING: accessors combinators kernel math sequences sets assocs prettyprint.backend make lexer namespaces parser arrays fry regexp.backend regexp.utils regexp.parser regexp.nfa -regexp.dfa regexp.traversal regexp.transition-tables ; +regexp.dfa regexp.traversal regexp.transition-tables splitting ; IN: regexp : default-regexp ( string -- regexp ) @@ -52,27 +52,25 @@ IN: regexp [ 3drop drop f f ] [ drop [ 1+ ] dip match-range ] if ] if ; -: first-match ( string regexp -- pair/f ) +: first-match ( string regexp -- slice/f ) dupd 0 swap match-range rot over [ ] [ 3drop f ] if ; : re-cut ( string regexp -- end/f start ) dupd first-match - [ [ second tail-slice ] [ first head ] 2bi ] - [ "" like f swap ] - if* ; + [ split1-slice swap ] [ "" like f swap ] if* ; : re-split ( string regexp -- seq ) - [ dup ] swap '[ _ re-cut ] [ ] produce nip ; + [ dup length 0 > ] swap '[ _ re-cut ] [ ] produce nip ; : re-replace ( string regexp replacement -- result ) [ re-split ] dip join ; : next-match ( string regexp -- end/f match/f ) dupd first-match dup - [ [ length 1+ tail-slice ] keep ] [ 2drop f f ] if ; + [ [ split1-slice nip ] keep ] [ 2drop f f ] if ; : all-matches ( string regexp -- seq ) - [ dup ] swap '[ _ next-match ] [ ] produce nip ; + [ dup ] swap '[ _ next-match ] [ ] produce nip harvest ; : count-matches ( string regexp -- n ) all-matches length 1- ;