Commits

Bryan O'Sullivan committed aed9292

Tidy up error cases.

Comments (0)

Files changed (2)

     ) where
 
 import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
-                Eq, (==), (++), error,
+                Eq, (==), (++),
                 Read(..), Show(..),
                 (&&), (||), (+), (-), (<), (>), (<=), (>=), (.), ($),
                 not, return, otherwise)
 -- non-empty.  Subject to array fusion.
 last :: Text -> Char
 last (Text arr off len)
-    | len <= 0                 = errorEmptyList "last"
+    | len <= 0                 = emptyError "last"
     | n < 0xDC00 || n > 0xDFFF = unsafeChr n
     | otherwise                = U16.chr2 n0 n
     where n  = A.unsafeIndex arr (off+len-1)
 -- must be non-empty.  Subject to array fusion.
 tail :: Text -> Text
 tail t@(Text arr off len)
-    | len <= 0  = errorEmptyList "tail"
+    | len <= 0  = emptyError "tail"
     | otherwise = textP arr (off+d) (len-d)
     where d = iter_ t 0
 {-# INLINE [1] tail #-}
 -- | /O(1)/ Returns all but the last character of a 'Text', which must
 -- be non-empty.  Subject to array fusion.
 init :: Text -> Text
-init (Text arr off len) | len <= 0                   = errorEmptyList "init"
+init (Text arr off len) | len <= 0                   = emptyError "init"
                         | n >= 0xDC00 && n <= 0xDFFF = textP arr off (len-2)
                         | otherwise                  = textP arr off (len-1)
     where
 isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
 {-# INLINE isInfixOf #-}
 
-errorEmptyList :: String -> a
-errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")
+emptyError :: String -> a
+emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")

Data/Text/Fusion.hs

     ) where
 
 import Prelude (Bool(..), Char, Either(..), Eq(..), Maybe(..), Monad(..),
-                Num(..), Ord(..), String, ($), (++), (.), (&&), error,
-                fromIntegral, fst, otherwise, snd)
+                Num(..), Ord(..), String, ($), (++), (.), (&&),
+                fromIntegral, otherwise)
 import Control.Monad (liftM2)
 import Control.Monad.ST (runST)
 import qualified Data.List as L
 import qualified Data.Text.Array as A
 import qualified Data.Text.Internal as I
 import qualified Data.Text.Utf16 as U16
+import qualified Prelude as P
 
 default(Int)
 
 unstream :: Stream Char -> Text
 unstream (Stream next0 s0 len)
     | len == 0 = I.empty
-    | otherwise = Text (fst a) 0 (snd a)
+    | otherwise = Text (P.fst a) 0 (P.snd a)
     where
       a = runST (A.unsafeNew len >>= (\arr -> loop arr len s0 0))
       loop arr !top !s !i
                                           cmp (next1 s1') (next2 s2')
 {-# SPECIALISE eq :: Stream Char -> Stream Char -> Bool #-}
 
+streamError :: String -> String -> a
+streamError func msg = P.error $ "Data.Text.Fusion." ++ func ++ ": " ++ msg
+
 internalError :: String -> a
-internalError func = error $ "Data.Text.Fusion." ++ func ++ ": internal error"
+internalError func = streamError func "Internal error"
+
+emptyError :: String -> a
+emptyError func = internalError func "Empty input"
 
 -- ----------------------------------------------------------------------------
 -- * Basic stream functions
       loop_head !s = case next s of
                       Yield x _ -> x
                       Skip s' -> loop_head s'
-                      Done -> error "head: Empty list"
+                      Done -> streamError "head" "Empty stream"
 {-# INLINE [0] head #-}
 
 -- | /O(1)/ Returns the first character and remainder of a 'Stream
 last (Stream next s0 _len) = loop0_last s0
     where
       loop0_last !s = case next s of
-                        Done       -> error "last: Empty list"
+                        Done       -> emptyError "last"
                         Skip s'    -> loop0_last  s'
                         Yield x s' -> loop_last x s'
       loop_last !x !s = case next s of
     where
       {-# INLINE next #-}
       next (False :!: s) = case next0 s of
-                          Done -> error "tail"
+                          Done -> emptyError "tail"
                           Skip s' -> Skip (False :!: s')
                           Yield _ s' -> Skip (True :!: s')
       next (True :!: s) = case next0 s of
     where
       {-# INLINE next #-}
       next (Nothing :!: s) = case next0 s of
-                               Done       -> errorEmptyList "init"
+                               Done       -> emptyError "init"
                                Skip s'    -> Skip (Nothing :!: s')
                                Yield x s' -> Skip (Just x  :!: s')
       next (Just x :!: s)  = case next0 s of
       loop0_foldl1 !s = case next s of
                           Skip s' -> loop0_foldl1 s'
                           Yield x s' -> loop_foldl1 x s'
-                          Done -> errorEmptyList "foldl1"
+                          Done -> emptyError "foldl1"
       loop_foldl1 z !s = case next s of
                            Done -> z
                            Skip s' -> loop_foldl1 z s'
       loop0_foldl1' !s = case next s of
                            Skip s' -> loop0_foldl1' s'
                            Yield x s' -> loop_foldl1' x s'
-                           Done -> errorEmptyList "foldl1"
+                           Done -> emptyError "foldl1"
       loop_foldl1' !z !s = case next s of
                              Done -> z
                              Skip s' -> loop_foldl1' z s'
 foldr1 f (Stream next s0 _len) = loop0_foldr1 s0
   where
     loop0_foldr1 !s = case next s of
-      Done       -> error "foldr1"
+      Done       -> emptyError "foldr1"
       Skip    s' -> loop0_foldr1  s'
       Yield x s' -> loop_foldr1 x s'
 
 maximum (Stream next0 s0 _len) = loop0_maximum s0
     where
       loop0_maximum !s   = case next0 s of
-                             Done       -> errorEmptyList "maximum"
+                             Done       -> emptyError "maximum"
                              Skip s'    -> loop0_maximum s'
                              Yield x s' -> loop_maximum x s'
       loop_maximum !z !s = case next0 s of
 minimum (Stream next0 s0 _len) = loop0_minimum s0
     where
       loop0_minimum !s   = case next0 s of
-                             Done       -> errorEmptyList "minimum"
+                             Done       -> emptyError "minimum"
                              Skip s'    -> loop0_minimum s'
                              Yield x s' -> loop_minimum x s'
       loop_minimum !z !s = case next0 s of
 -- | /O(1)/ stream index (subscript) operator, starting from 0.
 index :: Stream Char -> Int -> Char
 index (Stream next s0 _len) n0
-  | n0 < 0    = error "Stream.(!!): negative index"
+  | n0 < 0    = streamError "index" "Negative index"
   | otherwise = loop_index n0 s0
   where
     loop_index !n !s = case next s of
-      Done                   -> error "Stream.(!!): index too large"
+      Done                   -> streamError "index" "Index too large"
       Skip    s'             -> loop_index  n    s'
       Yield x s' | n == 0    -> x
                  | otherwise -> loop_index (n-1) s'
                                        Skip sb' -> Skip (sa' :!: sb' :!: Just a)
                                        Yield b sb' -> Yield (f a b) (sa' :!: sb' :!: Nothing)
 {-# INLINE [0] zipWith #-}
-
-
-errorEmptyList :: String -> a
-errorEmptyList fun = error ("Data.Text.Fusion." ++ fun ++ ": empty list")