encoding/Data/Encoding/ISO2022JP.hs
Daniel Wagner 8b1f45a6ec binary-0.6 compatibility
Ignore-this: 6af2adadedc20f51bb5084b3da59724e

darcs-hash:20121213030806-76d51-6d52680cab9b4f4b6c2ba17e29fa457b85d4d838
2012-12-12 19:08:06 -08:00

52 lines
1.9 KiB
Haskell

{-# 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 _ = fetchAhead $ do
w <- fetchWord8
if w == 27
then (do
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