diff --git a/Data/Encoding/ASCII.hs b/Data/Encoding/ASCII.hs index cde373a..990ae2d 100644 --- a/Data/Encoding/ASCII.hs +++ b/Data/Encoding/ASCII.hs @@ -14,4 +14,5 @@ instance Encoding ASCII where w <- fetchWord8 return $ chr $ fromIntegral w encodeChar _ c = do - pushWord8 $ fromIntegral $ ord c \ No newline at end of file + pushWord8 $ fromIntegral $ ord c + encodeable _ c = c < '\128' \ No newline at end of file diff --git a/Data/Encoding/Base.hs b/Data/Encoding/Base.hs index 2934633..2ef95e8 100644 --- a/Data/Encoding/Base.hs +++ b/Data/Encoding/Base.hs @@ -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 diff --git a/Data/Encoding/BootString.hs b/Data/Encoding/BootString.hs index ab82e4d..ea6f5cf 100644 --- a/Data/Encoding/BootString.hs +++ b/Data/Encoding/BootString.hs @@ -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? \ No newline at end of file diff --git a/Data/Encoding/GB18030.hs b/Data/Encoding/GB18030.hs index 83c9daa..a364079 100644 --- a/Data/Encoding/GB18030.hs +++ b/Data/Encoding/GB18030.hs @@ -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 diff --git a/Data/Encoding/Helper/Template.hs b/Data/Encoding/Helper/Template.hs index 68233b7..d1619b5 100644 --- a/Data/Encoding/Helper/Template.hs +++ b/Data/Encoding/Helper/Template.hs @@ -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 diff --git a/Data/Encoding/ISO88591.hs b/Data/Encoding/ISO88591.hs index 8ccff3b..fa48313 100644 --- a/Data/Encoding/ISO88591.hs +++ b/Data/Encoding/ISO88591.hs @@ -17,4 +17,5 @@ instance Encoding ISO88591 where | otherwise = pushWord8 (fromIntegral $ ord c) decodeChar _ = do w <- fetchWord8 - return (chr $ fromIntegral w) \ No newline at end of file + return (chr $ fromIntegral w) + encodeable _ c = c <= '\255' \ No newline at end of file diff --git a/Data/Encoding/KOI8R.hs b/Data/Encoding/KOI8R.hs index 2c5ee75..f1ee2dc 100644 --- a/Data/Encoding/KOI8R.hs +++ b/Data/Encoding/KOI8R.hs @@ -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) \ No newline at end of file + Nothing -> throwException (HasNoRepresentation ch) + encodeable _ c = member c koi8rMap \ No newline at end of file diff --git a/Data/Encoding/KOI8U.hs b/Data/Encoding/KOI8U.hs index f386fe0..7a27f14 100644 --- a/Data/Encoding/KOI8U.hs +++ b/Data/Encoding/KOI8U.hs @@ -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) \ No newline at end of file + Nothing -> throwException (HasNoRepresentation ch) + encodeable _ c = member c koi8uMap \ No newline at end of file diff --git a/Data/Encoding/UTF16.hs b/Data/Encoding/UTF16.hs index 11c5fcf..25bbb7b 100644 --- a/Data/Encoding/UTF16.hs +++ b/Data/Encoding/UTF16.hs @@ -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' \ No newline at end of file diff --git a/Data/Encoding/UTF32.hs b/Data/Encoding/UTF32.hs index fab3ca6..71e5d66 100644 --- a/Data/Encoding/UTF32.hs +++ b/Data/Encoding/UTF32.hs @@ -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 \ No newline at end of file diff --git a/Data/Encoding/UTF8.hs b/Data/Encoding/UTF8.hs index b78fdfe..9cb3f27 100644 --- a/Data/Encoding/UTF8.hs +++ b/Data/Encoding/UTF8.hs @@ -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