ISO2022JP encoding

Ignore-this: fa857024fa249fff451f5c024962484e

darcs-hash:20090303125641-a4fee-ac96e5788bb6b3cdf61f2e19f4bd19ece710e24b
This commit is contained in:
Henning Guenther 2009-03-03 04:56:41 -08:00
parent 7f65298d3a
commit fb5111d7dc

View File

@ -0,0 +1,52 @@
{-# LANGUAGE DeriveDataTypeable #-}
{- | Implements the japanese character encoding ISO 2022-JP.
See http://tools.ietf.org/html/rfc1468 for reference.
-}
module Data.Encoding.ISO2022JP where
import Data.Typeable
import Data.Encoding.Base
import Data.Encoding.Exception
import Data.Encoding.ByteSource
import Data.Encoding.ISO2022
import Data.Encoding.ASCII
import Data.Encoding.JISX0201
import Data.Encoding.JISX0208
import Control.Throws
data ISO2022JP = ISO2022JP deriving (Eq,Show,Typeable)
instance Encoding ISO2022JP where
encodeChar = encodeCharISO2022
decodeChar = decodeCharISO2022
encode = encodeISO2022
decode = decodeISO2022
encodeable _ c = encodeable ASCII c || encodeable JISX0201 c || encodeable JISX0208 c
instance ISO2022 ISO2022JP where
readEscape _ = do
w <- fetchAhead fetchWord8
if w == 27
then (do
fetchWord8
w2 <- fetchWord8
w3 <- fetchWord8
case w2 of
40 -> case w3 of
66 -> return $ Just $ DynEncoding ASCII
74 -> return $ Just $ DynEncoding JISX0201
_ -> throwException (IllegalCharacter w3)
36 -> case w3 of
64 -> return $ Just $ DynEncoding JISX0208 -- XXX: this actually has to be the 1978 version of the standard... too bad I can't find it
66 -> return $ Just $ DynEncoding JISX0208
_ -> throwException (IllegalCharacter w3)
_ -> throwException (IllegalCharacter w2)
)
else return Nothing
encodingForChar _ c
| encodeable ASCII c = Just (DynEncoding ASCII,[27,40,66])
| encodeable JISX0201 c = Just (DynEncoding JISX0201,[27,40,74])
| encodeable JISX0208 c = Just (DynEncoding JISX0208,[27,36,66])
| otherwise = Nothing