]> gitweb.factorcode.org Git - factor.git/commitdiff
Moving inessential encodings to extra
authorDaniel Ehrenberg <ehrenbed@carleton.edu>
Thu, 21 Feb 2008 21:28:58 +0000 (15:28 -0600)
committerDaniel Ehrenberg <ehrenbed@carleton.edu>
Thu, 21 Feb 2008 21:28:58 +0000 (15:28 -0600)
31 files changed:
core/io/encodings/ascii/ascii.factor [deleted file]
core/io/encodings/ascii/authors.txt [deleted file]
core/io/encodings/latin1/authors.txt [deleted file]
core/io/encodings/latin1/latin1-docs.factor [deleted file]
core/io/encodings/latin1/latin1.factor [deleted file]
core/io/encodings/latin1/summary.txt [deleted file]
core/io/encodings/latin1/tags.txt [deleted file]
core/io/encodings/utf16/.utf16.factor.swo [deleted file]
core/io/encodings/utf16/authors.txt [deleted file]
core/io/encodings/utf16/summary.txt [deleted file]
core/io/encodings/utf16/tags.txt [deleted file]
core/io/encodings/utf16/utf16-docs.factor [deleted file]
core/io/encodings/utf16/utf16-tests.factor [deleted file]
core/io/encodings/utf16/utf16.factor [deleted file]
extra/db/sqlite/test.db [deleted file]
extra/io/encodings/ascii/ascii.factor [new file with mode: 0644]
extra/io/encodings/ascii/authors.txt [new file with mode: 0644]
extra/io/encodings/ascii/summary.txt [new file with mode: 0644]
extra/io/encodings/ascii/tags.txt [new file with mode: 0644]
extra/io/encodings/latin1/authors.txt [new file with mode: 0644]
extra/io/encodings/latin1/latin1-docs.factor [new file with mode: 0644]
extra/io/encodings/latin1/latin1.factor [new file with mode: 0755]
extra/io/encodings/latin1/summary.txt [new file with mode: 0644]
extra/io/encodings/latin1/tags.txt [new file with mode: 0644]
extra/io/encodings/utf16/.utf16.factor.swo [new file with mode: 0644]
extra/io/encodings/utf16/authors.txt [new file with mode: 0644]
extra/io/encodings/utf16/summary.txt [new file with mode: 0644]
extra/io/encodings/utf16/tags.txt [new file with mode: 0644]
extra/io/encodings/utf16/utf16-docs.factor [new file with mode: 0644]
extra/io/encodings/utf16/utf16-tests.factor [new file with mode: 0755]
extra/io/encodings/utf16/utf16.factor [new file with mode: 0755]

