encoding/Data/Encoding/ASCII.hs
Henning Guenther cb81698b7e Derived all encodings from Show
This way, we can declare DynEncoding an instance of Show

darcs-hash:20070827020833-a4fee-a84635bf911573022b780c17b2085ff814302b3e
2007-08-26 19:08:33 -07:00

30 lines
914 B
Haskell

-- | ASCII (American Standard Code for Information Interchange) is the
-- \"normal\" computer encoding using the byte values 0-127 to represent
-- characters. Refer to <http://en.wikipedia.org/wiki/ASCII> for
-- more informations.
module Data.Encoding.ASCII
(ASCII(..)) where
import Control.Exception (throwDyn)
import Data.ByteString (pack)
import qualified Data.ByteString.Lazy as Lazy (pack)
import Data.ByteString.Char8 (unpack)
import Data.ByteString.Base (c2w)
import qualified Data.ByteString.Lazy as Lazy
import Data.Encoding.Base
import Data.Word
data ASCII = ASCII deriving Show
charToASCII :: Char -> Word8
charToASCII ch = if ch < '\128'
then c2w ch
else throwDyn (HasNoRepresentation ch)
instance Encoding ASCII where
encode _ str = pack (map charToASCII str)
encodeLazy _ str = Lazy.pack (map charToASCII str)
encodable _ ch = ch < '\128'
decode _ = unpack
decodable _ = const True