]> gitweb.factorcode.org Git - factor.git/commitdiff
More updates for encodings
authorDaniel Ehrenberg <ehrenbed@carleton.edu>
Mon, 25 Feb 2008 01:58:34 +0000 (19:58 -0600)
committerDaniel Ehrenberg <ehrenbed@carleton.edu>
Mon, 25 Feb 2008 01:58:34 +0000 (19:58 -0600)
14 files changed:
core/io/backend/backend.factor
core/io/encodings/binary/binary-docs.factor
core/io/encodings/encodings.factor
core/io/encodings/utf8/utf8-docs.factor
core/io/encodings/utf8/utf8-tests.factor
core/io/files/files.factor
core/io/io-tests.factor
core/io/streams/byte-array/byte-array.factor
core/io/streams/string/string.factor
extra/io/encodings/utf16/utf16-tests.factor
extra/io/launcher/launcher.factor
extra/io/nonblocking/nonblocking.factor
extra/io/sockets/sockets-docs.factor
extra/io/sockets/sockets.factor

index 2824b6a299592ca1070cea0c20797ace87767bfa..6f41814ce9593de9891a4155e3f5f1f4aeb7e198 100755 (executable)
@@ -10,8 +10,8 @@ HOOK: init-io io-backend ( -- )
 HOOK: (init-stdio) io-backend ( -- stdin stdout stderr )
 
 : init-stdio ( -- )
-    (init-stdio) utf8 <encoded> stderr set-global
-    utf8 <encoded-duplex> stdio set-global ;
+    (init-stdio) utf8 <encoder> stderr set-global
+    utf8 <encoder-duplex> stdio set-global ;
 
 HOOK: io-multiplex io-backend ( ms -- )
 
@@ -23,8 +23,8 @@ HOOK: normalize-pathname io-backend ( str -- newstr )
 
 M: object normalize-pathname ;
 
-: set-io-backend ( backend -- )
-    io-backend set-global init-io init-stdio ;
+: set-io-backend ( io-backend -- )
+    io-backend set-global init-io init-stdio die ;
 
 [ init-io embedded? [ init-stdio ] unless ]
 "io.backend" add-init-hook
index f8be5054df5e431847c1593f7f8aaf2368631492..823eea67be19c18883bedcbb92a821435a999673 100644 (file)
@@ -2,4 +2,4 @@ USING: help.syntax help.markup ;
 IN: io.encodings.binary
 
 HELP: binary
-{ $class-description "This is the encoding descriptor for binary I/O." } ;
+{ $class-description "This is the encoding descriptor for binary I/O. Making an encoded stream with the binary encoding is a no-op; streams with this encoding deal with byte-arrays, not strings." } ;
index 670a2356159b55b7f74b333d1bf5f79060765134..79079929bb1047cd2e5f4037bbd782b03c9eb70a 100755 (executable)
@@ -54,7 +54,7 @@ GENERIC: decode-step ( buf byte ch state encoding -- buf ch state )
     decode-read-loop ;
 
 TUPLE: decoded code cr ;
