Commits

Bryan O'Sullivan  committed 09e8479

Fix yet another QC-exposed bug

  • Participants
  • Parent commits 5b8f27c

Comments (0)

Files changed (1)

File Data/Text/Fusion.hs

     -- * Creation and elimination
     , stream
     , unstream
+    , empty
 
     -- * Basic interface
     , cons
 import qualified Data.List as L
 import Data.Word (Word16)
 import GHC.Exts (Int(..), (+#))
-import Data.Text.Internal (Text(..), empty)
+import Data.Text.Internal (Text(..))
 import Data.Text.UnsafeChar (unsafeChr)
 import qualified Data.Text.Array as A
+import qualified Data.Text.Internal as I
 import qualified Data.Text.Utf16 as U16
 
 default(Int)
 
 -- | /O(n)/ Convert a Stream Char into a Text.
 unstream :: Stream Char -> Text
-unstream (Stream _next0 _s0 0) = empty
+unstream (Stream _next0 _s0 0) = I.empty
 unstream (Stream next0 s0 len) = Text (fst a) 0 (snd a)
     where
       a = runST (A.unsafeNew len >>= (\arr -> loop arr 0 len s0))
 {-# INLINE [0] unstream #-}
 {-# RULES "STREAM stream/unstream fusion" forall s. stream (unstream s) = s #-}
 
+-- | The empty stream.
+empty :: Stream Char
+empty = Stream next 0 0
+    where next _ = Done
+{-# INLINE [0] empty #-}
 
 copy :: A.MArray s Word16 -> A.MArray s Word16 -> ST s ()
 copy src dest = copy_loop 0
 {-# INLINE [0] foldr #-}
 
 -- | foldr1 is a variant of 'foldr' that has no starting value argument,
--- and thust must be applied to non-empty streams.
+-- and thus must be applied to non-empty streams.
 -- Subject to array fusion.
 foldr1 :: (Char -> Char -> Char) -> Stream Char -> Char
 foldr1 f (Stream next s0 _len) = loop0_foldr1 s0
 -- | Map a function over a stream that results in a stream and concatenate the
 -- results.
 concatMap :: (Char -> Stream Char) -> Stream Char -> Stream Char
-concatMap f = foldr (append . f) (stream empty)
+concatMap f = foldr (append . f) empty
 
 -- | /O(n)/ any @p @xs determines if any character in the stream
 -- @xs@ satisifes the predicate @p@.
 -- ** Generating and unfolding streams
 
 replicate :: Int -> Char -> Stream Char
-replicate n c = Stream next 0 n
+replicate n c
+    | n < 0     = empty
+    | otherwise = Stream next 0 n
   where
     {-# INLINE next #-}
     next i | i >= n    = Done