]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/io/encodings/8-bit/8-bit.factor
factor: trim using lists
[factor.git] / basis / io / encodings / 8-bit / 8-bit.factor
index f5b102ba31320c5c706f83fe35d09661d74da233..a1abf8ab658d4214b7c61522479b1cd2d4bd6372 100644 (file)
@@ -1,88 +1,98 @@
-! Copyright (C) 2008 Daniel Ehrenberg
+! Copyright (C) 2008 Daniel Ehrenberg, Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: math.parser arrays io.encodings sequences kernel assocs
-hashtables io.encodings.ascii generic parser classes.tuple words
-words.symbol io io.files splitting namespaces math
-compiler.units accessors ;
+USING: accessors arrays assocs classes.singleton generic
+hashtables io io.encodings io.encodings.iana kernel lexer parser
+sequences simple-flat-file words ;
 IN: io.encodings.8-bit
 
+<<
 <PRIVATE
 
-: mappings {
-    { "latin1" "8859-1" }
-    { "latin2" "8859-2" }
-    { "latin3" "8859-3" }
-    { "latin4" "8859-4" }
-    { "latin/cyrillic" "8859-5" }
-    { "latin/arabic" "8859-6" }
-    { "latin/greek" "8859-7" }
-    { "latin/hebrew" "8859-8" }
-    { "latin5" "8859-9" }
-    { "latin6" "8859-10" }
-    { "latin/thai" "8859-11" }
-    { "latin7" "8859-13" }
-    { "latin8" "8859-14" }
-    { "latin9" "8859-15" }
-    { "latin10" "8859-16" }
-    { "koi8-r" "KOI8-R" }
-    { "windows-1252" "CP1252" }
-    { "ebcdic" "CP037" }
-    { "mac-roman" "ROMAN" }
-} ;
-
 : encoding-file ( file-name -- stream )
-    "resource:basis/io/encodings/8-bit/" swap ".TXT"
-    3append ascii <file-reader> ;
-
-: process-contents ( lines -- assoc )
-    [ "#" split1 drop ] map harvest
-    [ "\t" split 2 head [ 2 short tail hex> ] map ] map ;
-
-: byte>ch ( assoc -- array )
-    256 replacement-char <array>
-    [ [ swapd set-nth ] curry assoc-each ] keep ;
-
-: ch>byte ( assoc -- newassoc )
-    [ swap ] assoc-map >hashtable ;
-
-: parse-file ( stream -- byte>ch ch>byte )
-    lines process-contents
-    [ byte>ch ] [ ch>byte ] bi ;
-
-SYMBOL: 8-bit-encodings
+    "vocab:io/encodings/8-bit/" ".TXT" surround ;
 
-TUPLE: 8-bit decode encode ;
+TUPLE: 8-bit { from array read-only } { to hashtable read-only } ;
 
-: encode-8-bit ( char stream assoc -- )
-    swapd at*
-    [ swap stream-write1 ] [ nip encode-error ] if ; inline
+: <8-bit> ( biassoc -- 8-bit )
+    [ from>> 256 <iota> [ of ] with map ] [ to>> ] bi 8-bit boa ;
 
-M: 8-bit encode-char encode>> encode-8-bit ;
+: 8-bit-encode ( char 8-bit -- byte )
+    to>> at [ encode-error ] unless* ; inline
 
-: decode-8-bit ( stream array -- char/f )
-    swap stream-read1 dup
-    [ swap nth [ replacement-char ] unless* ] [ 2drop f ] if ; inline
+M: 8-bit encode-char
+    swap [ 8-bit-encode ] dip stream-write1 ;
 
