factor/basis/io/encodings/8-bit/8-bit.factor

56 lines
1.5 KiB
Factor
Raw Normal View History

2009-11-12 17:38:21 -05:00
! Copyright (C) 2008 Daniel Ehrenberg, Doug Coleman.
2008-03-21 12:30:13 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-03-29 00:00:20 -04:00
USING: math.parser arrays io.encodings sequences kernel assocs
hashtables io.encodings.ascii generic parser classes.tuple words
2008-12-17 19:22:48 -05:00
words.symbol io io.files splitting namespaces math
2009-02-03 18:32:05 -05:00
compiler.units accessors classes.singleton classes.mixin
2009-11-12 17:38:21 -05:00
io.encodings.iana fry simple-flat-file lexer ;
2008-03-21 12:30:13 -04:00
IN: io.encodings.8-bit
<PRIVATE
2008-04-06 20:03:00 -04:00
: encoding-file ( file-name -- stream )
"vocab:io/encodings/8-bit/" ".TXT" surround ;
2008-03-21 14:01:50 -04:00
2008-06-19 00:29:56 -04:00
SYMBOL: 8-bit-encodings
8-bit-encodings [ H{ } clone ] initialize
2008-06-19 00:29:56 -04:00
TUPLE: 8-bit biassoc ;
2008-03-21 12:30:13 -04:00
: encode-8-bit ( char stream assoc -- )
swapd value-at
[ swap stream-write1 ] [ encode-error ] if* ; inline
2008-03-21 12:30:13 -04:00
M: 8-bit encode-char biassoc>> encode-8-bit ;
2008-03-21 12:30:13 -04:00
: decode-8-bit ( stream assoc -- char/f )
swap stream-read1
[ swap at [ replacement-char ] unless* ]
[ drop f ] if* ; inline
2008-03-21 12:30:13 -04:00
M: 8-bit decode-char biassoc>> decode-8-bit ;
2008-03-21 12:30:13 -04:00
2009-02-03 18:32:05 -05:00
MIXIN: 8-bit-encoding
2008-06-12 04:49:29 -04:00
2008-06-19 00:29:56 -04:00
M: 8-bit-encoding <encoder>
8-bit-encodings get-global at <encoder> ;
2008-06-12 04:49:29 -04:00
2008-06-19 00:29:56 -04:00
M: 8-bit-encoding <decoder>
8-bit-encodings get-global at <decoder> ;
2008-06-12 04:49:29 -04:00
2009-02-03 18:32:05 -05:00
: create-encoding ( name -- word )
create-in
2009-02-03 18:32:05 -05:00
[ define-singleton-class ]
[ 8-bit-encoding add-mixin-instance ]
[ ] tri ;
2009-11-12 17:38:21 -05:00
: load-encoding ( name iana-name file-name -- )
[ create-encoding dup ]
[ register-encoding ]
[ encoding-file flat-file>biassoc 8-bit boa ] tri*
swap 8-bit-encodings get-global set-at ;
2008-03-21 12:30:13 -04:00
PRIVATE>
2009-11-12 17:38:21 -05:00
SYNTAX: 8-BIT: scan scan scan load-encoding ;