on master help sign in
Simplify the unstream code
7366026Bryan O'Sullivan
Data/Text/Fusion.hs+5−7
@@ -95,13 +95,10 @@ reverseStream (Text arr off len) = Stream next (off+len-1) (maxSize len)
95
95
96-- | /O(n)/ Convert a 'Stream Char' into a 'Text'.
96-- | /O(n)/ Convert a 'Stream Char' into a 'Text'.
97unstream :: Stream Char -> Text
97unstream :: Stream Char -> Text
98−unstream (Stream next0 s0 len) = a
98+unstream (Stream next0 s0 len) = runText $ \done -> do
99− where
99+ let mlen = upperBound 4 len
100− a = runText (\done -> A.new mlen >>= \arr -> first done arr mlen s0 0)
100+ arr0 <- A.new mlen
101− where mlen = upperBound 4 len
102− first done = outer
103− where
104− outer arr top = loop
101+ let outer arr top = loop
105 where
102 where
106 loop !s !i =
103 loop !s !i =
107 case next0 s of
104 case next0 s of
@@ -117,6 +114,7 @@ unstream (Stream next0 s0 len) = a
117 loop s' (i+d)
114 loop s' (i+d)
118 where j | ord x < 0x10000 = i
115 where j | ord x < 0x10000 = i
119 | otherwise = i + 1
116 | otherwise = i + 1
117+ outer arr0 mlen s0 0
120{-# INLINE [0] unstream #-}
118{-# INLINE [0] unstream #-}
121{-# RULES "STREAM stream/unstream fusion" forall s. stream (unstream s) = s #-}
119{-# RULES "STREAM stream/unstream fusion" forall s. stream (unstream s) = s #-}
122
120