]> gitweb.factorcode.org Git - factor.git/commitdiff
io.encodings.latin1: adding a memory efficient latin1 encoding.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 28 Feb 2018 16:54:50 +0000 (08:54 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 28 Feb 2018 16:56:01 +0000 (08:56 -0800)
basis/io/encodings/latin1/latin1-docs.factor [new file with mode: 0644]
basis/io/encodings/latin1/latin1-tests.factor [new file with mode: 0644]
basis/io/encodings/latin1/latin1.factor [new file with mode: 0644]

diff --git a/basis/io/encodings/latin1/latin1-docs.factor b/basis/io/encodings/latin1/latin1-docs.factor
new file mode 100644 (file)
index 0000000..8c0c3de
--- /dev/null
@@ -0,0 +1,5 @@
+USING: help.markup help.syntax io.encodings.latin1 ;
+
+HELP: latin1
+{ $var-description "This is the ISO-8859-1 encoding, also called Latin-1: Western European. It is an 8-bit superset of ASCII which is the default for a mimetype starting with 'text' and provides the characters necessary for most western European languages." }
+{ $see-also "encodings-introduction" } ;
diff --git a/basis/io/encodings/latin1/latin1-tests.factor b/basis/io/encodings/latin1/latin1-tests.factor
new file mode 100644 (file)
index 0000000..fed4b86
--- /dev/null
@@ -0,0 +1,14 @@
+USING: arrays io.encodings.latin1 io.encodings.string strings
+tools.test ;
+
+{ B{ CHAR: f CHAR: o CHAR: o } } [ "foo" latin1 encode ] unit-test
+
+[ { 256 } >string latin1 encode ] must-fail
+
+{ B{ 255 } } [ { 255 } >string latin1 encode ] unit-test
+
+{ "bar" } [ "bar" latin1 decode ] unit-test
+
+{ { CHAR: b 233 CHAR: r } } [
+    B{ CHAR: b 233 CHAR: r } latin1 decode >array
+] unit-test
diff --git a/basis/io/encodings/latin1/latin1.factor b/basis/io/encodings/latin1/latin1.factor
new file mode 100644 (file)
index 0000000..3932c06
--- /dev/null
@@ -0,0 +1,15 @@
+USING: io io.encodings io.encodings.iana kernel math ;
+
+IN: io.encodings.latin1
+
+SINGLETON: latin1
+
+M: latin1 encode-char
+    drop over 256 < [ stream-write1 ] [ encode-error ] if ;
+
+M: latin1 decode-char
+    drop stream-read1 [
+        dup 256 < [ drop replacement-char ] unless
+    ] [ f ] if* ;
+
+latin1 "ISO_8859-1:1987" register-encoding