import GHC.Base
import GHC.Real
import GHC.Num
-import GHC.IO
+-- import GHC.IO
import GHC.IO.Exception
import GHC.IO.Buffer
import GHC.IO.Encoding.Types
utf32 = TextEncoding { mkTextDecoder = utf32_DF,
mkTextEncoder = utf32_EF }
-utf32_DF :: IO TextDecoder
+utf32_DF :: IO (TextDecoder (Maybe DecodeBuffer))
utf32_DF = do
seen_bom <- newIORef Nothing
- return (BufferCodec (utf32_decode seen_bom) (return ()))
-
-utf32_EF :: IO TextEncoder
+ return (BufferCodec {
+ encode = utf32_decode seen_bom,
+ close = return (),
+ getState = readIORef seen_bom,
+ setState = writeIORef seen_bom
+ })
+
+utf32_EF :: IO (TextEncoder Bool)
utf32_EF = do
done_bom <- newIORef False
- return (BufferCodec (utf32_encode done_bom) (return ()))
+ return (BufferCodec {
+ encode = utf32_encode done_bom,
+ close = return (),
+ getState = readIORef done_bom,
+ setState = writeIORef done_bom
+ })
utf32_encode :: IORef Bool -> EncodeBuffer
utf32_encode done_bom input
utf32be = TextEncoding { mkTextDecoder = utf32be_DF,
mkTextEncoder = utf32be_EF }
-utf32be_DF :: IO TextDecoder
-utf32be_DF = return (BufferCodec utf32be_decode (return ()))
-
-utf32be_EF :: IO TextEncoder
-utf32be_EF = return (BufferCodec utf32be_encode (return ()))
+utf32be_DF :: IO (TextDecoder ())
+utf32be_DF =
+ return (BufferCodec {
+ encode = utf32be_decode,
+ close = return (),
+ getState = return (),
+ setState = const $ return ()
+ })
+
+utf32be_EF :: IO (TextEncoder ())
+utf32be_EF =
+ return (BufferCodec {
+ encode = utf32be_encode,
+ close = return (),
+ getState = return (),
+ setState = const $ return ()
+ })
utf32le :: TextEncoding
utf32le = TextEncoding { mkTextDecoder = utf32le_DF,
mkTextEncoder = utf32le_EF }
-utf32le_DF :: IO TextDecoder
-utf32le_DF = return (BufferCodec utf32le_decode (return ()))
-
-utf32le_EF :: IO TextEncoder
-utf32le_EF = return (BufferCodec utf32le_encode (return ()))
-
+utf32le_DF :: IO (TextDecoder ())
+utf32le_DF =
+ return (BufferCodec {
+ encode = utf32le_decode,
+ close = return (),
+ getState = return (),
+ setState = const $ return ()
+ })
+
+utf32le_EF :: IO (TextEncoder ())
+utf32le_EF =
+ return (BufferCodec {
+ encode = utf32le_encode,
+ close = return (),
+ getState = return (),
+ setState = const $ return ()
+ })
utf32be_decode :: DecodeBuffer
c3 <- readWord8Buf iraw (ir+3)
let x1 = chr4 c0 c1 c2 c3
if not (validate x1) then invalid else do
- writeCharBuf oraw ow x1
- loop (ir+4) (ow+1)
+ ow' <- writeCharBuf oraw ow x1
+ loop (ir+4) ow'
where
invalid = if ir > ir0 then done ir ow else ioe_decodingError
c3 <- readWord8Buf iraw (ir+3)
let x1 = chr4 c3 c2 c1 c0
if not (validate x1) then invalid else do
- writeCharBuf oraw ow x1
- loop (ir+4) (ow+1)
+ ow' <- writeCharBuf oraw ow x1
+ loop (ir+4) ow'
where
invalid = if ir > ir0 then done ir ow else ioe_decodingError