on master help sign in
Tidy up error cases.

--HG-- extra : convert_revision : 11de5cbf0f2a1407e66f7545d6499319fa86a6f0

commit9d0cf7950a5a52ebeb2d13c402193cc2f1ac0a99 authorBryan O'Sullivan <bos@serpentine.com> dateSun 22 Feb 2009, 23:19 (18 years ago) parent044dff4
2 files changed+28−25
Data/Text.hs+6−6 Data/Text/Fusion.hs+22−19
Data/Text.hs+6−6
@@ -143,7 +143,7 @@ module Data.Text
143143 ) where
144144
145145import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
146− Eq, (==), (++), error,
146+ Eq, (==), (++),
147147 Read(..), Show(..),
148148 (&&), (||), (+), (-), (<), (>), (<=), (>=), (.), ($),
149149 not, return, otherwise)
@@ -297,7 +297,7 @@ second f (a, b) = (a, f b)
297297-- non-empty. Subject to array fusion.
298298last :: Text -> Char
299299last (Text arr off len)
300− | len <= 0 = errorEmptyList "last"
300+ | len <= 0 = emptyError "last"
301301 | n < 0xDC00 || n > 0xDFFF = unsafeChr n
302302 | otherwise = U16.chr2 n0 n
303303 where n = A.unsafeIndex arr (off+len-1)
@@ -322,7 +322,7 @@ textP arr off len | len == 0 = empty
322322-- must be non-empty. Subject to array fusion.
323323tail :: Text -> Text
324324tail t@(Text arr off len)
325− | len <= 0 = errorEmptyList "tail"
325+ | len <= 0 = emptyError "tail"
326326 | otherwise = textP arr (off+d) (len-d)
327327 where d = iter_ t 0
328328{-# INLINE [1] tail #-}
@@ -337,7 +337,7 @@ tail t@(Text arr off len)
337337-- | /O(1)/ Returns all but the last character of a 'Text', which must
338338-- be non-empty. Subject to array fusion.
339339init :: Text -> Text
340−init (Text arr off len) | len <= 0 = errorEmptyList "init"
340+init (Text arr off len) | len <= 0 = emptyError "init"
341341 | n >= 0xDC00 && n <= 0xDFFF = textP arr off (len-2)
342342 | otherwise = textP arr off (len-1)
343343 where
@@ -957,5 +957,5 @@ isInfixOf :: Text -> Text -> Bool
957957isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
958958{-# INLINE isInfixOf #-}
959959
960−errorEmptyList :: String -> a
961−errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")
960+emptyError :: String -> a
961+emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")
Data/Text/Fusion.hs+22−19
@@ -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")