From 72e15848bf63c0efd4a909e0a2223e313b5526d3 Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Fri, 22 Feb 2008 20:21:23 -0600 Subject: [PATCH] More changes for encodings --- core/bootstrap/image/image.factor | 3 +-- core/io/encodings/encodings.factor | 19 +++++++++++++++---- core/io/encodings/utf8/utf8-tests.factor | 4 ++-- core/io/io-tests.factor | 4 ++-- core/io/streams/c/c-tests.factor | 2 +- core/io/streams/string/string.factor | 10 +++++++--- extra/io/encodings/ascii/ascii.factor | 6 +++--- extra/io/encodings/latin1/latin1.factor | 2 +- extra/io/encodings/utf16/utf16-tests.factor | 4 ++-- extra/io/nonblocking/nonblocking.factor | 9 ++++----- extra/io/unix/backend/backend.factor | 2 +- extra/io/unix/launcher/launcher.factor | 2 +- extra/io/unix/sockets/sockets.factor | 2 +- extra/xmode/utilities/utilities-tests.factor | 2 +- 14 files changed, 42 insertions(+), 29 deletions(-) diff --git a/core/bootstrap/image/image.factor b/core/bootstrap/image/image.factor index 36cfad828e..241511c00d 100755 --- a/core/bootstrap/image/image.factor +++ b/core/bootstrap/image/image.factor @@ -416,8 +416,7 @@ M: curry ' "Writing image to " write architecture get boot-image-name resource-path dup write "..." print flush - ! binary - [ (write-image) ] with-stream ; + binary [ (write-image) ] with-stream ; PRIVATE> diff --git a/core/io/encodings/encodings.factor b/core/io/encodings/encodings.factor index dfd9f9a36f..8267ad5217 100755 --- a/core/io/encodings/encodings.factor +++ b/core/io/encodings/encodings.factor @@ -3,7 +3,7 @@ USING: math kernel sequences sbufs vectors namespaces growable strings io classes continuations combinators io.styles io.streams.plain io.encodings.binary splitting -io.streams.string io.streams.duplex ; +io.streams.duplex ; IN: io.encodings ! Decoding @@ -50,7 +50,7 @@ GENERIC: decode-step ( buf byte ch state encoding -- buf ch state ) ] if ; : decode-read ( length stream encoding -- string ) - >r swap start-decoding r> + >r swap >fixnum start-decoding r> decode-read-loop ; TUPLE: decoded code cr ; @@ -114,7 +114,7 @@ M: decoded stream-read-until ] [ nip ] if ; M: decoded stream-read1 - 1 swap stream-read [ first ] [ f ] if* ; + 1 swap stream-read f like [ first ] [ f ] if* ; M: decoded stream-readln ( stream -- str ) "\r\n" over stream-read-until handle-readln ; @@ -127,7 +127,10 @@ TUPLE: encode-error ; TUPLE: encoded code ; : ( stream encoding-class -- encoded-stream ) - construct-empty { set-delegate set-encoded-code } encoded construct ; + dup binary eq? [ drop ] [ + construct-empty { set-delegate set-encoded-code } + encoded construct + ] if ; GENERIC: encode-string ( string encoding -- byte-array ) M: tuple-class encode-string construct-empty encode-string ; @@ -153,3 +156,11 @@ INSTANCE: encoded plain-writer : ( duplex-stream encoding -- duplex-stream ) swap { duplex-stream-in duplex-stream-out } get-slots pick reencode >r swap redecode r> ; + +! The null encoding does nothing +! (used to wrap things as line-reader/plain-writer) +! Later this will be replaced by inheritance + +TUPLE: null-encoding ; +M: null-encoding encode-string drop ; +M: null-encoding decode-step 3drop over push f f ; diff --git a/core/io/encodings/utf8/utf8-tests.factor b/core/io/encodings/utf8/utf8-tests.factor index 44d0870385..a6a32041be 100644 --- a/core/io/encodings/utf8/utf8-tests.factor +++ b/core/io/encodings/utf8/utf8-tests.factor @@ -2,10 +2,10 @@ USING: io.encodings.utf8 tools.test sbufs kernel io io.encodings sequences strings arrays unicode ; : decode-utf8-w/stream ( array -- newarray ) - >sbuf dup reverse-here utf8 contents ; + >sbuf dup reverse-here utf8 contents ; : encode-utf8-w/stream ( array -- newarray ) - SBUF" " clone tuck utf8 stream-write >array ; + SBUF" " clone tuck utf8 stream-write >array ; [ { CHAR: replacement-character } ] [ { BIN: 11110101 BIN: 10111111 BIN: 10000000 BIN: 11111111 } decode-utf8-w/stream >array ] unit-test diff --git a/core/io/io-tests.factor b/core/io/io-tests.factor index 74b6b5034f..394deb0e5c 100644 --- a/core/io/io-tests.factor +++ b/core/io/io-tests.factor @@ -1,5 +1,5 @@ USING: arrays io io.files kernel math parser strings system -tools.test words namespaces io.encodings.ascii io.encodings.binary ; +tools.test words namespaces io.encodings.latin1 io.encodings.binary ; IN: temporary [ f ] [ @@ -8,7 +8,7 @@ IN: temporary ] unit-test : ( resource -- stream ) - resource-path ascii ; + resource-path latin1 ; [ "This is a line.\rThis is another line.\r" diff --git a/core/io/streams/c/c-tests.factor b/core/io/streams/c/c-tests.factor index 66ea460126..cc2aa9dde0 100755 --- a/core/io/streams/c/c-tests.factor +++ b/core/io/streams/c/c-tests.factor @@ -1,4 +1,4 @@ -USING: tools.test io.files io io.streams.c ; +USING: tools.test io.files io io.streams.c io.encodings.ascii ; IN: temporary [ "hello world" ] [ diff --git a/core/io/streams/string/string.factor b/core/io/streams/string/string.factor index f74f91c5bd..e1c14e6ee3 100755 --- a/core/io/streams/string/string.factor +++ b/core/io/streams/string/string.factor @@ -2,7 +2,8 @@ ! See http://factorcode.org/license.txt for BSD license. IN: io.streams.string USING: io kernel math namespaces sequences sbufs strings -generic splitting growable continuations io.streams.plain ; +generic splitting growable continuations io.streams.plain +io.encodings ; M: growable dispose drop ; @@ -23,7 +24,7 @@ M: growable stream-read1 dup empty? [ drop f ] [ pop ] if ; underlying like ; : growable-read-until ( growable n -- str ) - dupd tail-slice swap harden-as dup reverse-here ; + >fixnum dupd tail-slice swap harden-as dup reverse-here ; : find-last-sep swap [ memq? ] curry find-last drop ; @@ -49,7 +50,7 @@ M: growable stream-read-partial stream-read ; : ( str -- stream ) - >sbuf dup reverse-here ; + >sbuf dup reverse-here null-encoding ; : with-string-reader ( str quot -- ) >r r> with-stream ; inline @@ -74,3 +75,6 @@ M: plain-writer stream-write-table [ drop format-table [ print ] each ] with-stream* ; M: plain-writer make-cell-stream 2drop ; + +M: growable stream-readln ( stream -- str ) + "\r\n" over stream-read-until handle-readln ; diff --git a/extra/io/encodings/ascii/ascii.factor b/extra/io/encodings/ascii/ascii.factor index 410c07f1ca..0878a7a624 100644 --- a/extra/io/encodings/ascii/ascii.factor +++ b/extra/io/encodings/ascii/ascii.factor @@ -3,13 +3,13 @@ USING: io io.encodings strings kernel math sequences byte-arrays io.encodings ; IN: io.encodings.ascii -: encode-check>= ( string max -- byte-array ) - dupd [ >= ] curry all? [ >byte-array ] [ encode-error ] if ; +: encode-check<= ( string max -- byte-array ) + dupd [ <= ] curry all? [ >byte-array ] [ encode-error ] if ; TUPLE: ascii ; M: ascii encode-string - drop 127 encode-check>= ; + drop 127 encode-check<= ; M: ascii decode-step 3drop dup 127 >= [ encode-error ] when over push f f ; diff --git a/extra/io/encodings/latin1/latin1.factor b/extra/io/encodings/latin1/latin1.factor index 7e867b15af..b914589dc9 100755 --- a/extra/io/encodings/latin1/latin1.factor +++ b/extra/io/encodings/latin1/latin1.factor @@ -6,7 +6,7 @@ IN: io.encodings.latin1 TUPLE: latin1 ; M: latin1 encode-string - drop 255 encode-check>= ; + drop 255 encode-check<= ; M: latin1 decode-step 3drop over push f f ; diff --git a/extra/io/encodings/utf16/utf16-tests.factor b/extra/io/encodings/utf16/utf16-tests.factor index 041c486915..1677c2d38d 100755 --- a/extra/io/encodings/utf16/utf16-tests.factor +++ b/extra/io/encodings/utf16/utf16-tests.factor @@ -2,10 +2,10 @@ USING: kernel tools.test io.encodings.utf16 arrays sbufs sequences io.encodings io unicode ; : decode-w/stream ( array encoding -- newarray ) - >r >sbuf dup reverse-here r> contents >array ; + >r >sbuf dup reverse-here r> contents >array ; : encode-w/stream ( array encoding -- newarray ) - >r SBUF" " clone tuck r> stream-write >array ; + >r SBUF" " clone tuck r> stream-write >array ; [ { CHAR: x } ] [ { 0 CHAR: x } utf16be decode-w/stream ] unit-test [ { HEX: 1D11E } ] [ { HEX: D8 HEX: 34 HEX: DD HEX: 1E } utf16be decode-w/stream ] unit-test diff --git a/extra/io/nonblocking/nonblocking.factor b/extra/io/nonblocking/nonblocking.factor index a8828afff6..21bb284805 100755 --- a/extra/io/nonblocking/nonblocking.factor +++ b/extra/io/nonblocking/nonblocking.factor @@ -39,15 +39,14 @@ GENERIC: close-handle ( handle -- ) : ( handle type -- port ) default-buffer-size get swap ; -: ( handle -- stream ) +: ( handle -- input-port ) input-port ; -: ( handle -- stream ) +: ( handle -- output-port ) output-port ; -: handle>duplex-stream ( in-handle out-handle encoding -- stream ) - [ swap swap ] keep - [ -rot >r swap r> ] +: handle>duplex-stream ( in-handle out-handle -- stream ) + [ swap swap ] [ ] [ dispose ] cleanup ; : pending-error ( port -- ) diff --git a/extra/io/unix/backend/backend.factor b/extra/io/unix/backend/backend.factor index d3873c60fe..e0921f6f02 100755 --- a/extra/io/unix/backend/backend.factor +++ b/extra/io/unix/backend/backend.factor @@ -182,7 +182,7 @@ M: unix-io io-multiplex ( ms -- ) mx get-global wait-for-events ; M: unix-io init-stdio ( -- ) - 0 1 utf8 handle>duplex-stream io:stdio set-global + 0 1 handle>duplex-stream io:stdio utf8 set-global 2 utf8 io:stderr set-global ; ! mx io-task for embedding an fd-based mx inside another mx diff --git a/extra/io/unix/launcher/launcher.factor b/extra/io/unix/launcher/launcher.factor index 128adbc3dc..deff2d2642 100755 --- a/extra/io/unix/launcher/launcher.factor +++ b/extra/io/unix/launcher/launcher.factor @@ -99,7 +99,7 @@ M: unix-io kill-process* ( pid -- ) M: unix-io process-stream* [ - spawn-process-stream >r latin1 handle>duplex-stream r> + spawn-process-stream >r handle>duplex-stream r> ] with-descriptor ; : find-process ( handle -- process ) diff --git a/extra/io/unix/sockets/sockets.factor b/extra/io/unix/sockets/sockets.factor index b78abbe11b..60a49aadd2 100755 --- a/extra/io/unix/sockets/sockets.factor +++ b/extra/io/unix/sockets/sockets.factor @@ -48,7 +48,7 @@ M: unix-io (client) ( addrspec -- stream ) dup r> r> connect zero? err_no EINPROGRESS = or [ dup init-client-socket - dup f handle>duplex-stream + dup handle>duplex-stream dup duplex-stream-out dup wait-to-connect pending-init-error diff --git a/extra/xmode/utilities/utilities-tests.factor b/extra/xmode/utilities/utilities-tests.factor index 713700bf7a..820d1bcc9a 100755 --- a/extra/xmode/utilities/utilities-tests.factor +++ b/extra/xmode/utilities/utilities-tests.factor @@ -49,5 +49,5 @@ TAGS> } ] [ "extra/xmode/utilities/test.xml" - resource-path read-xml parse-company-tag + resource-path file>xml parse-company-tag ] unit-test