@@ -108,9 +108,10 @@ import qualified Prelude as P
108import Data.Int (Int64)
108import Data.Int (Int64)
109import Data.Text.Fusion.Internal
109import Data.Text.Fusion.Internal
110import Data.Text.Fusion.CaseMapping (foldMapping, lowerMapping, upperMapping)
110import Data.Text.Fusion.CaseMapping (foldMapping, lowerMapping, upperMapping)
111+import Data.Text.Fusion.Size
112singleton :: Char -> Stream Char
113singleton :: Char -> Stream Char
113−singleton c = Stream next False 1 -- HINT maybe too low
114+singleton c = Stream next False 1
114 where next False = Yield c True
115 where next False = Yield c True
115 next True = Done
116 next True = Done
116{-# INLINE singleton #-}
117{-# INLINE singleton #-}
@@ -118,10 +119,9 @@ singleton c = Stream next False 1 -- HINT maybe too low
118streamList :: [a] -> Stream a
119streamList :: [a] -> Stream a
119{-# INLINE [0] streamList #-}
120{-# INLINE [0] streamList #-}
120streamList [] = empty
121streamList [] = empty
121−streamList s = Stream next s unknownLength
122+streamList s = Stream next s unknownSize
122 where next [] = Done
123 where next [] = Done
123 next (x:xs) = Yield x xs
124 next (x:xs) = Yield x xs
124− unknownLength = 8 -- random HINT
126unstreamList :: Stream a -> [a]
126unstreamList :: Stream a -> [a]
127{-# INLINE [0] unstreamList #-}
127{-# INLINE [0] unstreamList #-}
@@ -138,7 +138,7 @@ unstreamList (Stream next s0 _len) = unfold s0
139-- | /O(n)/ Adds a character to the front of a Stream Char.
139-- | /O(n)/ Adds a character to the front of a Stream Char.
140cons :: Char -> Stream Char -> Stream Char
140cons :: Char -> Stream Char -> Stream Char
141−cons w (Stream next0 s0 len) = Stream next (S2 :!: s0) (len+2) -- HINT maybe too high
141+cons w (Stream next0 s0 len) = Stream next (S2 :!: s0) (len+1)
143 {-# INLINE next #-}
143 {-# INLINE next #-}
144 next (S2 :!: s) = Yield w (S1 :!: s)
144 next (S2 :!: s) = Yield w (S1 :!: s)
@@ -150,7 +150,7 @@ cons w (Stream next0 s0 len) = Stream next (S2 :!: s0) (len+2) -- HINT maybe too
151-- | /O(n)/ Adds a character to the end of a stream.
151-- | /O(n)/ Adds a character to the end of a stream.
152snoc :: Stream Char -> Char -> Stream Char
152snoc :: Stream Char -> Char -> Stream Char
153−snoc (Stream next0 xs0 len) w = Stream next (J xs0) (len+2) -- HINT maybe too high
153+snoc (Stream next0 xs0 len) w = Stream next (J xs0) (len+1)
155 {-# INLINE next #-}
155 {-# INLINE next #-}
156 next (J xs) = case next0 xs of
156 next (J xs) = case next0 xs of
@@ -193,7 +193,7 @@ uncons :: Stream Char -> Maybe (Char, Stream Char)
193uncons (Stream next s0 len) = loop_uncons s0
193uncons (Stream next s0 len) = loop_uncons s0
195 loop_uncons !s = case next s of
195 loop_uncons !s = case next s of
196− Yield x s1 -> Just (x, Stream next s1 (len-1)) -- HINT maybe too high
196+ Yield x s1 -> Just (x, Stream next s1 (len-1))
197 Skip s' -> loop_uncons s'
197 Skip s' -> loop_uncons s'
198 Done -> Nothing
198 Done -> Nothing
199{-# INLINE [0] uncons #-}
199{-# INLINE [0] uncons #-}
@@ -216,7 +216,7 @@ last (Stream next s0 _len) = loop0_last s0
216-- | /O(1)/ Returns all characters after the head of a Stream Char, which must
216-- | /O(1)/ Returns all characters after the head of a Stream Char, which must
217-- be non-empty.
217-- be non-empty.
218tail :: Stream Char -> Stream Char
218tail :: Stream Char -> Stream Char
219−tail (Stream next0 s0 len) = Stream next (False :!: s0) (len-1) -- HINT maybe too high
219+tail (Stream next0 s0 len) = Stream next (False :!: s0) (len-1)
221 {-# INLINE next #-}
221 {-# INLINE next #-}
222 next (False :!: s) = case next0 s of
222 next (False :!: s) = case next0 s of
@@ -233,7 +233,7 @@ tail (Stream next0 s0 len) = Stream next (False :!: s0) (len-1) -- HINT maybe to
233-- | /O(1)/ Returns all but the last character of a Stream Char, which
233-- | /O(1)/ Returns all but the last character of a Stream Char, which
234-- must be non-empty.
234-- must be non-empty.
235init :: Stream Char -> Stream Char
235init :: Stream Char -> Stream Char
236−init (Stream next0 s0 len) = Stream next (N :!: s0) (len-1) -- HINT maybe too high
236+init (Stream next0 s0 len) = Stream next (N :!: s0) (len-1)
238 {-# INLINE next #-}
238 {-# INLINE next #-}
239 next (N :!: s) = case next0 s of
239 next (N :!: s) = case next0 s of
@@ -269,10 +269,10 @@ lengthI (Stream next s0 _len) = loop_length 0 s0
269-- ----------------------------------------------------------------------------
269-- ----------------------------------------------------------------------------
270-- * Stream transformations
270-- * Stream transformations
272−-- | /O(n)/ 'map' @f @xs is the Stream Char obtained by applying @f@ to each element of
272+-- | /O(n)/ 'map' @f @xs is the Stream Char obtained by applying @f@
273−-- @xs@.
273+-- to each element of @xs@.
274map :: (Char -> Char) -> Stream Char -> Stream Char
274map :: (Char -> Char) -> Stream Char -> Stream Char
275−map f (Stream next0 s0 len) = Stream next s0 len -- HINT depends on f
275+map f (Stream next0 s0 len) = Stream next s0 len
277 {-# INLINE next #-}
277 {-# INLINE next #-}
278 next !s = case next0 s of
278 next !s = case next0 s of
@@ -289,7 +289,7 @@ map f (Stream next0 s0 len) = Stream next s0 len -- HINT depends on f
289-- | /O(n)/ Take a character and place it between each of the
289-- | /O(n)/ Take a character and place it between each of the
290-- characters of a 'Stream Char'.
290-- characters of a 'Stream Char'.
291intersperse :: Char -> Stream Char -> Stream Char
291intersperse :: Char -> Stream Char -> Stream Char
292−intersperse c (Stream next0 s0 len) = Stream next (s0 :!: N :!: S1) len -- HINT maybe too low
292+intersperse c (Stream next0 s0 len) = Stream next (s0 :!: N :!: S1) len
294 {-# INLINE next #-}
294 {-# INLINE next #-}
295 next (s :!: N :!: S1) = case next0 s of
295 next (s :!: N :!: S1) = case next0 s of
@@ -364,11 +364,9 @@ toLower = caseConvert lowerMapping
364{-# INLINE [0] toLower #-}
364{-# INLINE [0] toLower #-}
366justifyLeftI :: Integral a => a -> Char -> Stream Char -> Stream Char
366justifyLeftI :: Integral a => a -> Char -> Stream Char -> Stream Char
367−justifyLeftI k c (Stream next0 s0 len) = Stream next (s0 :!: S1 :!: 0) newLen
367+justifyLeftI k c (Stream next0 s0 len) =
368+ Stream next (s0 :!: S1 :!: 0) (larger (fromIntegral k) len)
370− newLen | j > len = j
372 next (s :!: S1 :!: n) =
370 next (s :!: S1 :!: n) =
373 case next0 s of
371 case next0 s of
374 Done -> next (s :!: S2 :!: n)
372 Done -> next (s :!: S2 :!: n)
@@ -590,7 +588,7 @@ replicateCharI n c
591replicateI :: Int64 -> Stream Char -> Stream Char
589replicateI :: Int64 -> Stream Char -> Stream Char
592replicateI n (Stream next0 s0 len) =
590replicateI n (Stream next0 s0 len) =
593− Stream next (0 :!: s0) (max 0 (fromIntegral n * len))
591+ Stream next (0 :!: s0) (fromIntegral (max 0 n) * len)
595 next (k :!: s)
593 next (k :!: s)
596 | k >= n = Done
594 | k >= n = Done
@@ -638,7 +636,7 @@ unfoldrNI n f s0 | n < 0 = empty
638-- length of the stream.
636-- length of the stream.
639take :: Integral a => a -> Stream Char -> Stream Char
637take :: Integral a => a -> Stream Char -> Stream Char
640take n0 (Stream next0 s0 len) =
638take n0 (Stream next0 s0 len) =
641− Stream next (n0 :!: s0) (max 0 (len - fromIntegral n0)) -- HINT maybe too high
639+ Stream next (n0 :!: s0) (smaller len (fromIntegral (max 0 n0)))
643 {-# INLINE next #-}
641 {-# INLINE next #-}
644 next (n :!: s) | n <= 0 = Done
642 next (n :!: s) | n <= 0 = Done
@@ -653,11 +651,11 @@ take n0 (Stream next0 s0 len) =
653-- length of the stream.
651-- length of the stream.
654drop :: Integral a => a -> Stream Char -> Stream Char
652drop :: Integral a => a -> Stream Char -> Stream Char
655drop n0 (Stream next0 s0 len) =
653drop n0 (Stream next0 s0 len) =
656− Stream next (J (max 0 n0) :!: s0) (len - fromIntegral n0) -- HINT maybe too high
654+ Stream next (J n0 :!: s0) (len - fromIntegral (max 0 n0))
658 {-# INLINE next #-}
656 {-# INLINE next #-}
659 next (J n :!: s)
657 next (J n :!: s)
660− | n == 0 = Skip (N :!: s)
658+ | n <= 0 = Skip (N :!: s)
661 | otherwise = case next0 s of
659 | otherwise = case next0 s of
662 Done -> Done
660 Done -> Done
663 Skip s' -> Skip (J n :!: s')
661 Skip s' -> Skip (J n :!: s')
@@ -806,7 +804,8 @@ findIndicesI p (Stream next s0 _len) = loop_findIndex 0 s0
806-- | zipWith generalises 'zip' by zipping with the function given as
804-- | zipWith generalises 'zip' by zipping with the function given as
807-- the first argument, instead of a tupling function.
805-- the first argument, instead of a tupling function.
808zipWith :: (a -> a -> b) -> Stream a -> Stream a -> Stream b
806zipWith :: (a -> a -> b) -> Stream a -> Stream a -> Stream b
809−zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) = Stream next (sa0 :!: sb0 :!: N) (min len1 len2)
807+zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) =
808+ Stream next (sa0 :!: sb0 :!: N) (smaller len1 len2)
811 {-# INLINE next #-}
810 {-# INLINE next #-}
812 next (sa :!: sb :!: N) = case next0 sa of
811 next (sa :!: sb :!: N) = case next0 sa of