io.buffers: refactor buffer-read-until to use buffer-find.

char-rename
John Benediktsson 2016-11-09 10:43:47 -08:00
parent 4bebe98fa1
commit 1abe794205
2 changed files with 15 additions and 6 deletions

View File

@ -61,6 +61,10 @@ io.buffers kernel libc namespaces sequences strings tools.test ;
{ "hello" CHAR: \s } [ " " "b" get buffer-read-until [ >string ] dip ] unit-test
"b" get dispose
"hello world" string>buffer "b" set
{ "hello worl" CHAR: d } [ "d" "b" get buffer-read-until [ >string ] dip ] unit-test
"b" get dispose
"hello world" string>buffer "b" set
{ "hello world" f } [ "\n" "b" get buffer-read-until [ >string ] dip ] unit-test
"b" get dispose

View File

@ -76,16 +76,22 @@ TYPED: buffer-write1 ( byte: fixnum buffer: buffer -- )
[ [ ptr>> ] [ fill>> ] bi set-alien-unsigned-1 ]
[ 1 swap buffer+ ] bi ; inline
TYPED: buffer-find ( seps buffer: buffer -- n/f )
[
swap [ [ pos>> ] [ fill>> ] [ ptr>> ] tri ] dip
[ swap alien-unsigned-1 ] [ member-eq? ] bi-curry*
compose (find-integer)
] [
[ pos>> - ] curry [ f ] if*
] bi ; inline
<PRIVATE
: search-buffer-until ( pos fill ptr seps -- n )
[ iota ] 2dip
[ [ swap alien-unsigned-1 ] dip member-eq? ] 2curry
find-from drop ; inline
: search-buffer-until ( seps buffer -- buffer n/f )
[ buffer-find ] keep swap ; inline
: finish-buffer-until ( buffer n -- byte-array sep/f )
[
over pos>> -
over buffer-read
swap buffer-pop
] [
@ -96,6 +102,5 @@ TYPED: buffer-write1 ( byte: fixnum buffer: buffer -- )
PRIVATE>
TYPED: buffer-read-until ( seps buffer: buffer -- byte-array sep/f )
swap [ { [ ] [ pos>> ] [ fill>> ] [ ptr>> ] } cleave ] dip
search-buffer-until
finish-buffer-until ;