on master help sign in
Speed up the hopefully common case of a big enough chunk.
47f25e1Bryan O'Sullivan
Data/Text/Lazy/Encoding/Fusion.hs+17−2
@@ -51,9 +51,24 @@ unknownLength = 4
51-- | /O(n)/ Convert a lazy 'ByteString' into a 'Stream Char', using
51-- | /O(n)/ Convert a lazy 'ByteString' into a 'Stream Char', using
52-- UTF-8 encoding.
52-- UTF-8 encoding.
53streamUtf8 :: ByteString -> Stream Char
53streamUtf8 :: 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
55 where
55 where
56+ empty = S N N N N
56 {-# INLINE next #-}
57 {-# 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)
57 next st@(bs :!: s :!: i) =
72 next st@(bs :!: s :!: i) =
58 case s of
73 case s of
59 S (J a) N _ _ | U8.validate1 a ->
74 S (J a) N _ _ | U8.validate1 a ->
@@ -65,7 +80,7 @@ streamUtf8 bs0 = Stream next (bs0 :!: S N N N N :!: 0) unknownLength
65 S (J a) (J b) (J c) (J d) | U8.validate4 a b c d ->
80 S (J a) (J b) (J c) (J d) | U8.validate4 a b c d ->
66 Yield (U8.chr4 a b c d) es
81 Yield (U8.chr4 a b c d) es
67 _ -> consume st
82 _ -> consume st
68− where es = bs :!: S N N N N :!: i
83+ where es = bs :!: empty :!: i
69 {-# INLINE consume #-}
84 {-# INLINE consume #-}
70 consume (bs@(Chunk ps rest) :!: s :!: i)
85 consume (bs@(Chunk ps rest) :!: s :!: i)
71 | i >= len = consume (rest :!: s :!: 0)
86 | i >= len = consume (rest :!: s :!: 0)