From: Doug Coleman Date: Sun, 31 Jul 2022 21:35:01 +0000 (-0500) Subject: splitting: simpler split X-Git-Tag: 0.99~1259 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=afd782663dbe84054efb2205d18bec1e0b61eb04 splitting: simpler split --- diff --git a/core/splitting/splitting-tests.factor b/core/splitting/splitting-tests.factor index 757cf7ffd1..0c628d0291 100644 --- a/core/splitting/splitting-tests.factor +++ b/core/splitting/splitting-tests.factor @@ -4,11 +4,13 @@ USING: ascii kernel math sequences splitting strings tools.test ; { "hello" "world-+." } [ "hello-+world-+." "-+" split1 ] unit-test { "goodbye" f } [ "goodbye" " " split1 ] unit-test { "" "" } [ "great" "great" split1 ] unit-test +{ { 1 2 3 } f } [ { 1 2 3 } { 5 6 } split1 ] unit-test { "hello world" "." } [ "hello world ." " " split1-last ] unit-test { "hello-+world" "." } [ "hello-+world-+." "-+" split1-last ] unit-test { "goodbye" f } [ "goodbye" " " split1-last ] unit-test { "" "" } [ "great" "great" split1-last ] unit-test +{ { 1 2 3 } f } [ { 1 2 3 } { 5 6 } split1-last ] unit-test { "hello world" "." } [ "hello world ." " " split1-last-slice [ >string ] bi@ ] unit-test { "hello-+world" "." } [ "hello-+world-+." "-+" split1-last-slice [ >string ] bi@ ] unit-test diff --git a/core/splitting/splitting.factor b/core/splitting/splitting.factor index 0002fbe880..27cf79c10b 100644 --- a/core/splitting/splitting.factor +++ b/core/splitting/splitting.factor @@ -25,21 +25,20 @@ PRIVATE> : ?tail-slice ( seq end -- newseq ? ) [ tail? ] [ head-slice* ] ?chomp ; - +: ?snip-slice ( from/f to/f seq -- before after ) + over [ snip-slice ] [ 2nip f ] if ; inline : split1 ( seq subseq -- before after ) - [ snip ] (split1) ; + [ subseq-range ] keepd ?snip ; inline : split1-slice ( seq subseq -- before-slice after-slice ) - [ snip-slice ] (split1) ; + [ subseq-range ] keepd ?snip-slice ; inline : split-subseq ( seq subseq -- seqs ) [ @@ -51,18 +50,11 @@ PRIVATE> : replace ( seq old new -- new-seq ) pick [ [ split-subseq ] dip ] dip join-as ; - - : split1-when ( ... seq quot: ( ... elt -- ... ? ) -- ... before after ) - [ snip ] (split1-when) ; inline + [ find drop ] keepd swap [ dup 1 + rot snip ] [ f ] if* ; inline : split1-when-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... before-slice after-slice ) - [ snip-slice ] (split1-when) ; inline + [ find drop ] keepd swap [ dup 1 + rot snip-slice ] [ f ] if* ; inline : split1-last ( seq subseq -- before after ) [ ] bi@ split1 [ reverse ] bi@