-: <decoded> ( stream decoding-class -- decoded-stream )
+: <decoder> ( stream decoding-class -- decoded-stream )
     dup binary eq? [ drop ] [
         construct-empty { set-delegate set-decoded-code }
         decoded construct
@@ -126,7 +126,7 @@ TUPLE: encode-error ;
 : encode-error ( -- * ) \ encode-error construct-empty throw ;
 
 TUPLE: encoded code ;
-: <encoded> ( stream encoding-class -- encoded-stream )
+: <encoder> ( stream encoding-class -- encoded-stream )
     dup binary eq? [ drop ] [
         construct-empty { set-delegate set-encoded-code }
         encoded construct
@@ -148,12 +148,12 @@ INSTANCE: encoded plain-writer
 ! Rebinding duplex streams which have not read anything yet
 
 : reencode ( stream encoding -- newstream )
-    over encoded? [ >r delegate r> ] when <encoded> ;
+    over encoded? [ >r delegate r> ] when <encoder> ;
 
 : redecode ( stream encoding -- newstream )
-    over decoded? [ >r delegate r> ] when <decoded> ;
+    over decoded? [ >r delegate r> ] when <decoder> ;
 
-: <encoded-duplex> ( stream-in stream-out encoding -- duplex-stream )
+: <encoder-duplex> ( stream-in stream-out encoding -- duplex-stream )
     tuck reencode >r redecode r> <duplex-stream> ;
 
 ! The null encoding does nothing
index 6e1923824fd0a42f048b31dd786bfb730ee9d83f..734a7f723673507e94aa7724301e6f137e488b65 100755 (executable)
@@ -1,18 +1,11 @@
-USING: help.markup help.syntax io.encodings strings ;
+USING: help.markup help.syntax io.encodings strings io.files ;
 IN: io.encodings.utf8
 
 ARTICLE: "io.encodings.utf8" "Working with UTF8-encoded data"
-"The UTF8 encoding is a variable-width encoding. 7-bit ASCII characters are encoded as single bytes, and other Unicode code points are encoded as 2 to 4 byte sequences."
-{ $subsection encode-utf8 }
-{ $subsection decode-utf8 } ;
+"The UTF8 encoding is a variable-width encoding. 7-bit ASCII characters are encoded as single bytes, and other Unicode code points are encoded as 2 to 4 byte sequences. The encoding descriptor for UTF-8:"
+{ $subsection utf8 } ;
 
-ABOUT: "io.encodings.utf8"
-
-HELP: decode-utf8
-{ $values { "seq" "a sequence of bytes" } { "str" string } }
-{ $description "Decodes a sequence of bytes representing a Unicode string in UTF8 format." }
-{ $errors "Throws a " { $link decode-error } " if the input is malformed." } ;
+HELP: utf8
+{ $class-description "This is the class of encoding tuples which denote a UTF-8 encoding. You can pass this class symbol as an encoding descriptor to words like " { $link <file-reader> } " and " { $link encode-string } "." } ;
 
-HELP: encode-utf8
-{ $values { "str" string } { "seq" "a sequence of bytes" } }
-{ $description "Encodes a Unicode string as a sequence of bytes in UTF8 format." } ;
+ABOUT: "io.encodings.utf8"
index a6a32041bebde6a16afc15fdb2820a6af456cad6..0671fe2129a790e959c0e47d2900e6723da0293f 100644 (file)
@@ -1,11 +1,11 @@
 USING: io.encodings.utf8 tools.test sbufs kernel io io.encodings
-sequences strings arrays unicode ;
+sequences strings arrays unicode io.streams.byte-array ;
 
 : decode-utf8-w/stream ( array -- newarray )
-    >sbuf dup reverse-here utf8 <decoded> contents ;
+    utf8 <byte-reader> contents >array ;
 
 : encode-utf8-w/stream ( array -- newarray )
-    SBUF" " clone tuck utf8 <encoded> stream-write >array ;
+    utf8 [ write ] with-byte-writer >array ;
 
 [ { CHAR: replacement-character } ] [ { BIN: 11110101 BIN: 10111111 BIN: 10000000 BIN: 11111111 } decode-utf8-w/stream >array ] unit-test
 
index f41e04d72d82033723069b14d7b95bed08f8aba6..bff9d69129d1e6195528a7637017b2bbef5a6e98 100755 (executable)
@@ -17,13 +17,13 @@ HOOK: (file-writer) io-backend ( path -- stream )
 HOOK: (file-appender) io-backend ( path -- stream )
 
 : <file-reader> ( path encoding -- stream )
-    swap (file-reader) swap <decoded> ;
+    swap (file-reader) swap <decoder> ;
 
 : <file-writer> ( path encoding -- stream )
-    swap (file-writer) swap <encoded> ;
+    swap (file-writer) swap <encoder> ;
 
 : <file-appender> ( path encoding -- stream )
-    swap (file-appender) swap <encoded> ;
+    swap (file-appender) swap <encoder> ;
 
 HOOK: delete-file io-backend ( path -- )
 
@@ -171,4 +171,4 @@ M: pathname <=> [ pathname-string ] compare ;
       [ dup make-directory ]
     when ;
 
-: temp-file ( name -- path ) temp-directory swap path+ ;
\ No newline at end of file
+: temp-file ( name -- path ) temp-directory swap path+ ;
index 394deb0e5cd097c024856e34384e349765e00d30..74b6b5034f58df549a5543f95e5713f0cf18f6cb 100644 (file)
@@ -1,5 +1,5 @@
 USING: arrays io io.files kernel math parser strings system
-tools.test words namespaces io.encodings.latin1 io.encodings.binary ;
+tools.test words namespaces io.encodings.ascii io.encodings.binary ;
 IN: temporary
 
 [ f ] [
@@ -8,7 +8,7 @@ IN: temporary
 ] unit-test
 
 : <resource-reader> ( resource -- stream )
-    resource-path latin1 <file-reader> ;
+    resource-path ascii <file-reader> ;
 
 [
     "This is a line.\rThis is another line.\r"
index afbc94bf6a05c9815440d1f21a123cb0362e9974..d5ca8eac6867127b849807a5189b34de06f84194 100644 (file)
@@ -3,14 +3,14 @@ sequences io namespaces ;
 IN: io.streams.byte-array
 
 : <byte-writer> ( encoding -- stream )
-    512 <byte-vector> swap <encoded> ;
+    512 <byte-vector> swap <encoder> ;
 
 : 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> <decoded> ;
+    >r >byte-vector dup reverse-here r> <decoder> ;
 
 : with-byte-reader ( byte-array encoding quot -- )
     >r <byte-reader> r> with-stream ; inline
index e1c14e6ee32e96f897948366f803c57e1993d460..15fc2b704ec9cbf1533c8181a6462d22184a1912 100755 (executable)
@@ -50,7 +50,7 @@ M: growable stream-read-partial
     stream-read ;
 
 : <string-reader> ( str -- stream )
-    >sbuf dup reverse-here null-encoding <decoded> ;
+    >sbuf dup reverse-here null-encoding <decoder> ;
 
 : with-string-reader ( str quot -- )
     >r <string-reader> r> with-stream ; inline
index 1677c2d38d94557b8966f8c3c556385715023ab1..7ee5c9574e41a5d42beea7c28129c9cae6bf7bfd 100755 (executable)
@@ -1,11 +1,11 @@
-USING: kernel tools.test io.encodings.utf16 arrays sbufs sequences io.encodings
-io unicode ;
+USING: kernel tools.test io.encodings.utf16 arrays sbufs
+sequences io.encodings io unicode io.streams.byte-array ;
 
 : decode-w/stream ( array encoding -- newarray )
-    >r >sbuf dup reverse-here r> <decoded> contents >array ;
+    <byte-reader> contents >array ;
 
 : encode-w/stream ( array encoding -- newarray )
-    >r SBUF" " clone tuck r> <encoded> stream-write >array ;
+    [ write ] with-byte-writer >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
index 9236ec2ce99176d843ed5512d07af87d4e9d7882..b1990df6544b705a14c161d2a3f7d8bf4920a001 100755 (executable)
@@ -127,7 +127,7 @@ TUPLE: process-stream process ;
 
 : <process-stream> ( desc encoding -- stream )
     swap >descriptor
-    [ (process-stream) >r rot <encoded-duplex> r> ] keep
+    [ (process-stream) >r rot <encoder-duplex> r> ] keep
     +timeout+ swap at [ over set-timeout ] when*
     { set-delegate set-process-stream-process }
     process-stream construct ;
index b8007192fe827632d32cb9bde65d8b166ed805b2..b0ce1fcc12a9a9d62ffff9afd650c78237c56fca 100755 (executable)
@@ -45,7 +45,7 @@ GENERIC: close-handle ( handle -- )
     output-port <buffered-port> ;
 
 : <reader&writer> ( read-handle write-handle -- input-port output-port )
-    swap <reader> [ swap <writer> ] [ dispose ] cleanup ;
+    swap <reader> [ swap <writer> ] [ ] [ dispose drop ] cleanup ;
 
 : pending-error ( port -- )
     dup port-error f rot set-port-error [ throw ] when* ;
index 510d47ff2bfdb4dad3cd85b5bb8c4b8617e4bbc9..fa38ec90eee1a057811e0c72057506d95505ce82 100755 (executable)
@@ -100,12 +100,12 @@ HELP: <client>
 } ;
 
 HELP: <server>
-{ $values  { "addrspec" "an address specifier" } { "server" "a handle" } }
+{ $values  { "addrspec" "an address specifier" } { "encoding" "an encoding descriptor" } { "server" "a handle" } }
 { $description
     "Begins listening for network connections to a local address. Server objects responds to two words:"
     { $list
         { { $link dispose } " - stops listening on the port and frees all associated resources" }
-        { { $link accept } " - blocks until there is a connection" }
+        { { $link accept } " - blocks until there is a connection, and returns a stream of the encoding passed to the constructor" }
     }
 }
 { $notes
@@ -119,7 +119,7 @@ HELP: <server>
 
 HELP: accept
 { $values { "server" "a handle" } { "client" "a bidirectional stream" } }
-{ $description "Waits for a connection to a server socket created by " { $link <server> } ", and outputs a bidirectional stream when the connection has been established."
+{ $description "Waits for a connection to a server socket created by " { $link <server> } ", and outputs a bidirectional stream when the connection has been established. The encoding of this stream is the one that was passed to the server constructor."
 $nl
 "The returned client stream responds to the " { $link client-stream-addr } " word with the address of the incoming connection." }
 { $errors "Throws an error if the server socket is closed or otherwise is unavailable." } ;
@@ -139,6 +139,7 @@ HELP: <datagram>
     "To accept UDP/IP packets from the loopback interface only, use an address specifier returned by the following code, where 1234 is the desired port number:"
     { $code "\"localhost\" 1234 t resolve-host" }
     "Since " { $link resolve-host } " can return multiple address specifiers, your code must create a datagram socket for each one and co-ordinate packet sending accordingly."
+    "Datagrams are low-level binary ports that don't map onto streams, so the constructor does not use an encoding"
 }
 { $errors "Throws an error if the port is already in use, or if the OS forbids access." } ;
 
index 07e4f0afca737062950b09e8ba2a4d93e5421fb4..8de43bfd20f08c8048e7f604d114b6f801f3b83b 100755 (executable)
@@ -33,7 +33,7 @@ M: array client* [ (client) ] attempt-all ;
 M: object client* (client) ;
 
 : <client> ( addrspec encoding -- stream )
-    [ >r client* r> <encoded-duplex> ] keep <client-stream> ;
+    [ >r client* r> <encoder-duplex> ] keep <client-stream> ;
 
 HOOK: (server) io-backend ( addrspec -- handle )
 
@@ -43,7 +43,7 @@ HOOK: (server) io-backend ( addrspec -- handle )
 HOOK: (accept) io-backend ( server -- stream-in stream-out )
 
 : accept ( server -- client )
-    [ (accept) ] keep server-port-encoding <encoded-duplex> ;
+    [ (accept) ] keep server-port-encoding <encoder-duplex> ;
 
 HOOK: <datagram> io-backend ( addrspec -- datagram )