1 {-# LANGUAGE NoImplicitPrelude
3 , NondecreasingIndentation
5 {-# OPTIONS_GHC -funbox-strict-fields #-}
7 -----------------------------------------------------------------------------
9 -- Module : GHC.IO.Encoding.Latin1
10 -- Copyright : (c) The University of Glasgow, 2009
11 -- License : see libraries/base/LICENSE
13 -- Maintainer : libraries@haskell.org
14 -- Stability : internal
15 -- Portability : non-portable
17 -- UTF-32 Codecs for the IO library
19 -- Portions Copyright : (c) Tom Harper 2008-2009,
20 -- (c) Bryan O'Sullivan 2009,
21 -- (c) Duncan Coutts 2009
23 -----------------------------------------------------------------------------
25 module GHC.IO.Encoding.Latin1 (
27 latin1_checked, mkLatin1_checked,
30 latin1_checked_encode,
38 import GHC.IO.Encoding.Failure
39 import GHC.IO.Encoding.Types
41 -- -----------------------------------------------------------------------------
44 latin1 :: TextEncoding
45 latin1 = mkLatin1 ErrorOnCodingFailure
47 mkLatin1 :: CodingFailureMode -> TextEncoding
48 mkLatin1 cfm = TextEncoding { textEncodingName = "ISO8859-1",
49 mkTextDecoder = latin1_DF cfm,
50 mkTextEncoder = latin1_EF cfm }
52 latin1_DF :: CodingFailureMode -> IO (TextDecoder ())
55 encode = latin1_decode,
56 recover = recoverDecode cfm,
59 setState = const $ return ()
62 latin1_EF :: CodingFailureMode -> IO (TextEncoder ())
65 encode = latin1_encode,
66 recover = recoverEncode cfm,
69 setState = const $ return ()
72 latin1_checked :: TextEncoding
73 latin1_checked = mkLatin1_checked ErrorOnCodingFailure
75 mkLatin1_checked :: CodingFailureMode -> TextEncoding
76 mkLatin1_checked cfm = TextEncoding { textEncodingName = "ISO8859-1(checked)",
77 mkTextDecoder = latin1_DF cfm,
78 mkTextEncoder = latin1_checked_EF cfm }
80 latin1_checked_EF :: CodingFailureMode -> IO (TextEncoder ())
81 latin1_checked_EF cfm =
83 encode = latin1_checked_encode,
84 recover = recoverEncode cfm,
87 setState = const $ return ()
91 latin1_decode :: DecodeBuffer
93 input@Buffer{ bufRaw=iraw, bufL=ir0, bufR=iw, bufSize=_ }
94 output@Buffer{ bufRaw=oraw, bufL=_, bufR=ow0, bufSize=os }
97 | ow >= os = done OutputUnderflow ir ow
98 | ir >= iw = done InputUnderflow ir ow
100 c0 <- readWord8Buf iraw ir
101 ow' <- writeCharBuf oraw ow (unsafeChr (fromIntegral c0))
104 -- lambda-lifted, to avoid thunks being built in the inner-loop:
105 done why !ir !ow = return (why,
106 if ir == iw then input{ bufL=0, bufR=0 }
107 else input{ bufL=ir },
112 latin1_encode :: EncodeBuffer
114 input@Buffer{ bufRaw=iraw, bufL=ir0, bufR=iw, bufSize=_ }
115 output@Buffer{ bufRaw=oraw, bufL=_, bufR=ow0, bufSize=os }
117 done why !ir !ow = return (why,
118 if ir == iw then input{ bufL=0, bufR=0 }
119 else input{ bufL=ir },
122 | ow >= os = done OutputUnderflow ir ow
123 | ir >= iw = done InputUnderflow ir ow
125 (c,ir') <- readCharBuf iraw ir
126 writeWord8Buf oraw ow (fromIntegral (ord c))
131 latin1_checked_encode :: EncodeBuffer
132 latin1_checked_encode
133 input@Buffer{ bufRaw=iraw, bufL=ir0, bufR=iw, bufSize=_ }
134 output@Buffer{ bufRaw=oraw, bufL=_, bufR=ow0, bufSize=os }
136 done why !ir !ow = return (why,
137 if ir == iw then input{ bufL=0, bufR=0 }
138 else input{ bufL=ir },
141 | ow >= os = done OutputUnderflow ir ow
142 | ir >= iw = done InputUnderflow ir ow
144 (c,ir') <- readCharBuf iraw ir
145 if ord c > 0xff then invalid else do
146 writeWord8Buf oraw ow (fromIntegral (ord c))
149 invalid = done InvalidSequence ir ow