@@ -103,8 +103,8 @@ module Data.Text.Fusion
103103 ) where
104104
105105import Prelude (Bool(..), Char, Either(..), Eq(..), Maybe(..), Monad(..),
106− Num(..), Ord(..), String, ($), (++), (.), (&&), error,
107− fromIntegral, fst, otherwise, snd)
106+ Num(..), Ord(..), String, ($), (++), (.), (&&),
107+ fromIntegral, otherwise)
108108import Control.Monad (liftM2)
109109import Control.Monad.ST (runST)
110110import qualified Data.List as L
@@ -116,6 +116,7 @@ import Data.Text.UnsafeChar (unsafeChr, unsafeWrite, unsafeWriteRev)
116116import qualified Data.Text.Array as A
117117import qualified Data.Text.Internal as I
118118import qualified Data.Text.Utf16 as U16
119+import qualified Prelude as P
119120
120121default(Int)
121122
@@ -164,7 +165,7 @@ reverseStream (Text arr off len) = Stream next (off+len-1) len
164165unstream :: Stream Char -> Text
165166unstream (Stream next0 s0 len)
166167 | len == 0 = I.empty
167− | otherwise = Text (fst a) 0 (snd a)
168+ | otherwise = Text (P.fst a) 0 (P.snd a)
168169 where
169170 a = runST (A.unsafeNew len >>= (\arr -> loop arr len s0 0))
170171 loop arr !top !s !i
@@ -200,8 +201,14 @@ eq (Stream next1 s1 _) (Stream next2 s2 _) = cmp (next1 s1) (next2 s2)
200201 cmp (next1 s1') (next2 s2')
201202{-# SPECIALISE eq :: Stream Char -> Stream Char -> Bool #-}
202203
204+streamError :: String -> String -> a
205+streamError func msg = P.error $ "Data.Text.Fusion." ++ func ++ ": " ++ msg
206+
203207internalError :: String -> a
204−internalError func = error $ "Data.Text.Fusion." ++ func ++ ": internal error"
208+internalError func = streamError func "Internal error"
209+
210+emptyError :: String -> a
211+emptyError func = internalError func "Empty input"
205212
206213-- ----------------------------------------------------------------------------
207214-- * Basic stream functions
@@ -254,7 +261,7 @@ head (Stream next s0 _len) = loop_head s0
254261 loop_head !s = case next s of
255262 Yield x _ -> x
256263 Skip s' -> loop_head s'
257− Done -> error "head: Empty list"
264+ Done -> streamError "head" "Empty stream"
258265{-# INLINE [0] head #-}
259266
260267-- | /O(1)/ Returns the first character and remainder of a 'Stream
@@ -274,7 +281,7 @@ last :: Stream Char -> Char
274281last (Stream next s0 _len) = loop0_last s0
275282 where
276283 loop0_last !s = case next s of
277− Done -> error "last: Empty list"
284+ Done -> emptyError "last"
278285 Skip s' -> loop0_last s'
279286 Yield x s' -> loop_last x s'
280287 loop_last !x !s = case next s of
@@ -290,7 +297,7 @@ tail (Stream next0 s0 len) = Stream next (False :!: s0) (len-1)
290297 where
291298 {-# INLINE next #-}
292299 next (False :!: s) = case next0 s of
293− Done -> error "tail"
300+ Done -> emptyError "tail"
294301 Skip s' -> Skip (False :!: s')
295302 Yield _ s' -> Skip (True :!: s')
296303 next (True :!: s) = case next0 s of
@@ -307,7 +314,7 @@ init (Stream next0 s0 len) = Stream next (Nothing :!: s0) (len-1)
307314 where
308315 {-# INLINE next #-}
309316 next (Nothing :!: s) = case next0 s of
310− Done -> errorEmptyList "init"
317+ Done -> emptyError "init"
311318 Skip s' -> Skip (Nothing :!: s')
312319 Yield x s' -> Skip (Just x :!: s')
313320 next (Just x :!: s) = case next0 s of
@@ -443,7 +450,7 @@ foldl1 f (Stream next s0 _len) = loop0_foldl1 s0
443450 loop0_foldl1 !s = case next s of
444451 Skip s' -> loop0_foldl1 s'
445452 Yield x s' -> loop_foldl1 x s'
446− Done -> errorEmptyList "foldl1"
453+ Done -> emptyError "foldl1"
447454 loop_foldl1 z !s = case next s of
448455 Done -> z
449456 Skip s' -> loop_foldl1 z s'
@@ -457,7 +464,7 @@ foldl1' f (Stream next s0 _len) = loop0_foldl1' s0
457464 loop0_foldl1' !s = case next s of
458465 Skip s' -> loop0_foldl1' s'
459466 Yield x s' -> loop_foldl1' x s'
460− Done -> errorEmptyList "foldl1"
467+ Done -> emptyError "foldl1"
461468 loop_foldl1' !z !s = case next s of
462469 Done -> z
463470 Skip s' -> loop_foldl1' z s'
@@ -483,7 +490,7 @@ foldr1 :: (Char -> Char -> Char) -> Stream Char -> Char
483490foldr1 f (Stream next s0 _len) = loop0_foldr1 s0
484491 where
485492 loop0_foldr1 !s = case next s of
486− Done -> error "foldr1"
493+ Done -> emptyError "foldr1"
487494 Skip s' -> loop0_foldr1 s'
488495 Yield x s' -> loop_foldr1 x s'
489496
@@ -542,7 +549,7 @@ maximum :: Stream Char -> Char
542549maximum (Stream next0 s0 _len) = loop0_maximum s0
543550 where
544551 loop0_maximum !s = case next0 s of
545− Done -> errorEmptyList "maximum"
552+ Done -> emptyError "maximum"
546553 Skip s' -> loop0_maximum s'
547554 Yield x s' -> loop_maximum x s'
548555 loop_maximum !z !s = case next0 s of
@@ -559,7 +566,7 @@ minimum :: Stream Char -> Char
559566minimum (Stream next0 s0 _len) = loop0_minimum s0
560567 where
561568 loop0_minimum !s = case next0 s of
562− Done -> errorEmptyList "minimum"
569+ Done -> emptyError "minimum"
563570 Skip s' -> loop0_minimum s'
564571 Yield x s' -> loop_minimum x s'
565572 loop_minimum !z !s = case next0 s of
@@ -800,11 +807,11 @@ filter p (Stream next0 s0 len) = Stream next s0 len
800807-- | /O(1)/ stream index (subscript) operator, starting from 0.
801808index :: Stream Char -> Int -> Char
802809index (Stream next s0 _len) n0
803− | n0 < 0 = error "Stream.(!!): negative index"
810+ | n0 < 0 = streamError "index" "Negative index"
804811 | otherwise = loop_index n0 s0
805812 where
806813 loop_index !n !s = case next s of
807− Done -> error "Stream.(!!): index too large"
814+ Done -> streamError "index" "Index too large"
808815 Skip s' -> loop_index n s'
809816 Yield x s' | n == 0 -> x
810817 | otherwise -> loop_index (n-1) s'
@@ -897,7 +904,3 @@ zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) = Stream next (sa0 :!:
897904 Skip sb' -> Skip (sa' :!: sb' :!: Just a)
898905 Yield b sb' -> Yield (f a b) (sa' :!: sb' :!: Nothing)
899906{-# INLINE [0] zipWith #-}
900−
901−
902−errorEmptyList :: String -> a
903−errorEmptyList fun = error ("Data.Text.Fusion." ++ fun ++ ": empty list")