@@ -23,6 +23,7 @@ module Data.Text.Fusion
2323 -- * Creation and elimination
2424 , stream
2525 , unstream
26+ , empty
2627
2728 -- * Basic interface
2829 , cons
@@ -99,9 +100,10 @@ import Data.Bits (shiftR, (.&.))
99100import qualified Data.List as L
100101import Data.Word (Word16)
101102import GHC.Exts (Int(..), (+#))
102−import Data.Text.Internal (Text(..), empty)
103+import Data.Text.Internal (Text(..))
103104import Data.Text.UnsafeChar (unsafeChr)
104105import qualified Data.Text.Array as A
106+import qualified Data.Text.Internal as I
105107import qualified Data.Text.Utf16 as U16
106108
107109default(Int)
@@ -134,7 +136,7 @@ stream (Text arr off len) = Stream next off len
134136
135137-- | /O(n)/ Convert a Stream Char into a Text.
136138unstream :: Stream Char -> Text
137−unstream (Stream _next0 _s0 0) = empty
139+unstream (Stream _next0 _s0 0) = I.empty
138140unstream (Stream next0 s0 len) = Text (fst a) 0 (snd a)
139141 where
140142 a = runST (A.unsafeNew len >>= (\arr -> loop arr 0 len s0))
@@ -163,6 +165,11 @@ unstream (Stream next0 s0 len) = Text (fst a) 0 (snd a)
163165{-# INLINE [0] unstream #-}
164166{-# RULES "STREAM stream/unstream fusion" forall s. stream (unstream s) = s #-}
165167
168+-- | The empty stream.
169+empty :: Stream Char
170+empty = Stream next 0 0
171+ where next _ = Done
172+{-# INLINE [0] empty #-}
166173
167174copy :: A.MArray s Word16 -> A.MArray s Word16 -> ST s ()
168175copy src dest = copy_loop 0
@@ -445,7 +452,7 @@ foldr f z (Stream next s0 _len) = loop_foldr s0
445452{-# INLINE [0] foldr #-}
446453
447454-- | foldr1 is a variant of 'foldr' that has no starting value argument,
448−-- and thust must be applied to non-empty streams.
455+-- and thus must be applied to non-empty streams.
449456-- Subject to array fusion.
450457foldr1 :: (Char -> Char -> Char) -> Stream Char -> Char
451458foldr1 f (Stream next s0 _len) = loop0_foldr1 s0
@@ -478,7 +485,7 @@ concat = L.foldr append (Stream next Done 0)
478485-- | Map a function over a stream that results in a stream and concatenate the
479486-- results.
480487concatMap :: (Char -> Stream Char) -> Stream Char -> Stream Char
481−concatMap f = foldr (append . f) (stream empty)
488+concatMap f = foldr (append . f) empty
482489
483490-- | /O(n)/ any @p @xs determines if any character in the stream
484491-- @xs@ satisifes the predicate @p@.
@@ -556,7 +563,9 @@ scanl f z0 (Stream next0 s0 len) = Stream next (S1 :!: z0 :!: s0) (len+1)
556563-- ** Generating and unfolding streams
557564
558565replicate :: Int -> Char -> Stream Char
559−replicate n c = Stream next 0 n
566+replicate n c
567+ | n < 0 = empty
568+ | otherwise = Stream next 0 n
560569 where
561570 {-# INLINE next #-}
562571 next i | i >= n = Done