From 86b2dea8d5791e476c8f432a45fed069a2c9e65b Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Fri, 15 Mar 2013 10:50:40 -0700 Subject: [PATCH] io.encodings: remove duplication in decoder-readln. --- basis/io/ports/ports.factor | 2 -- core/io/encodings/encodings.factor | 22 +++++++--------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/basis/io/ports/ports.factor b/basis/io/ports/ports.factor index bf8b1b20ff..cc6ce42ff1 100644 --- a/basis/io/ports/ports.factor +++ b/basis/io/ports/ports.factor @@ -232,8 +232,6 @@ M: object underlying-handle underlying-port handle>> ; HINTS: decoder-read-until { string input-port utf8 } { string input-port ascii } ; -HINTS: decoder-readln { input-port utf8 } { input-port ascii } ; - HINTS: M\ input-port stream-read-partial-unsafe { fixnum byte-array input-port } { fixnum string input-port } ; diff --git a/core/io/encodings/encodings.factor b/core/io/encodings/encodings.factor index f88a8d5b86..906ccdd9ab 100644 --- a/core/io/encodings/encodings.factor +++ b/core/io/encodings/encodings.factor @@ -111,28 +111,20 @@ M: decoder stream-contents* { CHAR: \n [ line-ends\n ] } } case ; inline -! If the stop? branch is taken convert the sbuf to a string ! If sep is present, returns ``string sep'' (string can be "") ! If sep is f, returns ``string f'' or ``f f'' -: read-until-loop ( buf quot: ( -- char stop? ) -- string/f sep/f ) - dup call - [ nip [ "" like ] dip [ f like f ] unless* ] - [ pick push read-until-loop ] if ; inline recursive - -: (read-until) ( quot -- string/f sep/f ) - [ 100 ] dip read-until-loop ; inline +: (decoder-read-until) ( quot: ( -- char stop? ) -- string/f sep/f ) + [ 100 ] dip over [ push ] curry until + [ "" like ] dip [ f like f ] unless* ; inline : decoder-read-until ( seps stream encoding -- string/f sep/f ) - [ decode-char dup [ dup rot member? ] [ 2drop f t ] if ] 3curry - (read-until) ; + [ decode-char dup ] 2curry swap [ dupd member? ] curry + [ [ drop f t ] if ] curry compose (decoder-read-until) ; inline M: decoder stream-read-until >decoder< decoder-read-until ; -: decoder-readln ( stream encoding -- string/f sep/f ) - [ decode-char dup [ dup "\r\n" member? ] [ drop f t ] if ] 2curry - (read-until) ; - -M: decoder stream-readln dup >decoder< decoder-readln handle-readln ; +M: decoder stream-readln + "\r\n" over >decoder< decoder-read-until handle-readln ; M: decoder dispose stream>> dispose ;