From c763d92e12b0de3b9bff52b001c1f0b3d14d84be Mon Sep 17 00:00:00 2001 From: "Yun, Jonghyouk" Date: Sun, 1 Mar 2009 02:24:03 +0900 Subject: [PATCH] io.encodings.korean iso2022kr encode-char working... --- basis/io/encodings/korean/korean.factor | 68 ++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/basis/io/encodings/korean/korean.factor b/basis/io/encodings/korean/korean.factor index fecf339bdb..4ac1a87074 100644 --- a/basis/io/encodings/korean/korean.factor +++ b/basis/io/encodings/korean/korean.factor @@ -3,7 +3,8 @@ USING: assocs byte-arrays combinators io io.encodings io.encodings.ascii io.encodings.iana io.files kernel locals math math.order math.parser values multiline sequences splitting -values hashtables io.binary io.encodings.asian ; +values hashtables io.binary io.encodings.asian math.ranges +namespaces ; IN: io.encodings.korean @@ -13,7 +14,8 @@ cp949 "EUC-KR" register-encoding SINGLETON: johab -! johab "JOHAB" register-encoding +SINGLETON: iso2022kr + unicode ] [ johab-1st? ] decode-char-mb ; +! iso-2022-kr encodings + +: shift-in ( -- c ) HEX: 0F ; +: shift-out ( -- c ) HEX: 0E ; +: designator ( -- s ) { CHAR: $ CHAR: \ CHAR: ) CHAR: C } ; + +: GR-range ( -- r ) HEX: A1 HEX: FE [a,b] ; +: GL-range ( -- r ) HEX: 21 HEX: 7E [a,b] ; + +: GR>GL ( -- assoc ) + GR-range GL-range zip >hashtable ; + +: GL>GR ( -- assoc ) + GL-range GR-range zip >hashtable ; + + +SYMBOL: *iso2022kr-status* + +H{ } *iso2022kr-status* set-global + +: iso2022kr-stream-get-status ( stream -- so/si/f ) + *iso2022kr-status* get-global swap at ; + +: iso2022kr-stream-get-status* ( stream -- so/si ) + iso2022kr-stream-get-status + [ shift-in ] unless* ; + +:: iso2022kr-stream-set-status ( stream so/si -- ) + so/si stream *iso2022kr-status* get-global set-at ; + +: iso2022kr-stream-shift-out? ( stream -- ? ) + iso2022kr-stream-get-status* shift-out = ; + + +M: iso2022kr encode-char ( char stream encoding -- ) + drop + [let | stream [ ] + char [ ] | + char unicode>cp949 byte? + [ + ! if written, then enclose with . + stream iso2022kr-stream-shift-out? + [ shift-in 1byte-array stream stream-write ] [ ] if + ! plain ascii + char 1byte-array stream stream-write + ] + [ + ! if is closed, then start it. + stream iso2022kr-stream-shift-out? not + [ shift-out 1byte-array stream stream-write ] [ ] if + ! + char unicode>cp949 h>b/b swap 2byte-array + ! GR -> GL + [ GR>GL at ] map + ! + stream stream-write + ] if + ] ; + + + + PRIVATE>