From 3712ae68e5c5d2f712fb234ac6fa800a2479f723 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 23 Aug 2004 01:56:06 +0000 Subject: [PATCH] fix read line, and add read line regression tests --- contrib/irc.factor | 31 ++++++--------------- library/image.factor | 1 - library/platform/native/parse-stream.factor | 7 +++-- library/test/httpd/html.factor | 5 ---- library/test/io/io.factor | 25 +++++++++++++++++ library/test/io/mac-os-eol.txt | 1 + library/test/io/no-trailing-eol.factor | 2 ++ library/test/io/windows-eol.txt | 2 ++ library/test/test.factor | 1 + native/port.c | 2 -- native/read.c | 11 ++++---- 11 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 library/test/io/io.factor create mode 100644 library/test/io/mac-os-eol.txt create mode 100644 library/test/io/no-trailing-eol.factor create mode 100644 library/test/io/windows-eol.txt diff --git a/contrib/irc.factor b/contrib/irc.factor index 9cf36c4307..255784b150 100644 --- a/contrib/irc.factor +++ b/contrib/irc.factor @@ -1,4 +1,4 @@ -!:folding=indent:collapseFolds=1: +! :folding=indent:collapseFolds=1: ! $Id$ ! @@ -38,7 +38,6 @@ USE: math USE: namespaces USE: parser USE: prettyprint -USE: regexp USE: stack USE: stdio USE: streams @@ -71,7 +70,7 @@ USE: unparser : irc-stream-write ( string -- ) dup "buf" get sbuf-append ends-with-newline? [ - "buf" get >str + "buf" get sbuf>str 0 "buf" get set-sbuf-length "\n" split [ dup f-or-"" [ drop ] [ "recepient" get irc-message ] ifte ] each ] when ; @@ -112,25 +111,15 @@ USE: unparser : irc-action-handler ( recepient message -- ) " " split1 swap irc-action-quot call ; -: irc-handle-privmsg ( [ recepient message ] -- ) - uncons car irc-action-handler ; - -: irc-handle-join ( [ joined channel ] -- ) - uncons car - [ - dup "nick" get = [ - "Hi " swap cat2 print - ] unless - ] with-irc-stream ; - : irc-input ( line -- ) #! Handle a line of IRC input. dup - ":.+?!.+? PRIVMSG (.+)?:(.+)" groups [ - irc-handle-privmsg - ] when* - dup ":(.+)!.+ JOIN :(.+)" groups [ - irc-handle-join + " PRIVMSG " split1 nip [ + ":" split1 dup [ + irc-action-handler + ] [ + drop + ] ifte ] when* global [ print ] bind ; @@ -148,7 +137,7 @@ USE: unparser : irc ( channels -- ) irc-register - ! "identify foobar" "NickServ" irc-message + "identify foobar" "NickServ" irc-message [ irc-join ] each irc-loop ; @@ -161,5 +150,3 @@ USE: unparser "irc.freenode.net" 6667 [ [ "#factor" ] irc ] with-stream ; - -!! "factor/irc.factor" run-file diff --git a/library/image.factor b/library/image.factor index 201609aed4..a96ee66ab3 100644 --- a/library/image.factor +++ b/library/image.factor @@ -186,7 +186,6 @@ USE: words nip ] [ drop - "Forward reference: " write dup . ! Remember where we are, and add the reference later dup fixup-word-later ] ifte ; diff --git a/library/platform/native/parse-stream.factor b/library/platform/native/parse-stream.factor index cd2a1a08d5..1ae15f03ca 100644 --- a/library/platform/native/parse-stream.factor +++ b/library/platform/native/parse-stream.factor @@ -99,6 +99,9 @@ USE: strings : resource-path ( -- path ) "resource-path" get [ "." ] unless* ; +: ( path -- stream ) + resource-path swap cat2 ; + : parse-resource ( path -- quot ) #! Resources are loaded from the resource-path variable, or #! the current directory if it is not set. Words defined in @@ -106,9 +109,7 @@ USE: strings #! resource:. This allows words that operate on source #! files, like "jedit", to use a different resource path #! at run time than was used at parse time. - "resource:" over cat2 - swap resource-path swap cat2 - parse-stream ; + "resource:" over cat2 swap parse-stream ; : run-resource ( file -- ) parse-resource call ; diff --git a/library/test/httpd/html.factor b/library/test/httpd/html.factor index e1d795a9f4..c538a4397a 100644 --- a/library/test/httpd/html.factor +++ b/library/test/httpd/html.factor @@ -10,11 +10,6 @@ USE: test "<html>&'sgml'" ] [ "&'sgml'" chars>entities ] unit-test -[ "Hello world" ] -[ - "Hello world" f html-attr-string -] unit-test - [ "car" ] [ "car" diff --git a/library/test/io/io.factor b/library/test/io/io.factor new file mode 100644 index 0000000000..43a4f7edcc --- /dev/null +++ b/library/test/io/io.factor @@ -0,0 +1,25 @@ +IN: scratchpad +USE: namespaces +USE: parser +USE: stack +USE: streams +USE: test + +[ 4 ] [ "/library/test/io/no-trailing-eol.factor" run-resource ] unit-test + +: lines-test ( stream -- line1 line2 ) + dup freadln swap dup freadln swap fclose ; + +[ + "This is a line." + "This is another line." +] [ + "/library/test/io/windows-eol.txt" lines-test +] unit-test + +[ + "This is a line." + "This is another line." +] [ + "/library/test/io/mac-os-eol.txt" lines-test +] unit-test diff --git a/library/test/io/mac-os-eol.txt b/library/test/io/mac-os-eol.txt new file mode 100644 index 0000000000..53c8eaf415 --- /dev/null +++ b/library/test/io/mac-os-eol.txt @@ -0,0 +1 @@ +This is a line. This is another line. \ No newline at end of file diff --git a/library/test/io/no-trailing-eol.factor b/library/test/io/no-trailing-eol.factor new file mode 100644 index 0000000000..4da8a500b9 --- /dev/null +++ b/library/test/io/no-trailing-eol.factor @@ -0,0 +1,2 @@ +USE: arithmetic +2 2 + \ No newline at end of file diff --git a/library/test/io/windows-eol.txt b/library/test/io/windows-eol.txt new file mode 100644 index 0000000000..c78d0078ea --- /dev/null +++ b/library/test/io/windows-eol.txt @@ -0,0 +1,2 @@ +This is a line. +This is another line. diff --git a/library/test/test.factor b/library/test/test.factor index 16fb66b7cc..2d3110c60d 100644 --- a/library/test/test.factor +++ b/library/test/test.factor @@ -77,6 +77,7 @@ USE: unparser "parse-number" "prettyprint" "inspector" + "io/io" "vectors" "words" "unparser" diff --git a/native/port.c b/native/port.c index 242976adef..25e5d876ec 100644 --- a/native/port.c +++ b/native/port.c @@ -42,8 +42,6 @@ void init_line_buffer(PORT* port, FIXNUM count) { if(port->line == F) port->line = tag_object(sbuf(LINE_SIZE)); - else - untag_sbuf(port->line)->top = 0; } void primitive_portp(void) diff --git a/native/read.c b/native/read.c index fce806544b..547148cca9 100644 --- a/native/read.c +++ b/native/read.c @@ -59,6 +59,10 @@ bool read_line_step(PORT* port) if(ch == '\n') i++; } + + port->buf_pos = i + 1; + port->line_ready = true; + return true; } if(ch == '\n') @@ -110,8 +114,6 @@ void primitive_add_read_line_io_task(void) bool perform_read_line_io_task(PORT* port) { - SBUF* line; - if(port->buf_pos >= port->buf_fill) { if(!read_step(port)) @@ -123,17 +125,14 @@ bool perform_read_line_io_task(PORT* port) /* EOF */ if(port->line != F) { - line = untag_sbuf(port->line); - if(line->top == 0) + if(untag_sbuf(port->line)->top == 0) port->line = F; } port->line_ready = true; return true; } else - { return read_line_step(port); - } } void primitive_read_line_8(void)