diff --git a/core/io/encodings/ascii/ascii.factor b/core/io/encodings/ascii/ascii.factor
deleted file mode 100644 (file)
index 410c07f..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-! Copyright (C) 2008 Daniel Ehrenberg.
-! See http://factorcode.org/license.txt for BSD license.
-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 ;
-
-TUPLE: ascii ;
-
-M: ascii encode-string
-    drop 127 encode-check>= ;
-
-M: ascii decode-step
-    3drop dup 127 >= [ encode-error ] when over push f f ;
diff --git a/core/io/encodings/ascii/authors.txt b/core/io/encodings/ascii/authors.txt
deleted file mode 100644 (file)
index f990dd0..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Daniel Ehrenberg
diff --git a/core/io/encodings/latin1/authors.txt b/core/io/encodings/latin1/authors.txt
deleted file mode 100644 (file)
index f990dd0..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Daniel Ehrenberg
diff --git a/core/io/encodings/latin1/latin1-docs.factor b/core/io/encodings/latin1/latin1-docs.factor
deleted file mode 100644 (file)
index 5872b2b..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-USING: help.syntax help.markup ;
-IN: io.encodings.latin1
-
-HELP: latin1
-{ $class-description "This class is used for Latin 1 (ISO 8859-1) encoding and decoding" } ;
diff --git a/core/io/encodings/latin1/latin1.factor b/core/io/encodings/latin1/latin1.factor
deleted file mode 100755 (executable)
index 7e867b1..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-! Copyright (C) 2008 Daniel Ehrenberg.
-! See http://factorcode.org/license.txt for BSD license.
-USING: io io.encodings strings kernel io.encodings.ascii sequences ;
-IN: io.encodings.latin1
-
-TUPLE: latin1 ;
-
-M: latin1 encode-string 
-    drop 255 encode-check>= ;
-
-M: latin1 decode-step
-    3drop over push f f ;
diff --git a/core/io/encodings/latin1/summary.txt b/core/io/encodings/latin1/summary.txt
deleted file mode 100644 (file)
index d40d628..0000000
+++ /dev/null
@@ -1 +0,0 @@
-ISO 8859-1 encoding/decoding
diff --git a/core/io/encodings/latin1/tags.txt b/core/io/encodings/latin1/tags.txt
deleted file mode 100644 (file)
index 8e27be7..0000000
+++ /dev/null
@@ -1 +0,0 @@
-text
diff --git a/core/io/encodings/utf16/.utf16.factor.swo b/core/io/encodings/utf16/.utf16.factor.swo
deleted file mode 100644 (file)
index 01be8fd..0000000
Binary files a/core/io/encodings/utf16/.utf16.factor.swo and /dev/null differ
diff --git a/core/io/encodings/utf16/authors.txt b/core/io/encodings/utf16/authors.txt
deleted file mode 100644 (file)
index f990dd0..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Daniel Ehrenberg
diff --git a/core/io/encodings/utf16/summary.txt b/core/io/encodings/utf16/summary.txt
deleted file mode 100644 (file)
index b249067..0000000
+++ /dev/null
@@ -1 +0,0 @@
-UTF16 encoding/decoding
diff --git a/core/io/encodings/utf16/tags.txt b/core/io/encodings/utf16/tags.txt
deleted file mode 100644 (file)
index 8e27be7..0000000
+++ /dev/null
@@ -1 +0,0 @@
-text
diff --git a/core/io/encodings/utf16/utf16-docs.factor b/core/io/encodings/utf16/utf16-docs.factor
deleted file mode 100644 (file)
index c49c030..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-USING: help.markup help.syntax io.encodings strings ;
-IN: io.encodings.utf16
-
-ARTICLE: "io.utf16" "Working with UTF16-encoded data"
-"The UTF16 encoding is a variable-width encoding. Unicode code points are encoded as 2 or 4 byte sequences."
-{ $subsection encode-utf16le }
-{ $subsection encode-utf16be }
-{ $subsection decode-utf16le }
-{ $subsection decode-utf16be }
-"Support for UTF16 data with a byte order mark:"
-{ $subsection encode-utf16 }
-{ $subsection decode-utf16 } ;
-
-ABOUT: "io.utf16"
-
-HELP: decode-utf16
-{ $values { "seq" "a sequence of bytes" } { "str" string } }
-{ $description "Decodes a sequence of bytes representing a Unicode string in UTF16 format. The bytes must begin with a UTF16 byte order mark, which determines if the input is in little or big endian. To decode data without a byte order mark, use " { $link decode-utf16le } " or " { $link decode-utf16be } "." }
-{ $errors "Throws a " { $link decode-error } " if the input is malformed." } ;
-
-HELP: decode-utf16be
-{ $values { "seq" "a sequence of bytes" } { "str" string } }
-{ $description "Decodes a sequence of bytes representing a Unicode string in big endian UTF16 format. The bytes must not begin with a UTF16 byte order mark. To decode data with a byte order mark, use " { $link decode-utf16 } "." }
-{ $errors "Throws a " { $link decode-error } " if the input is malformed." } ;
-
-HELP: decode-utf16le
-{ $values { "seq" "a sequence of bytes" } { "str" string } }
-{ $description "Decodes a sequence of bytes representing a Unicode string in little endian UTF16 format. The bytes must not begin with a UTF16 byte order mark. To decode data with a byte order mark, use " { $link decode-utf16 } "." }
-{ $errors "Throws a " { $link decode-error } " if the input is malformed." } ;
-
-{ decode-utf16 decode-utf16le decode-utf16be } related-words
-
-HELP: encode-utf16be
-{ $values { "str" string } { "seq" "a sequence of bytes" } }
-{ $description "Encodes a Unicode string as a sequence of bytes in big endian UTF16 format." } ;
-
-HELP: encode-utf16le
-{ $values { "str" string } { "seq" "a sequence of bytes" } }
-{ $description "Encodes a Unicode string as a sequence of bytes in little endian UTF16 format." } ;
-
-HELP: encode-utf16
-{ $values { "str" string } { "seq" "a sequence of bytes" } }
-{ $description "Encodes a Unicode string as a sequence of bytes in UTF16 format with a byte order mark." } ;
-
-{ encode-utf16 encode-utf16be encode-utf16le } related-words
diff --git a/core/io/encodings/utf16/utf16-tests.factor b/core/io/encodings/utf16/utf16-tests.factor
deleted file mode 100755 (executable)
index 041c486..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-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> <decoding> contents >array ;
-
-: encode-w/stream ( array encoding -- newarray )
-    >r SBUF" " clone tuck r> <encoding> 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
-[ { CHAR: replacement-character } ] [ { BIN: 11011111 CHAR: q } utf16be decode-w/stream ] unit-test
-[ { CHAR: replacement-character } ] [ { BIN: 11011011 CHAR: x BIN: 11011011 CHAR: x } utf16be decode-w/stream ] unit-test
-
-[ { 0 120 216 52 221 30 } ] [ { CHAR: x HEX: 1d11e } utf16be encode-w/stream ] unit-test
-
-[ { CHAR: x } ] [ { CHAR: x 0 } utf16le decode-w/stream ] unit-test
-[ { 119070 } ] [ { HEX: 34 HEX: D8 HEX: 1E HEX: DD } utf16le decode-w/stream ] unit-test
-[ { CHAR: replacement-character } ] [ { 0 BIN: 11011111 } utf16le decode-w/stream ] unit-test
-[ { CHAR: replacement-character } ] [ { 0 BIN: 11011011 0 0 } utf16le decode-w/stream ] unit-test
-[ { 119070 } ] [ { HEX: 34 HEX: D8 HEX: 1E HEX: DD } utf16le decode-w/stream ] unit-test
-
-[ { 120 0 52 216 30 221 } ] [ { CHAR: x HEX: 1d11e } utf16le encode-w/stream ] unit-test
-
-[ { CHAR: x } ] [ { HEX: ff HEX: fe CHAR: x 0 } utf16 decode-w/stream ] unit-test
-[ { CHAR: x } ] [ { HEX: fe HEX: ff 0 CHAR: x } utf16 decode-w/stream ] unit-test
-
-[ { HEX: ff HEX: fe 120 0 52 216 30 221 } ] [ { CHAR: x HEX: 1d11e } utf16 encode-w/stream ] unit-test
diff --git a/core/io/encodings/utf16/utf16.factor b/core/io/encodings/utf16/utf16.factor
deleted file mode 100755 (executable)
index a241913..0000000
+++ /dev/null
@@ -1,149 +0,0 @@
-! Copyright (C) 2006, 2008 Daniel Ehrenberg.
-! See http://factorcode.org/license.txt for BSD license.
-USING: math kernel sequences sbufs vectors namespaces io.binary
-io.encodings combinators splitting io byte-arrays ;
-IN: io.encodings.utf16
-
-SYMBOL: double
-SYMBOL: quad1
-SYMBOL: quad2
-SYMBOL: quad3
-SYMBOL: ignore
-
-: do-ignore ( -- ch state ) 0 ignore ;
-
-: append-nums ( byte ch -- ch )
-    8 shift bitor ;
-
-: end-multibyte ( buf byte ch -- buf ch state )
-    append-nums push-decoded ;
-
-: begin-utf16be ( buf byte -- buf ch state )
-    dup -3 shift BIN: 11011 number= [
-        dup BIN: 00000100 bitand zero?
-        [ BIN: 11 bitand quad1 ]
-        [ drop do-ignore ] if
-    ] [ double ] if ;
-
-: handle-quad2be ( byte ch -- ch state )
-    swap dup -2 shift BIN: 110111 number= [
-        >r 2 shift r> BIN: 11 bitand bitor quad3
-    ] [ 2drop do-ignore ] if ;
-
-: decode-utf16be-step ( buf byte ch state -- buf ch state )
-    {
-        { begin [ drop begin-utf16be ] }
-        { double [ end-multibyte ] }
-        { quad1 [ append-nums quad2 ] }
-        { quad2 [ handle-quad2be ] }
-        { quad3 [ append-nums HEX: 10000 + push-decoded ] }
-        { ignore [ 2drop push-replacement ] }
-    } case ;
-
-: decode-utf16be ( seq -- str )
-    [ decode-utf16be-step ] decode ;
-
-: handle-double ( buf byte ch -- buf ch state )
-    swap dup -3 shift BIN: 11011 = [
-        dup BIN: 100 bitand 0 number=
-        [ BIN: 11 bitand 8 shift bitor quad2 ]
-        [ 2drop push-replacement ] if
-    ] [ end-multibyte ] if ;
-
-: handle-quad3le ( buf byte ch -- buf ch state )
-    swap dup -2 shift BIN: 110111 = [
-        BIN: 11 bitand append-nums HEX: 10000 + push-decoded
-    ] [ 2drop push-replacement ] if ;
-
-: decode-utf16le-step ( buf byte ch state -- buf ch state )
-    {
-        { begin [ drop double ] }
-        { double [ handle-double ] }
-        { quad1 [ append-nums quad2 ] }
-        { quad2 [ 10 shift bitor quad3 ] }
-        { quad3 [ handle-quad3le ] }
-    } case ;
-
-: decode-utf16le ( seq -- str )
-    [ decode-utf16le-step ] decode ;
-
-: encode-first
-    -10 shift
-    dup -8 shift BIN: 11011000 bitor
-    swap HEX: FF bitand ;
-
-: encode-second
-    BIN: 1111111111 bitand
-    dup -8 shift BIN: 11011100 bitor
-    swap BIN: 11111111 bitand ;
-
-: char>utf16be ( char -- )
-    dup HEX: FFFF > [
-        HEX: 10000 -
-        dup encode-first swap , ,
-        encode-second swap , ,
-    ] [ h>b/b , , ] if ;
-
-: encode-utf16be ( str -- seq )
-    [ [ char>utf16be ] each ] B{ } make ;
-
-: char>utf16le ( char -- )
-    dup HEX: FFFF > [
-        HEX: 10000 -
-        dup encode-first , ,
-        encode-second , ,
-    ] [ h>b/b swap , , ] if ; 
-
-: encode-utf16le ( str -- seq )
-    [ [ char>utf16le ] each ] B{ } make ;
-
-: bom-le B{ HEX: ff HEX: fe } ; inline
-
-: bom-be B{ HEX: fe HEX: ff } ; inline
-
-: encode-utf16 ( str -- seq )
-    encode-utf16le bom-le swap append ;
-
-: start-utf16le? ( seq1 -- seq2 ? ) bom-le ?head ;
-
-: start-utf16be? ( seq1 -- seq2 ? ) bom-be ?head ;
-
-: decode-utf16 ( seq -- str )
-    {
-        { [ start-utf16le? ] [ decode-utf16le ] }
-        { [ start-utf16be? ] [ decode-utf16be ] }
-        { [ t ] [ decode-error ] }
-    } cond ;
-
-TUPLE: utf16le ;
-
-M: utf16le encode-string drop encode-utf16le ;
-M: utf16le decode-step drop decode-utf16le-step ;
-
-TUPLE: utf16be ;
-
-M: utf16be encode-string drop encode-utf16be ;
-M: utf16be decode-step drop decode-utf16be-step ;
-
-TUPLE: utf16 encoding ;
-M: utf16 underlying-stream delegate dup delegate [ ] [ ] ?if ; ! necessary? 
-M: utf16 set-underlying-stream delegate set-delegate ; ! necessary? 
-
-M: utf16 encode-string
-    >r encode-utf16le r>
-    dup utf16-encoding [ drop ]
-    [ t swap set-utf16-encoding bom-le swap append ] if ;
-
-: bom>le/be ( bom -- le/be )
-    dup bom-le sequence= [ drop utf16le ] [
-        bom-be sequence= [ utf16be ] [ decode-error ] if
-    ] if ;
-
-: read-bom ( utf16 -- encoding )
-    2 over delegate stream-read bom>le/be construct-empty
-    [ swap set-utf16-encoding ] keep ;
-
-M: utf16 decode-step
-    ! inefficient: checks if bom is done many times
-    ! This should transform itself into utf16be or utf16le after reading BOM
-    dup utf16-encoding [ ] [ read-bom ] ?if decode-step ;
diff --git a/extra/db/sqlite/test.db b/extra/db/sqlite/test.db
deleted file mode 100644 (file)
index e483c47..0000000
Binary files a/extra/db/sqlite/test.db and /dev/null differ
diff --git a/extra/io/encodings/ascii/ascii.factor b/extra/io/encodings/ascii/ascii.factor
new file mode 100644 (file)
index 0000000..410c07f
--- /dev/null
@@ -0,0 +1,15 @@
+! Copyright (C) 2008 Daniel Ehrenberg.
+! See http://factorcode.org/license.txt for BSD license.
+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 ;
+
+TUPLE: ascii ;
+
+M: ascii encode-string
+    drop 127 encode-check>= ;
+
+M: ascii decode-step
+    3drop dup 127 >= [ encode-error ] when over push f f ;
diff --git a/extra/io/encodings/ascii/authors.txt b/extra/io/encodings/ascii/authors.txt
new file mode 100644 (file)
index 0000000..f990dd0
--- /dev/null
@@ -0,0 +1 @@
+Daniel Ehrenberg
diff --git a/extra/io/encodings/ascii/summary.txt b/extra/io/encodings/ascii/summary.txt
new file mode 100644 (file)
index 0000000..8c54de7
--- /dev/null
@@ -0,0 +1 @@
+ASCII encoding for streams
diff --git a/extra/io/encodings/ascii/tags.txt b/extra/io/encodings/ascii/tags.txt
new file mode 100644 (file)
index 0000000..8e27be7
--- /dev/null
@@ -0,0 +1 @@
+text
diff --git a/extra/io/encodings/latin1/authors.txt b/extra/io/encodings/latin1/authors.txt
new file mode 100644 (file)
index 0000000..f990dd0
--- /dev/null
@@ -0,0 +1 @@
+Daniel Ehrenberg
diff --git a/extra/io/encodings/latin1/latin1-docs.factor b/extra/io/encodings/latin1/latin1-docs.factor
new file mode 100644 (file)
index 0000000..5872b2b
--- /dev/null
@@ -0,0 +1,5 @@
+USING: help.syntax help.markup ;
+IN: io.encodings.latin1
+
+HELP: latin1
+{ $class-description "This class is used for Latin 1 (ISO 8859-1) encoding and decoding" } ;
diff --git a/extra/io/encodings/latin1/latin1.factor b/extra/io/encodings/latin1/latin1.factor
new file mode 100755 (executable)
index 0000000..7e867b1
--- /dev/null
@@ -0,0 +1,12 @@
+! Copyright (C) 2008 Daniel Ehrenberg.
+! See http://factorcode.org/license.txt for BSD license.
+USING: io io.encodings strings kernel io.encodings.ascii sequences ;
+IN: io.encodings.latin1
+
+TUPLE: latin1 ;
+
+M: latin1 encode-string 
+    drop 255 encode-check>= ;
+
+M: latin1 decode-step
+    3drop over push f f ;
diff --git a/extra/io/encodings/latin1/summary.txt b/extra/io/encodings/latin1/summary.txt
new file mode 100644 (file)
index 0000000..d40d628
--- /dev/null
@@ -0,0 +1 @@
+ISO 8859-1 encoding/decoding
diff --git a/extra/io/encodings/latin1/tags.txt b/extra/io/encodings/latin1/tags.txt
new file mode 100644 (file)
index 0000000..8e27be7
--- /dev/null
@@ -0,0 +1 @@
+text
diff --git a/extra/io/encodings/utf16/.utf16.factor.swo b/extra/io/encodings/utf16/.utf16.factor.swo
new file mode 100644 (file)
index 0000000..01be8fd
Binary files /dev/null and b/extra/io/encodings/utf16/.utf16.factor.swo differ
diff --git a/extra/io/encodings/utf16/authors.txt b/extra/io/encodings/utf16/authors.txt
new file mode 100644 (file)
index 0000000..f990dd0
--- /dev/null
@@ -0,0 +1 @@
+Daniel Ehrenberg
diff --git a/extra/io/encodings/utf16/summary.txt b/extra/io/encodings/utf16/summary.txt
new file mode 100644 (file)
index 0000000..b249067
--- /dev/null
@@ -0,0 +1 @@
+UTF16 encoding/decoding
diff --git a/extra/io/encodings/utf16/tags.txt b/extra/io/encodings/utf16/tags.txt
new file mode 100644 (file)
index 0000000..8e27be7
--- /dev/null
@@ -0,0 +1 @@
+text
diff --git a/extra/io/encodings/utf16/utf16-docs.factor b/extra/io/encodings/utf16/utf16-docs.factor
new file mode 100644 (file)
index 0000000..c49c030
--- /dev/null
@@ -0,0 +1,45 @@
+USING: help.markup help.syntax io.encodings strings ;
+IN: io.encodings.utf16
+
+ARTICLE: "io.utf16" "Working with UTF16-encoded data"
+"The UTF16 encoding is a variable-width encoding. Unicode code points are encoded as 2 or 4 byte sequences."
+{ $subsection encode-utf16le }
+{ $subsection encode-utf16be }
+{ $subsection decode-utf16le }
+{ $subsection decode-utf16be }
+"Support for UTF16 data with a byte order mark:"
+{ $subsection encode-utf16 }
+{ $subsection decode-utf16 } ;
+
+ABOUT: "io.utf16"
+
+HELP: decode-utf16
+{ $values { "seq" "a sequence of bytes" } { "str" string } }
+{ $description "Decodes a sequence of bytes representing a Unicode string in UTF16 format. The bytes must begin with a UTF16 byte order mark, which determines if the input is in little or big endian. To decode data without a byte order mark, use " { $link decode-utf16le } " or " { $link decode-utf16be } "." }
+{ $errors "Throws a " { $link decode-error } " if the input is malformed." } ;
+
+HELP: decode-utf16be
+{ $values { "seq" "a sequence of bytes" } { "str" string } }
+{ $description "Decodes a sequence of bytes representing a Unicode string in big endian UTF16 format. The bytes must not begin with a UTF16 byte order mark. To decode data with a byte order mark, use " { $link decode-utf16 } "." }
+{ $errors "Throws a " { $link decode-error } " if the input is malformed." } ;
+
+HELP: decode-utf16le
+{ $values { "seq" "a sequence of bytes" } { "str" string } }
+{ $description "Decodes a sequence of bytes representing a Unicode string in little endian UTF16 format. The bytes must not begin with a UTF16 byte order mark. To decode data with a byte order mark, use " { $link decode-utf16 } "." }
+{ $errors "Throws a " { $link decode-error } " if the input is malformed." } ;
+
+{ decode-utf16 decode-utf16le decode-utf16be } related-words
+
+HELP: encode-utf16be
+{ $values { "str" string } { "seq" "a sequence of bytes" } }
+{ $description "Encodes a Unicode string as a sequence of bytes in big endian UTF16 format." } ;
+
+HELP: encode-utf16le
+{ $values { "str" string } { "seq" "a sequence of bytes" } }
+{ $description "Encodes a Unicode string as a sequence of bytes in little endian UTF16 format." } ;
+
+HELP: encode-utf16
+{ $values { "str" string } { "seq" "a sequence of bytes" } }
+{ $description "Encodes a Unicode string as a sequence of bytes in UTF16 format with a byte order mark." } ;
+
+{ encode-utf16 encode-utf16be encode-utf16le } related-words
diff --git a/extra/io/encodings/utf16/utf16-tests.factor b/extra/io/encodings/utf16/utf16-tests.factor
new file mode 100755 (executable)
index 0000000..041c486
--- /dev/null
@@ -0,0 +1,28 @@
+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> <decoding> contents >array ;
+
+: encode-w/stream ( array encoding -- newarray )
+    >r SBUF" " clone tuck r> <encoding> 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
+[ { CHAR: replacement-character } ] [ { BIN: 11011111 CHAR: q } utf16be decode-w/stream ] unit-test
+[ { CHAR: replacement-character } ] [ { BIN: 11011011 CHAR: x BIN: 11011011 CHAR: x } utf16be decode-w/stream ] unit-test
+
+[ { 0 120 216 52 221 30 } ] [ { CHAR: x HEX: 1d11e } utf16be encode-w/stream ] unit-test
+
+[ { CHAR: x } ] [ { CHAR: x 0 } utf16le decode-w/stream ] unit-test
+[ { 119070 } ] [ { HEX: 34 HEX: D8 HEX: 1E HEX: DD } utf16le decode-w/stream ] unit-test
+[ { CHAR: replacement-character } ] [ { 0 BIN: 11011111 } utf16le decode-w/stream ] unit-test
+[ { CHAR: replacement-character } ] [ { 0 BIN: 11011011 0 0 } utf16le decode-w/stream ] unit-test
+[ { 119070 } ] [ { HEX: 34 HEX: D8 HEX: 1E HEX: DD } utf16le decode-w/stream ] unit-test
+
+[ { 120 0 52 216 30 221 } ] [ { CHAR: x HEX: 1d11e } utf16le encode-w/stream ] unit-test
+
+[ { CHAR: x } ] [ { HEX: ff HEX: fe CHAR: x 0 } utf16 decode-w/stream ] unit-test
+[ { CHAR: x } ] [ { HEX: fe HEX: ff 0 CHAR: x } utf16 decode-w/stream ] unit-test
+
+[ { HEX: ff HEX: fe 120 0 52 216 30 221 } ] [ { CHAR: x HEX: 1d11e } utf16 encode-w/stream ] unit-test
diff --git a/extra/io/encodings/utf16/utf16.factor b/extra/io/encodings/utf16/utf16.factor
new file mode 100755 (executable)
index 0000000..a241913
--- /dev/null
@@ -0,0 +1,149 @@
+! Copyright (C) 2006, 2008 Daniel Ehrenberg.
+! See http://factorcode.org/license.txt for BSD license.
+USING: math kernel sequences sbufs vectors namespaces io.binary
+io.encodings combinators splitting io byte-arrays ;
+IN: io.encodings.utf16
+
+SYMBOL: double
+SYMBOL: quad1
+SYMBOL: quad2
+SYMBOL: quad3
+SYMBOL: ignore
+
+: do-ignore ( -- ch state ) 0 ignore ;
+
+: append-nums ( byte ch -- ch )
+    8 shift bitor ;
+
+: end-multibyte ( buf byte ch -- buf ch state )
+    append-nums push-decoded ;
+
+: begin-utf16be ( buf byte -- buf ch state )
+    dup -3 shift BIN: 11011 number= [
+        dup BIN: 00000100 bitand zero?
+        [ BIN: 11 bitand quad1 ]
+        [ drop do-ignore ] if
+    ] [ double ] if ;
+
+: handle-quad2be ( byte ch -- ch state )
+    swap dup -2 shift BIN: 110111 number= [
+        >r 2 shift r> BIN: 11 bitand bitor quad3
+    ] [ 2drop do-ignore ] if ;
+
+: decode-utf16be-step ( buf byte ch state -- buf ch state )
+    {
+        { begin [ drop begin-utf16be ] }
+        { double [ end-multibyte ] }
+        { quad1 [ append-nums quad2 ] }
+        { quad2 [ handle-quad2be ] }
+        { quad3 [ append-nums HEX: 10000 + push-decoded ] }
+        { ignore [ 2drop push-replacement ] }
+    } case ;
+
+: decode-utf16be ( seq -- str )
+    [ decode-utf16be-step ] decode ;
+
+: handle-double ( buf byte ch -- buf ch state )
+    swap dup -3 shift BIN: 11011 = [
+        dup BIN: 100 bitand 0 number=
+        [ BIN: 11 bitand 8 shift bitor quad2 ]
+        [ 2drop push-replacement ] if
+    ] [ end-multibyte ] if ;
+
+: handle-quad3le ( buf byte ch -- buf ch state )
+    swap dup -2 shift BIN: 110111 = [
+        BIN: 11 bitand append-nums HEX: 10000 + push-decoded
+    ] [ 2drop push-replacement ] if ;
+
+: decode-utf16le-step ( buf byte ch state -- buf ch state )
+    {
+        { begin [ drop double ] }
+        { double [ handle-double ] }
+        { quad1 [ append-nums quad2 ] }
+        { quad2 [ 10 shift bitor quad3 ] }
+        { quad3 [ handle-quad3le ] }
+    } case ;
+
+: decode-utf16le ( seq -- str )
+    [ decode-utf16le-step ] decode ;
+
+: encode-first
+    -10 shift
+    dup -8 shift BIN: 11011000 bitor
+    swap HEX: FF bitand ;
+
+: encode-second
+    BIN: 1111111111 bitand
+    dup -8 shift BIN: 11011100 bitor
+    swap BIN: 11111111 bitand ;
+
+: char>utf16be ( char -- )
+    dup HEX: FFFF > [
+        HEX: 10000 -
+        dup encode-first swap , ,
+        encode-second swap , ,
+    ] [ h>b/b , , ] if ;
+
+: encode-utf16be ( str -- seq )
+    [ [ char>utf16be ] each ] B{ } make ;
+
+: char>utf16le ( char -- )
+    dup HEX: FFFF > [
+        HEX: 10000 -
+        dup encode-first , ,
+        encode-second , ,
+    ] [ h>b/b swap , , ] if ; 
+
+: encode-utf16le ( str -- seq )
+    [ [ char>utf16le ] each ] B{ } make ;
+
+: bom-le B{ HEX: ff HEX: fe } ; inline
+
+: bom-be B{ HEX: fe HEX: ff } ; inline
+
+: encode-utf16 ( str -- seq )
+    encode-utf16le bom-le swap append ;
+
+: start-utf16le? ( seq1 -- seq2 ? ) bom-le ?head ;
+
+: start-utf16be? ( seq1 -- seq2 ? ) bom-be ?head ;
+
+: decode-utf16 ( seq -- str )
+    {
+        { [ start-utf16le? ] [ decode-utf16le ] }
+        { [ start-utf16be? ] [ decode-utf16be ] }
+        { [ t ] [ decode-error ] }
+    } cond ;
+
+TUPLE: utf16le ;
+
+M: utf16le encode-string drop encode-utf16le ;
+M: utf16le decode-step drop decode-utf16le-step ;
+
+TUPLE: utf16be ;
+
+M: utf16be encode-string drop encode-utf16be ;
+M: utf16be decode-step drop decode-utf16be-step ;
+
+TUPLE: utf16 encoding ;
+M: utf16 underlying-stream delegate dup delegate [ ] [ ] ?if ; ! necessary? 
+M: utf16 set-underlying-stream delegate set-delegate ; ! necessary? 
+
+M: utf16 encode-string
+    >r encode-utf16le r>
+    dup utf16-encoding [ drop ]
+    [ t swap set-utf16-encoding bom-le swap append ] if ;
+
+: bom>le/be ( bom -- le/be )
+    dup bom-le sequence= [ drop utf16le ] [
+        bom-be sequence= [ utf16be ] [ decode-error ] if
+    ] if ;
+
+: read-bom ( utf16 -- encoding )
+    2 over delegate stream-read bom>le/be construct-empty
+    [ swap set-utf16-encoding ] keep ;
+
+M: utf16 decode-step
+    ! inefficient: checks if bom is done many times
+    ! This should transform itself into utf16be or utf16le after reading BOM
+    dup utf16-encoding [ ] [ read-bom ] ?if decode-step ;