1 {-# OPTIONS_GHC -XNoImplicitPrelude -funbox-strict-fields #-}
2 {-# LANGUAGE BangPatterns #-}
3 -----------------------------------------------------------------------------
5 -- Module : GHC.IO.Encoding.Latin1
6 -- Copyright : (c) The University of Glasgow, 2009
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : libraries@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable
13 -- UTF-32 Codecs for the IO library
15 -- Portions Copyright : (c) Tom Harper 2008-2009,
16 -- (c) Bryan O'Sullivan 2009,
17 -- (c) Duncan Coutts 2009
19 -----------------------------------------------------------------------------
21 module GHC.IO.Encoding.Latin1 (
26 latin1_checked_encode,
33 import GHC.IO.Exception
35 import GHC.IO.Encoding.Types
38 -- -----------------------------------------------------------------------------
41 latin1 :: TextEncoding
42 latin1 = TextEncoding { mkTextDecoder = latin1_DF,
43 mkTextEncoder = latin1_EF }
45 latin1_DF :: IO (TextDecoder ())
48 encode = latin1_decode,
51 setState = const $ return ()
54 latin1_EF :: IO (TextEncoder ())
57 encode = latin1_encode,
60 setState = const $ return ()
63 latin1_checked :: TextEncoding
64 latin1_checked = TextEncoding { mkTextDecoder = latin1_DF,
65 mkTextEncoder = latin1_checked_EF }
67 latin1_checked_EF :: IO (TextEncoder ())
70 encode = latin1_checked_encode,
73 setState = const $ return ()
77 latin1_decode :: DecodeBuffer
79 input@Buffer{ bufRaw=iraw, bufL=ir0, bufR=iw, bufSize=_ }
80 output@Buffer{ bufRaw=oraw, bufL=_, bufR=ow0, bufSize=os }
83 | ow >= os || ir >= iw = done ir ow
85 c0 <- readWord8Buf iraw ir
86 ow' <- writeCharBuf oraw ow (unsafeChr (fromIntegral c0))
89 -- lambda-lifted, to avoid thunks being built in the inner-loop:
90 done !ir !ow = return (if ir == iw then input{ bufL=0, bufR=0 }
91 else input{ bufL=ir },
96 latin1_encode :: EncodeBuffer
98 input@Buffer{ bufRaw=iraw, bufL=ir0, bufR=iw, bufSize=_ }
99 output@Buffer{ bufRaw=oraw, bufL=_, bufR=ow0, bufSize=os }
101 done !ir !ow = return (if ir == iw then input{ bufL=0, bufR=0 }
102 else input{ bufL=ir },
105 | ow >= os || ir >= iw = done ir ow
107 (c,ir') <- readCharBuf iraw ir
108 writeWord8Buf oraw ow (fromIntegral (ord c))
113 latin1_checked_encode :: EncodeBuffer
114 latin1_checked_encode
115 input@Buffer{ bufRaw=iraw, bufL=ir0, bufR=iw, bufSize=_ }
116 output@Buffer{ bufRaw=oraw, bufL=_, bufR=ow0, bufSize=os }
118 done !ir !ow = return (if ir == iw then input{ bufL=0, bufR=0 }
119 else input{ bufL=ir },
122 | ow >= os || ir >= iw = done ir ow
124 (c,ir') <- readCharBuf iraw ir
125 if ord c > 0xff then invalid else do
126 writeWord8Buf oraw ow (fromIntegral (ord c))
129 invalid = if ir > ir0 then done ir ow else ioe_encodingError
133 ioe_encodingError :: IO a
134 ioe_encodingError = ioException
135 (IOError Nothing InvalidArgument "latin1_checked_encode"
136 "character is out of range for this encoding" Nothing Nothing)