From 2a4b0fc0f6db6c2455bbf9c9ef1d2ebc2f80889e Mon Sep 17 00:00:00 2001 From: Ignat Insarov Date: Sat, 13 Apr 2024 16:03:27 +0700 Subject: [PATCH] Make sure `stimes` works right in corner cases. --- src/Data/Text.hs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Data/Text.hs b/src/Data/Text.hs index eeb40ec2..7175250c 100644 --- a/src/Data/Text.hs +++ b/src/Data/Text.hs @@ -261,7 +261,8 @@ import Data.Word (Word8) import Foreign.C.Types import GHC.Base (eqInt, neInt, gtInt, geInt, ltInt, leInt) import qualified GHC.Exts as Exts -import GHC.Int (Int8) +import GHC.Int (Int8, Int (I#)) +import GHC.Num (Integer(IS, IP, IN)) import GHC.Stack (HasCallStack) import qualified Language.Haskell.TH.Lib as TH import qualified Language.Haskell.TH.Syntax as TH @@ -361,7 +362,14 @@ instance Read Text where -- | @since 1.2.2.0 instance Semigroup Text where (<>) = append - stimes = replicate . P.fromIntegral + + -- | Beware: this function will evaluate to error if the given number does + -- not fit into an @Int@. + stimes howManyTimes = case P.toInteger howManyTimes of + IS howManyTimesInt# -> replicate (I# howManyTimesInt#) + IP _ -> P.error "`Data.Text.stimes`: given number does not fit into an `Int`!" + IN _ -> P.const empty + sconcat = concat . NonEmptyList.toList instance Monoid Text where