@@ -56,7 +56,7 @@ import qualified Data.Text.Utf16 as U16
5656import qualified Data.Text.Utf32 as U32
5757import qualified Data.Text.Utf8 as U8
5858
59−data T4 a b c d = T4 !a !b !c !d
59+data T4 a b c d = T4 {-# UNPACK #-} !a {-# UNPACK #-} !b {-# UNPACK #-} !c {-# UNPACK #-} !d
6060
6161streamASCII :: ByteString -> Stream Char
6262streamASCII bs = Stream next 0 l
@@ -169,36 +169,32 @@ restreamASCII (Stream next0 s0 len) = Stream next s0 (len*2)
169169 where x' = fromIntegral (ord x) :: Word8
170170{-# INLINE restreamASCII #-}
171171
172+data M = N | J {-# UNPACK #-} !Word8
173+ deriving (Eq, Ord, Show)
174+
172175-- | /O(n)/ Convert a Stream Char into a UTF-8 encoded Stream Word8.
173176restreamUtf8 :: Stream Char -> Stream Word8
174177restreamUtf8 (Stream next0 s0 len) =
175− Stream next (T4 (Just s0) Nothing Nothing Nothing) (len*2)
178+ Stream next (T4 s0 N N N) (len*2)
176179 where
177180 {-# INLINE next #-}
178− next (T4 (Just s) Nothing Nothing Nothing) = case next0 s of
181+ next (T4 s N N N) = case next0 s of
179182 Done -> Done
180− Skip s' -> Skip (T4 (Just s') Nothing Nothing Nothing)
183+ Skip s' -> Skip (T4 s' N N N)
181184 Yield x xs
182− | n <= 0x7F -> Yield c (T4 (Just xs) Nothing Nothing Nothing)
183− | n <= 0x07FF -> Yield (fst c2) (T4 (Just xs) (Just $ snd c2) Nothing Nothing)
184− | n <= 0xFFFF -> Yield (fst3 c3) (T4 (Just xs) (Just $ snd3 c3) (Just $ trd3 c3) Nothing)
185− | otherwise -> Yield (fst4 c4) (T4 (Just xs) (Just $ snd4 c4) (Just $ trd4 c4) (Just $ fth4 c4))
185+ | n <= 0x7F -> Yield c (T4 xs N N N)
186+ | n <= 0x07FF -> Yield a2 (T4 xs (J b2) N N)
187+ | n <= 0xFFFF -> Yield a3 (T4 xs (J b3) (J c3) N)
188+ | otherwise -> Yield a4 (T4 xs (J b4) (J c4) (J d4))
186189 where
187190 n = ord x
188191 c = fromIntegral n
189− c2 = U8.ord2 x
190− c3 = U8.ord3 x
191− c4 = U8.ord4 x
192− fst3 (x1,_,_) = x1
193− snd3 (_,x2,_) = x2
194− trd3 (_,_,x3) = x3
195− fst4 (x1,_,_,_) = x1
196− snd4 (_,x2,_,_) = x2
197− trd4 (_,_,x3,_) = x3
198− fth4 (_,_,_,x4) = x4
199− next (T4 (Just s) (Just x2) Nothing Nothing) = Yield x2 (T4 (Just s) Nothing Nothing Nothing)
200− next (T4 (Just s) (Just x2) x3 Nothing) = Yield x2 (T4 (Just s) x3 Nothing Nothing)
201− next (T4 (Just s) (Just x2) x3 x4) = Yield x2 (T4 (Just s) x3 x4 Nothing)
192+ (a2,b2) = U8.ord2 x
193+ (a3,b3,c3) = U8.ord3 x
194+ (a4,b4,c4,d4) = U8.ord4 x
195+ next (T4 s (J x2) N N) = Yield x2 (T4 s N N N)
196+ next (T4 s (J x2) x3 N) = Yield x2 (T4 s x3 N N)
197+ next (T4 s (J x2) x3 x4) = Yield x2 (T4 s x3 x4 N)
202198 next _ = internalError "restreamUtf8"
203199{-# INLINE restreamUtf8 #-}
204200