diff --git a/extra/modern/modern.factor b/extra/modern/modern.factor index df06a86306..e042b0f2a9 100644 --- a/extra/modern/modern.factor +++ b/extra/modern/modern.factor @@ -7,7 +7,7 @@ sequences.generalizations sets shuffle splitting strings syntax.modern unicode vocabs.loader ; IN: modern -ERROR: string-expected-got-eof string n ; +ERROR: unexpected-eof string n ; ERROR: long-opening-mismatch tag open string n ch ; ERROR: lex-expected-but-got-eof string n expected ; ERROR: expected-length-tokens string n length seq ; @@ -123,14 +123,14 @@ MACRO:: read-matched ( ch -- quot: ( string n tag -- string n' slice' ) ) { char: \\ [ drop next-char-from drop read-string-payload ] } } case ] [ - string-expected-got-eof + unexpected-eof ] if ; :: read-string ( string n tag -- string n' seq ) string n read-string-payload nip :> n' string n' - n' [ string n string-expected-got-eof ] unless + n' [ string n unexpected-eof ] unless n n' 1 - string n' 1 - n' string tag -rot 3array ; @@ -157,8 +157,11 @@ MACRO:: read-matched ( ch -- quot: ( string n tag -- string n' slice' ) ) : read-lowercase-colon ( string n slice -- string n' lowercase-colon ) dup [ char: \: = ] count-tail '[ - _ [ slice-til-not-whitespace drop [ lex-factor ] dip swap 2array ] replicate - ensure-no-false dup [ token-expected ] unless + _ [ + slice-til-not-whitespace drop ! XXX: whitespace here + [ dup [ unexpected-eof ] unless ] dip + [ lex-factor ] dip swap 2array + ] replicate dup terminator? [ unexpected-terminator ] when ] dip swap 2array ; diff --git a/extra/modern/slices/slices-tests.factor b/extra/modern/slices/slices-tests.factor new file mode 100644 index 0000000000..164151d871 --- /dev/null +++ b/extra/modern/slices/slices-tests.factor @@ -0,0 +1,46 @@ +! Copyright (C) 2019 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: ascii modern.slices sequences tools.test ; +IN: modern.slices.tests + +{ "foo:" f f f } [ + "foo:" f slice-til-not-whitespace +] unit-test + +{ "foo:" f f f } [ + "foo:" f slice-til-whitespace +] unit-test + +{ "foo:" 0 T{ slice f 0 0 "foo:" } 102 } [ + "foo:" 0 slice-til-not-whitespace +] unit-test + +{ "foo:" 3 T{ slice f 3 3 "foo:" } 58 } [ + "foo:" 3 slice-til-not-whitespace +] unit-test + +{ "foo:" f T{ slice f 0 4 "foo:" } f } [ + "foo:" 0 slice-til-whitespace +] unit-test + +{ "foo:" f T{ slice f 3 4 "foo:" } f } [ + "foo:" 3 slice-til-whitespace +] unit-test + +{ + "foo " + f + T{ slice f 0 4 "foo " } + f +} [ + "foo " 0 [ blank? ] slice-until-include +] unit-test + +{ + "foo " + 3 + T{ slice f 0 3 "foo " } + 32 +} [ + "foo " 0 [ blank? ] slice-until-exclude +] unit-test \ No newline at end of file diff --git a/extra/modern/slices/slices.factor b/extra/modern/slices/slices.factor index c87916ba78..b966b88e7b 100644 --- a/extra/modern/slices/slices.factor +++ b/extra/modern/slices/slices.factor @@ -52,17 +52,25 @@ ERROR: unexpected-end string n ; swapd find-from* ; inline : slice-until-exclude ( string n quot -- string n' slice/f ch/f ) - [ drop ] - [ find-from' ] 3bi ! ( string n n' ch ) - [ drop nip ] - [ [ rot ? ] dip ] 4bi ; inline + over [ + [ drop ] + [ find-from' ] 3bi ! ( string n n' ch ) + [ drop nip ] + [ [ rot ? ] dip ] 4bi + ] [ + drop f f + ] if ; inline : slice-until-include ( string n tokens -- string n' slice/f ch/f ) - '[ _ member? ] - [ drop ] - [ find-from' [ ?1+ ] dip ] 3bi ! ( string n n' ch ) - [ drop nip ] - [ [ rot ? ] dip ] 4bi ; inline + over [ + '[ _ member? ] + [ drop ] + [ find-from' [ ?1+ ] dip ] 3bi ! ( string n n' ch ) + [ drop nip ] + [ [ rot ? ] dip ] 4bi + ] [ + drop f f + ] if ; inline : slice-til-whitespace ( string n -- string n' slice/f ch/f ) [ "\s\r\n" member? ] slice-until-exclude ; inline