on master help sign in
Speed up the hopefully common case of a big enough chunk.

--HG-- extra : convert_revision : 7ea67c327929b282df0795baced4b1b08be84b99

commit47f25e10d872235df7a02e79daf38ad251014bfb authorBryan O'Sullivan <bos@serpentine.com> dateWed 3 Jun 2009, 06:04 (17 years ago) parent593485a
1 file changed+17−2
Data/Text/Lazy/Encoding/Fusion.hs+17−2
Data/Text/Lazy/Encoding/Fusion.hs+17−2
@@ -51,9 +51,24 @@ unknownLength = 4
5151-- | /O(n)/ Convert a lazy 'ByteString' into a 'Stream Char', using
5252-- UTF-8 encoding.
5353streamUtf8 :: ByteString -> Stream Char
54−streamUtf8 bs0 = Stream next (bs0 :!: S N N N N :!: 0) unknownLength
54+streamUtf8 bs0 = Stream next (bs0 :!: empty :!: 0) unknownLength
5555 where
56+ empty = S N N N N
5657 {-# INLINE next #-}
58+ next (bs@(Chunk ps _) :!: S N _ _ _ :!: i)
59+ | i < len && U8.validate1 a =
60+ Yield (unsafeChr8 a) (bs :!: empty :!: i+1)
61+ | i + 1 < len && U8.validate2 a b =
62+ Yield (U8.chr2 a b) (bs :!: empty :!: i+2)
63+ | i + 2 < len && U8.validate3 a b c =
64+ Yield (U8.chr3 a b c) (bs :!: empty :!: i+3)
65+ | i + 4 < len && U8.validate4 a b c d =
66+ Yield (U8.chr4 a b c d) (bs :!: empty :!: i+4)
67+ where len = B.length ps
68+ a = B.unsafeIndex ps i
69+ b = B.unsafeIndex ps (i+1)
70+ c = B.unsafeIndex ps (i+2)
71+ d = B.unsafeIndex ps (i+3)
5772 next st@(bs :!: s :!: i) =
5873 case s of
5974 S (J a) N _ _ | U8.validate1 a ->
@@ -65,7 +80,7 @@ streamUtf8 bs0 = Stream next (bs0 :!: S N N N N :!: 0) unknownLength
6580 S (J a) (J b) (J c) (J d) | U8.validate4 a b c d ->
6681 Yield (U8.chr4 a b c d) es
6782 _ -> consume st
68− where es = bs :!: S N N N N :!: i
83+ where es = bs :!: empty :!: i
6984 {-# INLINE consume #-}
7085 consume (bs@(Chunk ps rest) :!: s :!: i)
7186 | i >= len = consume (rest :!: s :!: 0)