From fb5111d7dcb79bb793fc6a5688e58f44c5260bad Mon Sep 17 00:00:00 2001 From: Henning Guenther Date: Tue, 3 Mar 2009 04:56:41 -0800 Subject: [PATCH] ISO2022JP encoding Ignore-this: fa857024fa249fff451f5c024962484e darcs-hash:20090303125641-a4fee-ac96e5788bb6b3cdf61f2e19f4bd19ece710e24b --- Data/Encoding/ISO2022JP.hs | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Data/Encoding/ISO2022JP.hs diff --git a/Data/Encoding/ISO2022JP.hs b/Data/Encoding/ISO2022JP.hs new file mode 100644 index 0000000..09b9f15 --- /dev/null +++ b/Data/Encoding/ISO2022JP.hs @@ -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 \ No newline at end of file