let D.E.ASCII conform to the spec

Ignore-this: 788eb99b3948ce201769471919562114

The contract for the "encodeable" function says, in part, "If it yields False,
encodeChar must throw an exception.". This patch makes the ASCII encoding
instance throw an exception for non-ASCII characters.

darcs-hash:20100909222914-76d51-76d9891ae18f13751b004e2b6ed24401192ae79f
This commit is contained in:
Daniel Wagner 2010-09-09 15:29:14 -07:00
parent fef1fbd22f
commit a3a24776a3

View File

@ -1,10 +1,12 @@
{-# LANGUAGE DeriveDataTypeable #-}
module Data.Encoding.ASCII where
import Control.Throws
import Data.Char
import Data.Encoding.Base
import Data.Encoding.ByteSource
import Data.Encoding.ByteSink
import Data.Encoding.Exception
import Data.Typeable
data ASCII = ASCII deriving (Show,Eq,Typeable)
@ -13,6 +15,7 @@ instance Encoding ASCII where
decodeChar _ = do
w <- fetchWord8
return $ chr $ fromIntegral w
encodeChar _ c = do
pushWord8 $ fromIntegral $ ord c
encodeChar enc c
| encodeable enc c = pushWord8 . fromIntegral . ord $ c
| otherwise = throwException . HasNoRepresentation $ c
encodeable _ c = c < '\128'