]> gitweb.factorcode.org Git - factor.git/commitdiff
Refactoring byte-array and string streams
authorDaniel Ehrenberg <ehrenbed@carleton.edu>
Sat, 16 Feb 2008 19:58:38 +0000 (13:58 -0600)
committerDaniel Ehrenberg <ehrenbed@carleton.edu>
Sat, 16 Feb 2008 19:58:38 +0000 (13:58 -0600)
core/io/streams/byte-array/byte-array-tests.factor [new file with mode: 0644]
core/io/streams/byte-array/byte-array.factor [new file with mode: 0644]
core/io/streams/string/string-tests.factor
core/io/streams/string/string.factor

diff --git a/core/io/streams/byte-array/byte-array-tests.factor b/core/io/streams/byte-array/byte-array-tests.factor
new file mode 100644 (file)
index 0000000..77a9126
--- /dev/null
@@ -0,0 +1,9 @@
+USING: tools.test io.streams.byte-array io.encodings.binary
+io.encodings.utf8 io kernel arrays strings ;
+
+[ B{ 1 2 3 } ] [ binary [ { 1 2 3 } write ] with-byte-writer ] unit-test
+[ B{ 1 2 3 } ] [ { 1 2 3 } binary [ 3 read ] with-byte-reader ] unit-test
+
+[ B{ BIN: 11110101 BIN: 10111111 BIN: 10000000 BIN: 10111111 BIN: 11101111 BIN: 10000000 BIN: 10111111 BIN: 11011111 BIN: 10000000 CHAR: x } ]
+[ { BIN: 101111111000000111111 BIN: 1111000000111111 BIN: 11111000000 CHAR: x } utf8 [ write ] with-byte-writer ] unit-test
+[ { BIN: 101111111000000111111 } t ] [ { BIN: 11110101 BIN: 10111111 BIN: 10000000 BIN: 10111111 } utf8 <byte-reader> contents dup >array swap string? ] unit-test
diff --git a/core/io/streams/byte-array/byte-array.factor b/core/io/streams/byte-array/byte-array.factor
new file mode 100644 (file)
index 0000000..eb22465
--- /dev/null
@@ -0,0 +1,16 @@
+USING: byte-arrays byte-vectors kernel io.encodings io.streams.string
+sequences io namespaces ;
+IN: io.streams.byte-array
+
+: <byte-writer> ( encoding -- stream )
+    512 <byte-vector> swap <encoding> ;
+
+: with-byte-writer ( encoding quot -- byte-array )
+    >r <byte-writer> r> [ stdio get ] compose with-stream*
+    >byte-array ; inline
+
+: <byte-reader> ( byte-array encoding -- stream )
+    >r >byte-vector dup reverse-here r> <decoding> ;
+
+: with-byte-reader ( byte-array encoding quot -- )
+    >r <byte-reader> r> with-stream ; inline
index ad46233501abef1ef264e3de55713b9fcf92979e..4bd31fe7d8cec510a3c828ee303dac425e17d748 100644 (file)
@@ -1,4 +1,4 @@
-USING: io.streams.string io kernel arrays namespaces tools.test io.encodings.latin1 ;
+USING: io.streams.string io kernel arrays namespaces tools.test ;
 IN: temporary
 
 [ "line 1" CHAR: l ]
@@ -56,6 +56,3 @@ unit-test
     dup stream-readln
     2 rot stream-read
 ] unit-test
-
-[ B{ 1 2 3 } ] [ latin1 [ { 1 2 3 } write ] with-byte-writer ] unit-test
-[ "\u000001\u000002\u000003" ] [ { 1 2 3 } latin1 [ 3 read ] with-byte-reader ] unit-test
index 42726e77626fc8f38583aa34110ec4ffeaaeca78..a45c616b9a31b4e83b0304daf55beb8d92c412bf 100755 (executable)
@@ -3,7 +3,7 @@
 IN: io.streams.string
 USING: io kernel math namespaces sequences sbufs strings
 generic splitting io.streams.plain io.streams.lines growable
-continuations byte-vectors io.encodings byte-arrays ;
+continuations ;
 
 M: growable dispose drop ;
 
@@ -18,13 +18,6 @@ M: growable stream-flush drop ;
     <string-writer> swap [ stdio get ] compose with-stream*
     >string ; inline
 
-: <byte-writer> ( encoding -- stream )
-    512 <byte-vector> swap <encoding> ;
-
-: with-byte-writer ( encoding quot -- byte-array )
-    >r <byte-writer> r> [ stdio get ] compose with-stream*
-    >byte-array ; inline
-
 : format-column ( seq ? -- seq )
     [
         [ 0 [ length max ] reduce ] keep
@@ -46,17 +39,20 @@ M: plain-writer make-cell-stream 2drop <string-writer> ;
 
 M: growable stream-read1 dup empty? [ drop f ] [ pop ] if ;
 
-: sbuf-read-until ( sbuf n -- str )
-    tail-slice >string dup reverse-here ;
+: harden-as ( seq growble-exemplar -- newseq )
+    underlying like ;
+
+: growable-read-until ( growable n -- str )
+    dupd tail-slice swap harden-as dup reverse-here ;
 
 : find-last-sep swap [ memq? ] curry find-last drop ;
 
 M: growable stream-read-until
     [ find-last-sep ] keep over [
-        [ swap 1+ sbuf-read-until ] 2keep [ nth ] 2keep
+        [ swap 1+ growable-read-until ] 2keep [ nth ] 2keep
         set-length
     ] [
-        [ swap drop 0 sbuf-read-until f like f ] keep
+        [ swap drop 0 growable-read-until f like f ] keep
         delete-all
     ] if ;
 
@@ -65,7 +61,7 @@ M: growable stream-read
         2drop f
     ] [
         [ length swap - 0 max ] keep
-        [ swap sbuf-read-until ] 2keep
+        [ swap growable-read-until ] 2keep
         set-length
     ] if ;
 
@@ -77,9 +73,3 @@ M: growable stream-read-partial
 
 : with-string-reader ( str quot -- )
     >r <string-reader> r> with-stream ; inline
-
-: <byte-reader> ( byte-array encoding -- stream )
-    >r >byte-vector dup reverse-here r> <decoding> ;
-
-: with-byte-reader ( byte-array encoding quot -- )
-    >r <byte-reader> r> with-stream ; inline