@@ -103,8 +103,8 @@ module Data.Text.Fusion
105import Prelude (Bool(..), Char, Either(..), Eq(..), Maybe(..), Monad(..),
105import Prelude (Bool(..), Char, Either(..), Eq(..), Maybe(..), Monad(..),
106− Num(..), Ord(..), String, ($), (++), (.), (&&), error,
106+ Num(..), Ord(..), String, ($), (++), (.), (&&),
107− fromIntegral, fst, otherwise, snd)
107+ fromIntegral, otherwise)
108import Control.Monad (liftM2)
108import Control.Monad (liftM2)
109import Control.Monad.ST (runST)
109import Control.Monad.ST (runST)
110import qualified Data.List as L
110import qualified Data.List as L
@@ -116,6 +116,7 @@ import Data.Text.UnsafeChar (unsafeChr, unsafeWrite, unsafeWriteRev)
116import qualified Data.Text.Array as A
116import qualified Data.Text.Array as A
117import qualified Data.Text.Internal as I
117import qualified Data.Text.Internal as I
118import qualified Data.Text.Utf16 as U16
118import qualified Data.Text.Utf16 as U16
119+import qualified Prelude as P
120default(Int)
121default(Int)
@@ -164,7 +165,7 @@ reverseStream (Text arr off len) = Stream next (off+len-1) len
164unstream :: Stream Char -> Text
165unstream :: Stream Char -> Text
165unstream (Stream next0 s0 len)
166unstream (Stream next0 s0 len)
166 | len == 0 = I.empty
167 | len == 0 = I.empty
167− | otherwise = Text (fst a) 0 (snd a)
168+ | otherwise = Text (P.fst a) 0 (P.snd a)
169 a = runST (A.unsafeNew len >>= (\arr -> loop arr len s0 0))
170 a = runST (A.unsafeNew len >>= (\arr -> loop arr len s0 0))
170 loop arr !top !s !i
171 loop arr !top !s !i
@@ -200,8 +201,14 @@ eq (Stream next1 s1 _) (Stream next2 s2 _) = cmp (next1 s1) (next2 s2)
200 cmp (next1 s1') (next2 s2')
201 cmp (next1 s1') (next2 s2')
201{-# SPECIALISE eq :: Stream Char -> Stream Char -> Bool #-}
202{-# SPECIALISE eq :: Stream Char -> Stream Char -> Bool #-}
204+streamError :: String -> String -> a
205+streamError func msg = P.error $ "Data.Text.Fusion." ++ func ++ ": " ++ msg
203internalError :: String -> a
207internalError :: String -> a
204−internalError func = error $ "Data.Text.Fusion." ++ func ++ ": internal error"
208+internalError func = streamError func "Internal error"
210+emptyError :: String -> a
211+emptyError func = internalError func "Empty input"
206-- ----------------------------------------------------------------------------
213-- ----------------------------------------------------------------------------
207-- * Basic stream functions
214-- * Basic stream functions
@@ -254,7 +261,7 @@ head (Stream next s0 _len) = loop_head s0
254 loop_head !s = case next s of
261 loop_head !s = case next s of
255 Yield x _ -> x
262 Yield x _ -> x
256 Skip s' -> loop_head s'
263 Skip s' -> loop_head s'
257− Done -> error "head: Empty list"
264+ Done -> streamError "head" "Empty stream"
258{-# INLINE [0] head #-}
265{-# INLINE [0] head #-}
260-- | /O(1)/ Returns the first character and remainder of a 'Stream
267-- | /O(1)/ Returns the first character and remainder of a 'Stream
@@ -274,7 +281,7 @@ last :: Stream Char -> Char
274last (Stream next s0 _len) = loop0_last s0
281last (Stream next s0 _len) = loop0_last s0
276 loop0_last !s = case next s of
283 loop0_last !s = case next s of
277− Done -> error "last: Empty list"
284+ Done -> emptyError "last"
278 Skip s' -> loop0_last s'
285 Skip s' -> loop0_last s'
279 Yield x s' -> loop_last x s'
286 Yield x s' -> loop_last x s'
280 loop_last !x !s = case next s of
287 loop_last !x !s = case next s of
@@ -290,7 +297,7 @@ tail (Stream next0 s0 len) = Stream next (False :!: s0) (len-1)
291 {-# INLINE next #-}
298 {-# INLINE next #-}
292 next (False :!: s) = case next0 s of
299 next (False :!: s) = case next0 s of
293− Done -> error "tail"
300+ Done -> emptyError "tail"
294 Skip s' -> Skip (False :!: s')
301 Skip s' -> Skip (False :!: s')
295 Yield _ s' -> Skip (True :!: s')
302 Yield _ s' -> Skip (True :!: s')
296 next (True :!: s) = case next0 s of
303 next (True :!: s) = case next0 s of
@@ -307,7 +314,7 @@ init (Stream next0 s0 len) = Stream next (Nothing :!: s0) (len-1)
308 {-# INLINE next #-}
315 {-# INLINE next #-}
309 next (Nothing :!: s) = case next0 s of
316 next (Nothing :!: s) = case next0 s of
310− Done -> errorEmptyList "init"
317+ Done -> emptyError "init"
311 Skip s' -> Skip (Nothing :!: s')
318 Skip s' -> Skip (Nothing :!: s')
312 Yield x s' -> Skip (Just x :!: s')
319 Yield x s' -> Skip (Just x :!: s')
313 next (Just x :!: s) = case next0 s of
320 next (Just x :!: s) = case next0 s of
@@ -443,7 +450,7 @@ foldl1 f (Stream next s0 _len) = loop0_foldl1 s0
443 loop0_foldl1 !s = case next s of
450 loop0_foldl1 !s = case next s of
444 Skip s' -> loop0_foldl1 s'
451 Skip s' -> loop0_foldl1 s'
445 Yield x s' -> loop_foldl1 x s'
452 Yield x s' -> loop_foldl1 x s'
446− Done -> errorEmptyList "foldl1"
453+ Done -> emptyError "foldl1"
447 loop_foldl1 z !s = case next s of
454 loop_foldl1 z !s = case next s of
448 Done -> z
455 Done -> z
449 Skip s' -> loop_foldl1 z s'
456 Skip s' -> loop_foldl1 z s'
@@ -457,7 +464,7 @@ foldl1' f (Stream next s0 _len) = loop0_foldl1' s0
457 loop0_foldl1' !s = case next s of
464 loop0_foldl1' !s = case next s of
458 Skip s' -> loop0_foldl1' s'
465 Skip s' -> loop0_foldl1' s'
459 Yield x s' -> loop_foldl1' x s'
466 Yield x s' -> loop_foldl1' x s'
460− Done -> errorEmptyList "foldl1"
467+ Done -> emptyError "foldl1"
461 loop_foldl1' !z !s = case next s of
468 loop_foldl1' !z !s = case next s of
462 Done -> z
469 Done -> z
463 Skip s' -> loop_foldl1' z s'
470 Skip s' -> loop_foldl1' z s'
@@ -483,7 +490,7 @@ foldr1 :: (Char -> Char -> Char) -> Stream Char -> Char
483foldr1 f (Stream next s0 _len) = loop0_foldr1 s0
490foldr1 f (Stream next s0 _len) = loop0_foldr1 s0
485 loop0_foldr1 !s = case next s of
492 loop0_foldr1 !s = case next s of
486− Done -> error "foldr1"
493+ Done -> emptyError "foldr1"
487 Skip s' -> loop0_foldr1 s'
494 Skip s' -> loop0_foldr1 s'
488 Yield x s' -> loop_foldr1 x s'
495 Yield x s' -> loop_foldr1 x s'
@@ -542,7 +549,7 @@ maximum :: Stream Char -> Char
542maximum (Stream next0 s0 _len) = loop0_maximum s0
549maximum (Stream next0 s0 _len) = loop0_maximum s0
544 loop0_maximum !s = case next0 s of
551 loop0_maximum !s = case next0 s of
545− Done -> errorEmptyList "maximum"
552+ Done -> emptyError "maximum"
546 Skip s' -> loop0_maximum s'
553 Skip s' -> loop0_maximum s'
547 Yield x s' -> loop_maximum x s'
554 Yield x s' -> loop_maximum x s'
548 loop_maximum !z !s = case next0 s of
555 loop_maximum !z !s = case next0 s of
@@ -559,7 +566,7 @@ minimum :: Stream Char -> Char
559minimum (Stream next0 s0 _len) = loop0_minimum s0
566minimum (Stream next0 s0 _len) = loop0_minimum s0
561 loop0_minimum !s = case next0 s of
568 loop0_minimum !s = case next0 s of
562− Done -> errorEmptyList "minimum"
569+ Done -> emptyError "minimum"
563 Skip s' -> loop0_minimum s'
570 Skip s' -> loop0_minimum s'
564 Yield x s' -> loop_minimum x s'
571 Yield x s' -> loop_minimum x s'
565 loop_minimum !z !s = case next0 s of
572 loop_minimum !z !s = case next0 s of
@@ -800,11 +807,11 @@ filter p (Stream next0 s0 len) = Stream next s0 len
800-- | /O(1)/ stream index (subscript) operator, starting from 0.
807-- | /O(1)/ stream index (subscript) operator, starting from 0.
801index :: Stream Char -> Int -> Char
808index :: Stream Char -> Int -> Char
802index (Stream next s0 _len) n0
809index (Stream next s0 _len) n0
803− | n0 < 0 = error "Stream.(!!): negative index"
810+ | n0 < 0 = streamError "index" "Negative index"
804 | otherwise = loop_index n0 s0
811 | otherwise = loop_index n0 s0
806 loop_index !n !s = case next s of
813 loop_index !n !s = case next s of
807− Done -> error "Stream.(!!): index too large"
814+ Done -> streamError "index" "Index too large"
808 Skip s' -> loop_index n s'
815 Skip s' -> loop_index n s'
809 Yield x s' | n == 0 -> x
816 Yield x s' | n == 0 -> x
810 | otherwise -> loop_index (n-1) s'
817 | otherwise -> loop_index (n-1) s'
@@ -897,7 +904,3 @@ zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) = Stream next (sa0 :!:
897 Skip sb' -> Skip (sa' :!: sb' :!: Just a)
904 Skip sb' -> Skip (sa' :!: sb' :!: Just a)
898 Yield b sb' -> Yield (f a b) (sa' :!: sb' :!: Nothing)
905 Yield b sb' -> Yield (f a b) (sa' :!: sb' :!: Nothing)
899{-# INLINE [0] zipWith #-}
906{-# INLINE [0] zipWith #-}
902−errorEmptyList :: String -> a
903−errorEmptyList fun = error ("Data.Text.Fusion." ++ fun ++ ": empty list")