File: ISO2022JP.hs

package info (click to toggle)
haskell-encoding 0.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,392 kB
  • sloc: haskell: 4,372; ansic: 11; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{-# 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