Added encodeable function

darcs-hash:20090225040044-a4fee-67753f6d651b18d5bd3a28340ea8f5d7ea7eb90d
This commit is contained in:
Henning Guenther 2009-02-24 20:00:44 -08:00
parent 1543e75f50
commit 5528bf1a55
11 changed files with 22 additions and 6 deletions

View File

@ -14,4 +14,5 @@ instance Encoding ASCII where
w <- fetchWord8
return $ chr $ fromIntegral w
encodeChar _ c = do
pushWord8 $ fromIntegral $ ord c
pushWord8 $ fromIntegral $ ord c
encodeable _ c = c < '\128'

View File

@ -61,6 +61,9 @@ encodeWithMap2 mp c = case Map.lookup c mp of
pushWord8 w1
pushWord8 w2
encodeableWithMap :: Map Char a -> Char -> Bool
encodeableWithMap = flip Map.member
decodeWithArray :: ByteSource m => Array Word8 (Maybe Char) -> m Char
decodeWithArray arr = do
w <- fetchWord8

View File

@ -182,3 +182,4 @@ instance Encoding BootString where
Nothing -> punyDecode base nbase
Just ww -> throwException (IllegalCharacter ww)
Nothing -> punyDecode [] wrds
encodeable bs c = True -- XXX: hm, really?

View File

@ -127,6 +127,7 @@ instance Encoding GB18030 where
2 -> pushWord8 w2
3 -> pushWord8 w2 >> pushWord8 w3
4 -> pushWord8 w2 >> pushWord8 w3 >> pushWord8 w4
encodeable _ c = c <= '\x10FFFF'
linear :: Word8 -> Word8 -> Word8 -> Word8 -> Int
linear w1 w2 w3 w4

View File

@ -15,14 +15,14 @@ makeISOInstance name file = do
trans <- runIO (readTranslation file)
mp <- encodingMap (validTranslations trans)
arr <- decodingArray (fillTranslations 0 255 trans)
return $ encodingInstance 'encodeWithMap 'decodeWithArray name mp arr
return $ encodingInstance 'encodeWithMap 'decodeWithArray 'encodeableWithMap name mp arr
makeJISInstance :: String -> FilePath -> Q [Dec]
makeJISInstance name file = do
trans <- runIO (readJISTranslation file)
mp <- encodingMap2 (validTranslations trans)
arr <- decodingArray2 (fillTranslations (0x21,0x21) (0x7E,0x7E) trans)
return $ encodingInstance 'encodeWithMap2 'decodeWithArray2 name mp arr
return $ encodingInstance 'encodeWithMap2 'decodeWithArray2 'encodeableWithMap name mp arr
encodingInstance :: Name -> Name -> Name -> String -> Exp -> Exp -> [Dec]
encodingInstance enc dec able name mp arr
@ -36,6 +36,10 @@ encodingInstance enc dec able name mp arr
[Clause [WildP] (NormalB $ AppE (VarE dec) (VarE rarr))
[ValD (VarP rarr) (NormalB arr) []]
]
,FunD 'encodeable
[Clause [WildP] (NormalB $ AppE (VarE able) (VarE rmp))
[ValD (VarP rmp) (NormalB mp) []]
]
]
]
where

View File

@ -17,4 +17,5 @@ instance Encoding ISO88591 where
| otherwise = pushWord8 (fromIntegral $ ord c)
decodeChar _ = do
w <- fetchWord8
return (chr $ fromIntegral w)
return (chr $ fromIntegral w)
encodeable _ c = c <= '\255'

View File

@ -56,4 +56,5 @@ instance Encoding KOI8R where
| ch < '\128' = pushWord8 $ fromIntegral $ ord ch
| otherwise = case lookup ch koi8rMap of
Just w -> pushWord8 w
Nothing -> throwException (HasNoRepresentation ch)
Nothing -> throwException (HasNoRepresentation ch)
encodeable _ c = member c koi8rMap

View File

@ -56,4 +56,5 @@ instance Encoding KOI8U where
| ch < '\128' = pushWord8 $ fromIntegral $ ord ch
| otherwise = case lookup ch koi8uMap of
Just w -> pushWord8 w
Nothing -> throwException (HasNoRepresentation ch)
Nothing -> throwException (HasNoRepresentation ch)
encodeable _ c = member c koi8uMap

View File

@ -79,3 +79,4 @@ instance Encoding UTF16 where
return (c:cs)
Right bom -> decode bom
decode enc = untilM sourceEmpty (decodeChar enc)
encodeable _ c = c <= '\x10FFFF'

View File

@ -44,3 +44,4 @@ instance Encoding UTF32 where
rest <- untilM sourceEmpty (decodeChar UTF32)
return ((chr $ fromIntegral ch):rest)
decode enc = untilM sourceEmpty (decodeChar enc)
encodeable _ _ = True

View File

@ -38,6 +38,7 @@ instance Encoding UTF8 where
where
n = ord c
p8 = pushWord8.fromIntegral
encodeable _ c = c <= '\x10FFFF'
decodeChar UTF8 = do
w1 <- fetchWord8
case () of