Fix stream-read-partial on a line-reader

release
Slava Pestov 2007-11-21 03:39:34 -05:00
parent 0c57b8e086
commit a552625ee3
3 changed files with 25 additions and 6 deletions

0
core/io/io.factor Normal file → Executable file
View File

8
core/io/streams/lines/lines-tests.factor Normal file → Executable file
View File

@ -41,6 +41,14 @@ unit-test
4 swap stream-read
] unit-test
[
"1234"
] [
"Hello world\r\n1234" <string-reader>
dup stream-readln drop
4 swap stream-read-partial
] unit-test
[
CHAR: 1
] [

23
core/io/streams/lines/lines.factor Normal file → Executable file
View File

@ -32,15 +32,26 @@ M: line-reader stream-readln ( stream -- str )
"\r\n" over delegate stream-read-until handle-readln ;
: fix-read ( stream string -- string )
"\n" ?head [ swap stream-read1 [ add ] when* ] [ nip ] if ;
over line-reader-cr [
over cr-
"\n" ?head [
swap stream-read1 [ add ] when*
] [ nip ] if
] [ nip ] if ;
M: line-reader stream-read
tuck delegate stream-read
over line-reader-cr [ over cr- fix-read ] [ nip ] if ;
tuck delegate stream-read fix-read ;
M: line-reader stream-read-partial
tuck delegate stream-read-partial fix-read ;
: fix-read1 ( stream char -- char )
dup CHAR: \n = [ drop stream-read1 ] [ nip ] if ;
over line-reader-cr [
over cr-
dup CHAR: \n = [
drop stream-read1
] [ nip ] if
] [ nip ] if ;
M: line-reader stream-read1 ( stream -- char )
dup delegate stream-read1
over line-reader-cr [ over cr- fix-read1 ] [ nip ] if ;
dup delegate stream-read1 fix-read1 ;