@@ -129,7 +129,6 @@ module Data.Text
130 , dropEnd
130 , dropEnd
131 , takeWhile
131 , takeWhile
133 , dropWhile
132 , dropWhile
134 , dropWhileEnd
133 , dropWhileEnd
135 , dropAround
134 , dropAround
@@ -192,12 +191,11 @@ module Data.Text
193 -- * Low level operations
192 -- * Low level operations
198import Prelude (Char, Bool(..), Int, Maybe(..), String,
196import Prelude (Char, Bool(..), Int, Maybe(..), String,
199 Eq(..), Ord(..), Ordering(..), (++),
197 Eq(..), Ord(..), Ordering(..), (++),
200− Read(..),
198+ Read(..), Show(..),
201 (&&), (||), (+), (-), (.), ($), ($!), (>>),
199 (&&), (||), (+), (-), (.), ($), ($!), (>>),
202 not, return, otherwise, quot)
200 not, return, otherwise, quot)
203#if defined(HAVE_DEEPSEQ)
201#if defined(HAVE_DEEPSEQ)
@@ -213,23 +211,17 @@ import Control.Monad (foldM)
213import Control.Monad.ST (ST)
211import Control.Monad.ST (ST)
214import qualified Data.Text.Array as A
212import qualified Data.Text.Array as A
215import qualified Data.List as L
213import qualified Data.List as L
216−import Data.Binary (Binary(get, put))
217import Data.Monoid (Monoid(..))
214import Data.Monoid (Monoid(..))
218−#if MIN_VERSION_base(4,9,0)
219−import Data.Semigroup (Semigroup(..))
221import Data.String (IsString(..))
215import Data.String (IsString(..))
222import qualified Data.Text.Internal.Fusion as S
216import qualified Data.Text.Internal.Fusion as S
223import qualified Data.Text.Internal.Fusion.Common as S
217import qualified Data.Text.Internal.Fusion.Common as S
224−import Data.Text.Encoding (decodeUtf8', encodeUtf8)
225import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
218import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
226import Data.Text.Internal.Private (span_)
219import Data.Text.Internal.Private (span_)
227−import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
220+import Data.Text.Internal (Text(..), empty, empty_, firstf, mul, safe, text)
228−import Data.Text.Show (singleton, unpack, unpackCString#)
229import qualified Prelude as P
221import qualified Prelude as P
230import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
222import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
231 reverseIter_, unsafeHead, unsafeTail)
223 reverseIter_, unsafeHead, unsafeTail)
232−import Data.Text.Internal.Unsafe.Char (unsafeChr)
224+import Data.Text.Internal.Unsafe.Char (unsafeChr, unsafeWrite)
233import qualified Data.Text.Internal.Functions as F
225import qualified Data.Text.Internal.Functions as F
234import qualified Data.Text.Internal.Encoding.Utf16 as U16
226import qualified Data.Text.Internal.Encoding.Utf16 as U16
235import Data.Text.Internal.Search (indices)
227import Data.Text.Internal.Search (indices)
@@ -238,13 +230,15 @@ import Data.ByteString (ByteString)
238import qualified Data.Text.Lazy as L
230import qualified Data.Text.Lazy as L
239import Data.Int (Int64)
231import Data.Int (Int64)
241−import GHC.Base (eqInt, neInt, gtInt, geInt, ltInt, leInt)
233+#if __GLASGOW_HASKELL__ >= 702
234+import qualified GHC.CString as GHC
236+import qualified GHC.Base as GHC
242#if __GLASGOW_HASKELL__ >= 708
238#if __GLASGOW_HASKELL__ >= 708
243import qualified GHC.Exts as Exts
239import qualified GHC.Exts as Exts
245−#if MIN_VERSION_base(4,7,0)
241+import GHC.Prim (Addr#)
246−import Text.Printf (PrintfArg, formatArg, formatString)
249-- $strict
243-- $strict
@@ -330,24 +324,15 @@ instance Eq Text where
330instance Ord Text where
324instance Ord Text where
331 compare = compareText
325 compare = compareText
327+instance Show Text where
328+ showsPrec p ps r = showsPrec p (unpack ps) r
333instance Read Text where
330instance Read Text where
334 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
331 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
336−#if MIN_VERSION_base(4,9,0)
337−-- Semigroup orphan instances for older GHCs are provided by
338−-- 'semigroups` package
340−instance Semigroup Text where
344instance Monoid Text where
333instance Monoid Text where
345 mempty = empty
334 mempty = empty
346−#if MIN_VERSION_base(4,9,0)
347− mappend = (<>) -- future-proof definition
349 mappend = append
335 mappend = append
351 mconcat = concat
336 mconcat = concat
353instance IsString Text where
338instance IsString Text where
@@ -364,14 +349,6 @@ instance Exts.IsList Text where
364instance NFData Text where rnf !_ = ()
349instance NFData Text where rnf !_ = ()
367−instance Binary Text where
368− put t = put (encodeUtf8 t)
371− case decodeUtf8' bs of
372− P.Left exn -> P.fail (P.show exn)
373− P.Right a -> P.return a
375-- | This instance preserves data abstraction at the cost of inefficiency.
352-- | This instance preserves data abstraction at the cost of inefficiency.
376-- We omit reflection services for the sake of data abstraction.
353-- We omit reflection services for the sake of data abstraction.
@@ -395,12 +372,6 @@ instance Data Text where
395 _ -> P.error "gunfold"
372 _ -> P.error "gunfold"
396 dataTypeOf _ = textDataType
373 dataTypeOf _ = textDataType
398−#if MIN_VERSION_base(4,7,0)
399−-- | Only defined for @base-4.7.0.0@ and later
400−instance PrintfArg Text where
401− formatArg txt = formatString $ unpack txt
404packConstr :: Constr
375packConstr :: Constr
405packConstr = mkConstr textDataType "pack" [] Prefix
376packConstr = mkConstr textDataType "pack" [] Prefix
@@ -430,6 +401,54 @@ pack :: String -> Text
430pack = unstream . S.map safe . S.streamList
401pack = unstream . S.map safe . S.streamList
431{-# INLINE [1] pack #-}
402{-# INLINE [1] pack #-}
404+-- | /O(n)/ Convert a 'Text' into a 'String'. Subject to fusion.
405+unpack :: Text -> String
406+unpack = S.unstreamList . stream
407+{-# INLINE [1] unpack #-}
409+-- | /O(n)/ Convert a literal string into a Text. Subject to fusion.
410+unpackCString# :: Addr# -> Text
411+unpackCString# addr# = unstream (S.streamCString# addr#)
412+{-# NOINLINE unpackCString# #-}
414+{-# RULES "TEXT literal" forall a.
415+ unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
416+ = unpackCString# a #-}
418+{-# RULES "TEXT literal UTF8" forall a.
419+ unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
420+ = unpackCString# a #-}
422+{-# RULES "TEXT empty literal"
423+ unstream (S.map safe (S.streamList []))
426+{-# RULES "TEXT singleton literal" forall a.
427+ unstream (S.map safe (S.streamList [a]))
430+-- | /O(1)/ Convert a character into a Text. Subject to fusion.
431+-- Performs replacement on invalid scalar values.
432+singleton :: Char -> Text
433+singleton = unstream . S.singleton . safe
434+{-# INLINE [1] singleton #-}
436+{-# RULES "TEXT singleton" forall a.
437+ unstream (S.singleton (safe a))
440+-- This is intended to reduce inlining bloat.
441+singleton_ :: Char -> Text
442+singleton_ c = Text (A.run x) 0 len
443+ where x :: ST s (A.MArray s)
444+ x = do arr <- A.new len
445+ _ <- unsafeWrite arr 0 d
447+ len | d < '\x10000' = 1
450+{-# NOINLINE singleton_ #-}
433-- -----------------------------------------------------------------------------
452-- -----------------------------------------------------------------------------
434-- * Basic functions
453-- * Basic functions
@@ -486,8 +505,8 @@ head t = S.head (stream t)
486uncons :: Text -> Maybe (Char, Text)
505uncons :: Text -> Maybe (Char, Text)
487uncons t@(Text arr off len)
506uncons t@(Text arr off len)
488 | len <= 0 = Nothing
507 | len <= 0 = Nothing
489− | otherwise = Just $ let !(Iter c d) = iter t 0
490− in (c, text arr (off+d) (len-d))
508+ | otherwise = Just (c, text arr (off+d) (len-d))
509+ where Iter c d = iter t 0
491{-# INLINE [1] uncons #-}
510{-# INLINE [1] uncons #-}
493-- | Lifted from Control.Arrow and specialized.
512-- | Lifted from Control.Arrow and specialized.
@@ -572,9 +591,7 @@ isSingleton = S.isSingleton . stream
572-- Subject to fusion.
591-- Subject to fusion.
573length :: Text -> Int
592length :: Text -> Int
574length t = S.length (stream t)
593length t = S.length (stream t)
575−{-# INLINE [0] length #-}
594+{-# INLINE length #-}
576−-- length needs to be phased after the compareN/length rules otherwise
577−-- it may inline before the rules have an opportunity to fire.
579-- | /O(n)/ Compare the count of characters in a 'Text' to a number.
596-- | /O(n)/ Compare the count of characters in a 'Text' to a number.
580-- Subject to fusion.
597-- Subject to fusion.
@@ -593,32 +610,32 @@ compareLength t n = S.compareLengthI (stream t) n
595"TEXT ==N/length -> compareLength/==EQ" [~1] forall t n.
612"TEXT ==N/length -> compareLength/==EQ" [~1] forall t n.
596− eqInt (length t) n = compareLength t n == EQ
613+ (==) (length t) n = compareLength t n == EQ
600"TEXT /=N/length -> compareLength//=EQ" [~1] forall t n.
617"TEXT /=N/length -> compareLength//=EQ" [~1] forall t n.
601− neInt (length t) n = compareLength t n /= EQ
618+ (/=) (length t) n = compareLength t n /= EQ
605"TEXT <N/length -> compareLength/==LT" [~1] forall t n.
622"TEXT <N/length -> compareLength/==LT" [~1] forall t n.
606− ltInt (length t) n = compareLength t n == LT
623+ (<) (length t) n = compareLength t n == LT
610"TEXT <=N/length -> compareLength//=GT" [~1] forall t n.
627"TEXT <=N/length -> compareLength//=GT" [~1] forall t n.
611− leInt (length t) n = compareLength t n /= GT
628+ (<=) (length t) n = compareLength t n /= GT
615"TEXT >N/length -> compareLength/==GT" [~1] forall t n.
632"TEXT >N/length -> compareLength/==GT" [~1] forall t n.
616− gtInt (length t) n = compareLength t n == GT
633+ (>) (length t) n = compareLength t n == GT
620"TEXT >=N/length -> compareLength//=LT" [~1] forall t n.
637"TEXT >=N/length -> compareLength//=LT" [~1] forall t n.
621− geInt (length t) n = compareLength t n /= LT
638+ (>=) (length t) n = compareLength t n /= LT
624-- -----------------------------------------------------------------------------
641-- -----------------------------------------------------------------------------
@@ -672,13 +689,9 @@ reverse t = S.reverse (stream t)
673-- In (unlikely) bad cases, this function's time complexity degrades
690-- In (unlikely) bad cases, this function's time complexity degrades
674-- towards /O(n*m)/.
691-- towards /O(n*m)/.
675−replace :: Text
692+replace :: Text -- ^ @needle@ to search for
676− -- ^ @needle@ to search for. If this string is empty, an
677− -- error will occur.
679− -- ^ @replacement@ to replace @needle@ with.
693+ -> Text -- ^ @replacement@ to replace @needle@ with
681− -- ^ @haystack@ in which to search.
694+ -> Text -- ^ @haystack@ in which to search
683replace needle@(Text _ _ neeLen)
696replace needle@(Text _ _ neeLen)
684 (Text repArr repOff repLen)
697 (Text repArr repOff repLen)
@@ -741,7 +754,7 @@ replace needle@(Text _ _ neeLen)
741-- instead of itself.
754-- instead of itself.
742toCaseFold :: Text -> Text
755toCaseFold :: Text -> Text
743toCaseFold t = unstream (S.toCaseFold (stream t))
756toCaseFold t = unstream (S.toCaseFold (stream t))
744−{-# INLINE toCaseFold #-}
757+{-# INLINE [0] toCaseFold #-}
746-- | /O(n)/ Convert a string to lower case, using simple case
759-- | /O(n)/ Convert a string to lower case, using simple case
747-- conversion. Subject to fusion.
760-- conversion. Subject to fusion.
@@ -915,13 +928,13 @@ concatMap f = concat . foldr ((:) . f) []
915{-# INLINE concatMap #-}
928{-# INLINE concatMap #-}
917-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
930-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
918−-- 'Text' @t@ satisfies the predicate @p@. Subject to fusion.
931+-- 'Text' @t@ satisifes the predicate @p@. Subject to fusion.
919any :: (Char -> Bool) -> Text -> Bool
932any :: (Char -> Bool) -> Text -> Bool
920any p t = S.any p (stream t)
933any p t = S.any p (stream t)
921{-# INLINE any #-}
934{-# INLINE any #-}
923-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
936-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
924−-- 'Text' @t@ satisfy the predicate @p@. Subject to fusion.
937+-- 'Text' @t@ satisify the predicate @p@. Subject to fusion.
925all :: (Char -> Bool) -> Text -> Bool
938all :: (Char -> Bool) -> Text -> Bool
926all p t = S.all p (stream t)
939all p t = S.all p (stream t)
927{-# INLINE all #-}
940{-# INLINE all #-}
@@ -1152,27 +1165,6 @@ takeWhile p t@(Text arr off len) = loop 0
1152 unstream (S.takeWhile p (stream t)) = takeWhile p t
1165 unstream (S.takeWhile p (stream t)) = takeWhile p t
1155−-- | /O(n)/ 'takeWhileEnd', applied to a predicate @p@ and a 'Text',
1156−-- returns the longest suffix (possibly empty) of elements that
1157−-- satisfy @p@. Subject to fusion.
1160−-- > takeWhileEnd (=='o') "foo" == "oo"
1161−takeWhileEnd :: (Char -> Bool) -> Text -> Text
1162−takeWhileEnd p t@(Text arr off len) = loop (len-1) len
1163− where loop !i !l | l <= 0 = t
1164− | p c = loop (i+d) (l+d)
1165− | otherwise = text arr (off+l) (len-l)
1166− where (c,d) = reverseIter t i
1167−{-# INLINE [1] takeWhileEnd #-}
1170−"TEXT takeWhileEnd -> fused" [~1] forall p t.
1171− takeWhileEnd p t = S.reverse (S.takeWhile p (S.reverseStream t))
1172−"TEXT takeWhileEnd -> unfused" [1] forall p t.
1173− S.reverse (S.takeWhile p (S.reverseStream t)) = takeWhileEnd p t
1176-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1168-- | /O(n)/ 'dropWhile' @p@ @t@ returns the suffix remaining after
1177-- 'takeWhile' @p@ @t@. Subject to fusion.
1169-- 'takeWhile' @p@ @t@. Subject to fusion.
1178dropWhile :: (Char -> Bool) -> Text -> Text
1170dropWhile :: (Char -> Bool) -> Text -> Text
@@ -1191,9 +1183,8 @@ dropWhile p t@(Text arr off len) = loop 0 0
1193-- | /O(n)/ 'dropWhileEnd' @p@ @t@ returns the prefix remaining after
1185-- | /O(n)/ 'dropWhileEnd' @p@ @t@ returns the prefix remaining after
1194−-- dropping characters that satisfy the predicate @p@ from the end of
1186+-- dropping characters that fail the predicate @p@ from the end of
1195-- @t@. Subject to fusion.
1187-- @t@. Subject to fusion.
1197-- Examples:
1188-- Examples:
1199-- > dropWhileEnd (=='.') "foo..." == "foo"
1190-- > dropWhileEnd (=='.') "foo..." == "foo"
@@ -1213,7 +1204,7 @@ dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1215-- | /O(n)/ 'dropAround' @p@ @t@ returns the substring remaining after
1206-- | /O(n)/ 'dropAround' @p@ @t@ returns the substring remaining after
1216−-- dropping characters that satisfy the predicate @p@ from both the
1207+-- dropping characters that fail the predicate @p@ from both the
1217-- beginning and end of @t@. Subject to fusion.
1208-- beginning and end of @t@. Subject to fusion.
1218dropAround :: (Char -> Bool) -> Text -> Text
1209dropAround :: (Char -> Bool) -> Text -> Text
1219dropAround p = dropWhile p . dropWhileEnd p
1210dropAround p = dropWhile p . dropWhileEnd p
@@ -1308,9 +1299,9 @@ tails t | null t = [empty]
1308-- copies to create substrings; they just construct new 'Text's that
1299-- copies to create substrings; they just construct new 'Text's that
1309-- are slices of the original.
1300-- are slices of the original.
1311−-- | /O(m+n)/ Break a 'Text' into pieces separated by the first 'Text'
1302+-- | /O(m+n)/ Break a 'Text' into pieces separated by the first
1312−-- argument (which cannot be empty), consuming the delimiter. An empty
1303+-- 'Text' argument, consuming the delimiter. An empty delimiter is
1313−-- delimiter is invalid, and will cause an error to be raised.
1304+-- invalid, and will cause an error to be raised.
1315-- Examples:
1306-- Examples:
@@ -1323,16 +1314,9 @@ tails t | null t = [empty]
1323-- > intercalate s . splitOn s == id
1314-- > intercalate s . splitOn s == id
1324-- > splitOn (singleton c) == split (==c)
1315-- > splitOn (singleton c) == split (==c)
1326−-- (Note: the string @s@ to split on above cannot be empty.)
1328-- In (unlikely) bad cases, this function's time complexity degrades
1317-- In (unlikely) bad cases, this function's time complexity degrades
1329-- towards /O(n*m)/.
1318-- towards /O(n*m)/.
1330−splitOn :: Text
1319+splitOn :: Text -> Text -> [Text]
1331− -- ^ String to split on. If this string is empty, an error
1336splitOn pat@(Text _ _ l) src@(Text arr off len)
1320splitOn pat@(Text _ _ l) src@(Text arr off len)
1337 | l <= 0 = emptyError "splitOn"
1321 | l <= 0 = emptyError "splitOn"
1338 | isSingleton pat = split (== unsafeHead pat) src
1322 | isSingleton pat = split (== unsafeHead pat) src
@@ -1540,7 +1524,7 @@ countChar c t = S.countChar c (stream t)
1540-- equivalent to a pair of 'unpack' operations.
1524-- equivalent to a pair of 'unpack' operations.
1541zip :: Text -> Text -> [(Char,Char)]
1525zip :: Text -> Text -> [(Char,Char)]
1542zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1526zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1543−{-# INLINE zip #-}
1527+{-# INLINE [0] zip #-}
1545-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1529-- | /O(n)/ 'zipWith' generalises 'zip' by zipping with the function
1546-- given as the first argument, instead of a tupling function.
1530-- given as the first argument, instead of a tupling function.
@@ -1548,7 +1532,7 @@ zip a b = S.unstreamList $ S.zipWith (,) (stream a) (stream b)
1548zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1532zipWith :: (Char -> Char -> Char) -> Text -> Text -> Text
1549zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1533zipWith f t1 t2 = unstream (S.zipWith g (stream t1) (stream t2))
1550 where g a b = safe (f a b)
1534 where g a b = safe (f a b)
1551−{-# INLINE zipWith #-}
1535+{-# INLINE [0] zipWith #-}
1553-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1537-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1554-- representing white space.
1538-- representing white space.