on master help sign in
Make incremental decoding types clearer
55724afBryan O'Sullivan
Data/Text/Encoding.hs+9−9
@@ -1,4 +1,4 @@
1−{-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, MagicHash,
1+{-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, GeneralizedNewtypeDeriving, MagicHash,
2 UnliftedFFITypes #-}
2 UnliftedFFITypes #-}
3#if __GLASGOW_HASKELL__ >= 702
3#if __GLASGOW_HASKELL__ >= 702
4{-# LANGUAGE Trustworthy #-}
4{-# LANGUAGE Trustworthy #-}
@@ -75,7 +75,7 @@ import Foreign.C.Types (CSize)
75import Foreign.ForeignPtr (withForeignPtr)
75import Foreign.ForeignPtr (withForeignPtr)
76import Foreign.Marshal.Utils (with)
76import Foreign.Marshal.Utils (with)
77import Foreign.Ptr (Ptr, minusPtr, plusPtr)
77import Foreign.Ptr (Ptr, minusPtr, plusPtr)
78−import Foreign.Storable (peek, poke)
78+import Foreign.Storable (Storable, peek, poke)
79import GHC.Base (MutableByteArray#)
79import GHC.Base (MutableByteArray#)
80import qualified Data.Text.Array as A
80import qualified Data.Text.Array as A
81import qualified Data.Text.Encoding.Fusion as E
81import qualified Data.Text.Encoding.Fusion as E
@@ -148,16 +148,16 @@ decodeUtf8With onErr (PS fp off len) = runText $ \done -> do
148
148
149data Decoder = Some !Text (ByteString -> Decoder)
149data Decoder = Some !Text (ByteString -> Decoder)
150
150
151−-- | (codepoint, state)
151+newtype CodePoint = CodePoint Word32 deriving (Num, Storable)
152−type DecoderState = (Word32, Word32)
152+newtype DecoderState = DecoderState Word32 deriving (Eq, Num, Storable)
153
153
154decodeUtf8With' :: OnDecodeError -> ByteString -> Decoder
154decodeUtf8With' :: OnDecodeError -> ByteString -> Decoder
155−decodeUtf8With' onErr = decodeChunk (0,0)
155+decodeUtf8With' onErr = decodeChunk 0 0
156 where
156 where
157 -- We create a slightly larger than necessary buffer to accomodate a
157 -- We create a slightly larger than necessary buffer to accomodate a
158 -- potential surrogate pair started in the last buffer
158 -- potential surrogate pair started in the last buffer
159− decodeChunk :: DecoderState -> ByteString -> Decoder
159+ decodeChunk :: CodePoint -> DecoderState -> ByteString -> Decoder
160− decodeChunk (codepoint0,state0) (PS fp off len) =
160+ decodeChunk codepoint0 state0 (PS fp off len) =
161 runST $ (unsafeIOToST . decodeChunkToBuffer) =<< A.new (len+1)
161 runST $ (unsafeIOToST . decodeChunkToBuffer) =<< A.new (len+1)
162 where
162 where
163 decodeChunkToBuffer :: A.MArray s -> IO Decoder
163 decodeChunkToBuffer :: A.MArray s -> IO Decoder
@@ -191,7 +191,7 @@ decodeUtf8With' onErr = decodeChunk (0,0)
191 chunkText <- unsafeSTToIO $ do
191 chunkText <- unsafeSTToIO $ do
192 arr <- A.unsafeFreeze dest
192 arr <- A.unsafeFreeze dest
193 return $! textP arr 0 (fromIntegral n)
193 return $! textP arr 0 (fromIntegral n)
194− return $ Some chunkText $ decodeChunk (codepoint, state)
194+ return $ Some chunkText $ decodeChunk codepoint state
195 in loop (ptr `plusPtr` off)
195 in loop (ptr `plusPtr` off)
196 desc = "Data.Text.Encoding.decodeUtf8With': Invalid UTF-8 stream"
196 desc = "Data.Text.Encoding.decodeUtf8With': Invalid UTF-8 stream"
197{- INLINE[0] decodeUtf8With' #-}
197{- INLINE[0] decodeUtf8With' #-}
@@ -356,7 +356,7 @@ foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
356foreign import ccall unsafe "_hs_text_decode_utf8_state" c_decode_utf8_with_state
356foreign import ccall unsafe "_hs_text_decode_utf8_state" c_decode_utf8_with_state
357 :: MutableByteArray# s -> Ptr CSize
357 :: MutableByteArray# s -> Ptr CSize
358 -> Ptr Word8 -> Ptr Word8
358 -> Ptr Word8 -> Ptr Word8
359− -> Ptr Word32 -> Ptr Word32 -> IO (Ptr Word8)
359+ -> Ptr CodePoint -> Ptr DecoderState -> IO (Ptr Word8)
360
360
361foreign import ccall unsafe "_hs_text_decode_latin1" c_decode_latin1
361foreign import ccall unsafe "_hs_text_decode_latin1" c_decode_latin1
362 :: MutableByteArray# s -> Ptr Word8 -> Ptr Word8 -> IO ()
362 :: MutableByteArray# s -> Ptr Word8 -> Ptr Word8 -> IO ()