-M: 8-bit decode-char decode>> decode-8-bit ;
+M: 8-bit encode-string
+    swap [ '[ _ 8-bit-encode ] B{ } map-as ] dip stream-write ;
 
-PREDICATE: 8-bit-encoding < word
-    8-bit-encodings get-global key? ;
+M: 8-bit decode-char
+    swap stream-read1 [
+        swap from>> ?nth [ replacement-char ] unless*
+    ] [ drop f ] if* ;
 
-M: 8-bit-encoding <encoder>
-    8-bit-encodings get-global at <encoder> ;
+: create-encoding ( name -- word )
+    create-word-in dup define-singleton-class ;
 
-M: 8-bit-encoding <decoder>
-    8-bit-encodings get-global at <decoder> ;
+: load-encoding ( name iana-name file-name -- )
+    [ create-encoding dup ]
+    [ register-encoding ]
+    [ encoding-file load-codetable-file <8-bit> ] tri*
+    [ [ \ <encoder> create-method ] dip '[ drop _ <encoder> ] define ]
+    [ [ \ <decoder> create-method ] dip '[ drop _ <decoder> ] define ] 2bi ;
 
 PRIVATE>
 
-[
-    mappings [
-        [ "io.encodings.8-bit" create ]
-        [ encoding-file parse-file 8-bit boa ]
-        bi*
-    ] assoc-map
-    [ keys [ define-symbol ] each ]
-    [ 8-bit-encodings set-global ]
-    bi
-] with-compilation-unit
+SYNTAX: 8-BIT: scan-token scan-token scan-token load-encoding ;
+>>
+
+8-BIT: cp424 IBM424 CP424
+8-BIT: cp437 IBM437 CP437
+8-BIT: cp500 IBM500 CP500
+8-BIT: cp775 IBM775 CP775
+8-BIT: cp850 IBM850 CP850
+8-BIT: cp852 IBM852 CP852
+8-BIT: cp855 IBM855 CP855
+8-BIT: cp857 IBM857 CP857
+8-BIT: cp860 IBM860 CP860
+8-BIT: cp861 IBM861 CP861
+8-BIT: cp862 IBM862 CP862
+8-BIT: cp863 IBM863 CP863
+8-BIT: cp864 IBM864 CP864
+8-BIT: cp865 IBM865 CP865
+8-BIT: cp866 IBM866 CP866
+8-BIT: cp869 IBM869 CP869
+8-BIT: cp1026 IBM1026 CP1026
+8-BIT: ebcdic IBM037 CP037
+8-BIT: kz1048 KZ-1048 KZ1048
+8-BIT: koi8-r KOI8-R KOI8-R
+8-BIT: koi8-u KOI8-U KOI8-U
+8-BIT: latin/arabic ISO_8859-6:1987 8859-6
+8-BIT: latin/cyrillic ISO_8859-5:1988 8859-5
+8-BIT: latin/greek ISO_8859-7:1987 8859-7
+8-BIT: latin/hebrew ISO_8859-8:1988 8859-8
+8-BIT: latin/thai TIS-620 8859-11
+! 8-BIT: latin1 ISO_8859-1:1987 8859-1
+8-BIT: latin2 ISO_8859-2:1987 8859-2
+8-BIT: latin3 ISO_8859-3:1988 8859-3
+8-BIT: latin4 ISO_8859-4:1988 8859-4
+8-BIT: latin5 ISO_8859-9:1989 8859-9
+8-BIT: latin6 ISO-8859-10 8859-10
+8-BIT: latin7 ISO-8859-13 8859-13
+8-BIT: latin8 ISO-8859-14 8859-14
+8-BIT: latin9 ISO-8859-15 8859-15
+8-BIT: latin10 ISO-8859-16 8859-16
+8-BIT: mac-roman macintosh ROMAN
+! 8-BIT: mac-cyrillic mac-cyrillic CYRILLIC
+! 8-BIT: mac-greek mac-greek GREEK
+! 8-BIT: mac-icelandic mac-icelandic ICELAND
+! 8-BIT: mac-latin2 mac-latin2 LATIN2
+! 8-BIT: mac-turkish mac-turkish TURKISH
+8-BIT: windows-1250 windows-1250 CP1250
+8-BIT: windows-1251 windows-1251 CP1251
+8-BIT: windows-1252 windows-1252 CP1252
+8-BIT: windows-1253 windows-1253 CP1253
+8-BIT: windows-1254 windows-1254 CP1254
+8-BIT: windows-1255 windows-1255 CP1255
+8-BIT: windows-1256 windows-1256 CP1256
+8-BIT: windows-1257 windows-1257 CP1257
+8-BIT: windows-1258 windows-1258 CP1258