@@ -174,7 +174,7 @@ import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
174 Eq(..), Ord(..), Ordering(..), (++),
174 Eq(..), Ord(..), Ordering(..), (++),
175 Read(..), Show(..),
175 Read(..), Show(..),
176 (&&), (||), (+), (-), (.), ($), ($!), (>>), (*),
176 (&&), (||), (+), (-), (.), ($), ($!), (>>), (*),
177− div, error, not, return, otherwise)
177+ div, maxBound, not, return, otherwise)
178#if defined(HAVE_DEEPSEQ)
178#if defined(HAVE_DEEPSEQ)
179import Control.DeepSeq (NFData)
179import Control.DeepSeq (NFData)
@@ -294,8 +294,8 @@ instance NFData Text
295instance Data Text where
295instance Data Text where
296 gfoldl f z txt = z pack `f` (unpack txt)
296 gfoldl f z txt = z pack `f` (unpack txt)
297− toConstr _ = error "Data.Text.Text.toConstr"
297+ toConstr _ = P.error "Data.Text.Text.toConstr"
298− gunfold _ _ = error "Data.Text.Text.gunfold"
298+ gunfold _ _ = P.error "Data.Text.Text.gunfold"
299#if __GLASGOW_HASKELL__ >= 612
299#if __GLASGOW_HASKELL__ >= 612
300 dataTypeOf _ = mkNoRepType "Data.Text.Text"
300 dataTypeOf _ = mkNoRepType "Data.Text.Text"
@@ -340,7 +340,11 @@ snoc t c = unstream (S.snoc (stream t) c)
340-- | /O(n)/ Appends one 'Text' to the other by copying both of them
340-- | /O(n)/ Appends one 'Text' to the other by copying both of them
341-- into a new 'Text'. Subject to fusion.
341-- into a new 'Text'. Subject to fusion.
342append :: Text -> Text -> Text
342append :: Text -> Text -> Text
343−append (Text arr1 off1 len1) (Text arr2 off2 len2) = Text (A.run x) 0 len
343+append a@(Text arr1 off1 len1) b@(Text arr2 off2 len2)
346+ | len > 0 = Text (A.run x) 0 len
347+ | otherwise = overflowError "append"
345 len = len1+len2
349 len = len1+len2
@@ -706,7 +710,7 @@ concat ts = case ts' of
706 _ -> Text (A.run go) 0 len
710 _ -> Text (A.run go) 0 len
708 ts' = L.filter (not . null) ts
712 ts' = L.filter (not . null) ts
709− len = L.sum $ L.map lengthWord16 ts'
713+ len = sumP "concat" $ L.map lengthWord16 ts'
711 arr <- A.new len
715 arr <- A.new len
712 let step i (Text a o l) =
716 let step i (Text a o l) =
@@ -804,12 +808,11 @@ mapAccumR f z0 = second reverse . S.mapAccumL f z0 . reverseStream
804-- @t@ repeated @n@ times.
808-- @t@ repeated @n@ times.
805replicate :: Int -> Text -> Text
809replicate :: Int -> Text -> Text
806replicate n t@(Text a o l)
810replicate n t@(Text a o l)
807− | n <= 0 || l <= 0 = empty
811+ | n <= 0 || l <= 0 = empty
808− | n == 1 = t
812+ | n == 1 = t
809− | isSingleton t = replicateChar n (unsafeHead t)
813+ | isSingleton t = replicateChar n (unsafeHead t)
810− | len < n = error $ "Data.Text.replicate: invalid length " ++
811− show n -- multiplication overflow
812− | otherwise = Text (A.run x) 0 len
814+ | n <= maxBound `div` l = Text (A.run x) 0 len
815+ | otherwise = overflowError "replicate"
814 len = l * n
817 len = l * n
@@ -1440,5 +1443,17 @@ stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1440 | p `isSuffixOf` t = Just $! textP arr off (len-plen)
1443 | p `isSuffixOf` t = Just $! textP arr off (len-plen)
1441 | otherwise = Nothing
1444 | otherwise = Nothing
1446+-- | Add a list of non-negative numbers. Errors out on overflow.
1447+sumP :: String -> [Int] -> Int
1450+ | ax >= 0 = go ax xs
1451+ | otherwise = overflowError fun
1443emptyError :: String -> a
1455emptyError :: String -> a
1444−emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")
1456+emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
1458+overflowError :: String -> a
1459+overflowError fun = P.error $ "Data.Text." ++ fun ++ ": size overflow"