! Copyright (C) 2008 Daniel Ehrenberg ! 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 classes.singleton classes.mixin io.encodings.iana ; IN: io.encodings.8-bit ] map ] map ; : byte>ch ( assoc -- array ) 256 replacement-char [ [ swapd set-nth ] curry assoc-each ] keep ; : ch>byte ( assoc -- newassoc ) [ swap ] assoc-map >hashtable ; : parse-file ( path -- byte>ch ch>byte ) ascii file-lines process-contents [ byte>ch ] [ ch>byte ] bi ; SYMBOL: 8-bit-encodings TUPLE: 8-bit decode encode ; : encode-8-bit ( char stream assoc -- ) swapd at* [ swap stream-write1 ] [ nip encode-error ] if ; inline M: 8-bit encode-char encode>> encode-8-bit ; : decode-8-bit ( stream array -- char/f ) swap stream-read1 dup [ swap nth [ replacement-char ] unless* ] [ 2drop f ] if ; inline M: 8-bit decode-char decode>> decode-8-bit ; MIXIN: 8-bit-encoding M: 8-bit-encoding 8-bit-encodings get-global at ; M: 8-bit-encoding 8-bit-encodings get-global at ; : create-encoding ( name -- word ) "io.encodings.8-bit" create [ define-singleton-class ] [ 8-bit-encoding add-mixin-instance ] [ ] tri ; PRIVATE> [ mappings [ first3 [ create-encoding ] [ dupd register-encoding ] [ encoding-file parse-file 8-bit boa ] tri* ] H{ } map>assoc 8-bit-encodings set-global ] with-compilation-unit