@@ -34,7 +34,6 @@
3434-- with Unicode text (including normalization, regular expressions,
3535-- non-standard encodings, text breaking, and locales), see
3636-- <http://hackage.haskell.org/package/text-icu the text-icu package >.
37−--
3837
3938module Data.Text
4039 (
@@ -44,9 +43,6 @@ module Data.Text
4443 -- * Acceptable data
4544 -- $replacement
4645
47− -- * Definition of character
48− -- $character_definition
49−
5046 -- * Fusion
5147 -- $fusion
5248
@@ -64,7 +60,6 @@ module Data.Text
6460 , snoc
6561 , append
6662 , uncons
67− , unsnoc
6863 , head
6964 , last
7065 , tail
@@ -251,17 +246,6 @@ import qualified GHC.Exts as Exts
251246import Text.Printf (PrintfArg, formatArg, formatString)
252247#endif
253248
254−-- $character-definition
255−--
256−-- This package uses the word /character/ to denote Unicode code points.
257−--
258−-- Note that this is not the same thing as a grapheme (e.g. a composition of
259−-- code points that form one visual symbol). For instance, consider the grapheme
260−-- @ä@. This symbol has two Unicode representations: a single code-point
261−-- representation @U+00E4@ (the @LATIN SMALL LETTER A WITH DIAERESIS@ code
262−-- point), and a two code point representation @U+0061@ (the @A@ code point) and
263−-- @U+0308@ (the @COMBINING DIAERESIS@ code point).
264−
265249-- $strict
266250--
267251-- This package provides both strict and lazy 'Text' types. The
@@ -350,13 +334,9 @@ instance Read Text where
350334 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
351335
352336#if MIN_VERSION_base(4,9,0)
353−-- | Non-orphan 'Semigroup' instance only defined for
354−-- @base-4.9.0.0@ and later; orphan instances for older GHCs are
355−-- provided by
356−-- the [semigroups](http://hackage.haskell.org/package/semigroups)
357−-- package
358−--
359−-- @since 1.2.2.0
337+-- Semigroup orphan instances for older GHCs are provided by
338+-- 'semigroups` package
339+
360340instance Semigroup Text where
361341 (<>) = append
362342#endif
@@ -374,7 +354,6 @@ instance IsString Text where
374354 fromString = pack
375355
376356#if __GLASGOW_HASKELL__ >= 708
377−-- | @since 1.2.0.0
378357instance Exts.IsList Text where
379358 type Item Text = Char
380359 fromList = pack
@@ -385,7 +364,6 @@ instance Exts.IsList Text where
385364instance NFData Text where rnf !_ = ()
386365#endif
387366
388−-- | @since 1.2.1.0
389367instance Binary Text where
390368 put t = put (encodeUtf8 t)
391369 get = do
@@ -419,8 +397,6 @@ instance Data Text where
419397
420398#if MIN_VERSION_base(4,7,0)
421399-- | Only defined for @base-4.7.0.0@ and later
422−--
423−-- @since 1.2.2.0
424400instance PrintfArg Text where
425401 formatArg txt = formatString $ unpack txt
426402#endif
@@ -569,19 +545,6 @@ init (Text arr off len) | len <= 0 = emptyError "init"
569545 unstream (S.init (stream t)) = init t
570546 #-}
571547
572−-- | /O(1)/ Returns all but the last character and the last character of a
573−-- 'Text', or 'Nothing' if empty.
574−--
575−-- @since 1.2.3.0
576−unsnoc :: Text -> Maybe (Text, Char)
577−unsnoc (Text arr off len)
578− | len <= 0 = Nothing
579− | n < 0xDC00 || n > 0xDFFF = Just (text arr off (len-1), unsafeChr n)
580− | otherwise = Just (text arr off (len-2), U16.chr2 n0 n)
581− where n = A.unsafeIndex arr (off+len-1)
582− n0 = A.unsafeIndex arr (off+len-2)
583−{-# INLINE [1] unsnoc #-}
584−
585548-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to
586549-- fusion.
587550null :: Text -> Bool
@@ -661,15 +624,8 @@ compareLength t n = S.compareLengthI (stream t) n
661624-- -----------------------------------------------------------------------------
662625-- * Transformations
663626-- | /O(n)/ 'map' @f@ @t@ is the 'Text' obtained by applying @f@ to
664−-- each element of @t@.
665−--
666−-- Example:
667−--
668−-- >>> let message = pack "I am not angry. Not at all."
669−-- >>> T.map (\c -> if c == '.' then '!' else c) message
670−-- "I am not angry! Not at all!"
671−--
672−-- Subject to fusion. Performs replacement on invalid scalar values.
627+-- each element of @t@. Subject to fusion. Performs replacement on
628+-- invalid scalar values.
673629map :: (Char -> Char) -> Text -> Text
674630map f t = unstream (S.map (safe . f) (stream t))
675631{-# INLINE [1] map #-}
@@ -677,36 +633,18 @@ map f t = unstream (S.map (safe . f) (stream t))
677633-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
678634-- 'Text's and concatenates the list after interspersing the first
679635-- argument between each element of the list.
680−--
681−-- Example:
682−--
683−-- >>> T.intercalate "NI!" ["We", "seek", "the", "Holy", "Grail"]
684−-- "WeNI!seekNI!theNI!HolyNI!Grail"
685636intercalate :: Text -> [Text] -> Text
686637intercalate t = concat . (F.intersperse t)
687638{-# INLINE intercalate #-}
688639
689640-- | /O(n)/ The 'intersperse' function takes a character and places it
690−-- between the characters of a 'Text'.
691−--
692−-- Example:
693−--
694−-- >>> T.intersperse '.' "SHIELD"
695−-- "S.H.I.E.L.D"
696−--
697−-- Subject to fusion. Performs replacement on invalid scalar values.
641+-- between the characters of a 'Text'. Subject to fusion. Performs
642+-- replacement on invalid scalar values.
698643intersperse :: Char -> Text -> Text
699644intersperse c t = unstream (S.intersperse (safe c) (stream t))
700645{-# INLINE intersperse #-}
701646
702−-- | /O(n)/ Reverse the characters of a string.
703−--
704−-- Example:
705−--
706−-- >>> T.reverse "desrever"
707−-- "reversed"
708−--
709−-- Subject to fusion.
647+-- | /O(n)/ Reverse the characters of a string. Subject to fusion.
710648reverse :: Text -> Text
711649reverse t = S.reverse (stream t)
712650{-# INLINE reverse #-}
@@ -725,14 +663,12 @@ reverse t = S.reverse (stream t)
725663-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
726664-- be replaced recursively:
727665--
728−-- >>> replace "oo" "foo" "oo"
729−-- "foo"
666+-- > replace "oo" "foo" "oo" == "foo"
730667--
731668-- In cases where several instances of @needle@ overlap, only the
732669-- first one will be replaced:
733670--
734−-- >>> replace "ofo" "bar" "ofofo"
735−-- "barfo"
671+-- > replace "ofo" "bar" "ofofo" == "barfo"
736672--
737673-- In (unlikely) bad cases, this function's time complexity degrades
738674-- towards /O(n*m)/.
@@ -846,8 +782,6 @@ toUpper t = unstream (S.toUpper (stream t))
846782-- guides disagree on whether the book name \"The Hill of the Red
847783-- Fox\" is correctly title cased—but this function will
848784-- capitalize /every/ word.
849−--
850−-- @since 1.0.0.0
851785toTitle :: Text -> Text
852786toTitle t = unstream (S.toTitle (stream t))
853787{-# INLINE toTitle #-}
@@ -858,11 +792,8 @@ toTitle t = unstream (S.toTitle (stream t))
858792--
859793-- Examples:
860794--
861−-- >>> justifyLeft 7 'x' "foo"
862−-- "fooxxxx"
863−--
864−-- >>> justifyLeft 3 'x' "foobar"
865−-- "foobar"
795+-- > justifyLeft 7 'x' "foo" == "fooxxxx"
796+-- > justifyLeft 3 'x' "foobar" == "foobar"
866797justifyLeft :: Int -> Char -> Text -> Text
867798justifyLeft k c t
868799 | len >= k = t
@@ -883,11 +814,8 @@ justifyLeft k c t
883814--
884815-- Examples:
885816--
886−-- >>> justifyRight 7 'x' "bar"
887−-- "xxxxbar"
888−--
889−-- >>> justifyRight 3 'x' "foobar"
890−-- "foobar"
817+-- > justifyRight 7 'x' "bar" == "xxxxbar"
818+-- > justifyRight 3 'x' "foobar" == "foobar"
891819justifyRight :: Int -> Char -> Text -> Text
892820justifyRight k c t
893821 | len >= k = t
@@ -901,8 +829,7 @@ justifyRight k c t
901829--
902830-- Examples:
903831--
904−-- >>> center 8 'x' "HS"
905−-- "xxxHSxxx"
832+-- > center 8 'x' "HS" = "xxxHSxxx"
906833center :: Int -> Char -> Text -> Text
907834center k c t
908835 | len >= k = t
@@ -917,14 +844,6 @@ center k c t
917844-- of its 'Text' argument. Note that this function uses 'pack',
918845-- 'unpack', and the list version of transpose, and is thus not very
919846-- efficient.
920−--
921−-- Examples:
922−--
923−-- >>> transpose ["green","orange"]
924−-- ["go","rr","ea","en","ng","e"]
925−--
926−-- >>> transpose ["blue","red"]
927−-- ["br","le","ud","e"]
928847transpose :: [Text] -> [Text]
929848transpose ts = P.map pack (L.transpose (P.map unpack ts))
930849
@@ -1169,10 +1088,7 @@ iterN n t@(Text _arr _off len) = loop 0 0
11691088--
11701089-- Examples:
11711090--
1172−-- >>> takeEnd 3 "foobar"
1173−-- "bar"
1174−--
1175−-- @since 1.1.1.0
1091+-- > takeEnd 3 "foobar" == "bar"
11761092takeEnd :: Int -> Text -> Text
11771093takeEnd n t@(Text arr off len)
11781094 | n <= 0 = empty
@@ -1183,8 +1099,8 @@ takeEnd n t@(Text arr off len)
11831099iterNEnd :: Int -> Text -> Int
11841100iterNEnd n t@(Text _arr _off len) = loop (len-1) n
11851101 where loop i !m
1186− | m <= 0 = i+1
11871102 | i <= 0 = 0
1103+ | m <= 1 = i
11881104 | otherwise = loop (i+d) (m-1)
11891105 where d = reverseIter_ t i
11901106
@@ -1211,10 +1127,7 @@ drop n t@(Text arr off len)
12111127--
12121128-- Examples:
12131129--
1214−-- >>> dropEnd 3 "foobar"
1215−-- "foo"
1216−--
1217−-- @since 1.1.1.0
1130+-- > dropEnd 3 "foobar" == "foo"
12181131dropEnd :: Int -> Text -> Text
12191132dropEnd n t@(Text arr off len)
12201133 | n <= 0 = t
@@ -1244,10 +1157,7 @@ takeWhile p t@(Text arr off len) = loop 0
12441157-- satisfy @p@. Subject to fusion.
12451158-- Examples:
12461159--
1247−-- >>> takeWhileEnd (=='o') "foo"
1248−-- "oo"
1249−--
1250−-- @since 1.2.2.0
1160+-- > takeWhileEnd (=='o') "foo" == "oo"
12511161takeWhileEnd :: (Char -> Bool) -> Text -> Text
12521162takeWhileEnd p t@(Text arr off len) = loop (len-1) len
12531163 where loop !i !l | l <= 0 = t
@@ -1286,8 +1196,7 @@ dropWhile p t@(Text arr off len) = loop 0 0
12861196--
12871197-- Examples:
12881198--
1289−-- >>> dropWhileEnd (=='.') "foo..."
1290−-- "foo"
1199+-- > dropWhileEnd (=='.') "foo..." == "foo"
12911200dropWhileEnd :: (Char -> Bool) -> Text -> Text
12921201dropWhileEnd p t@(Text arr off len) = loop (len-1) len
12931202 where loop !i !l | l <= 0 = empty
@@ -1405,14 +1314,9 @@ tails t | null t = [empty]
14051314--
14061315-- Examples:
14071316--
1408−-- >>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
1409−-- ["a","b","d","e"]
1410−--
1411−-- >>> splitOn "aaa" "aaaXaaaXaaaXaaa"
1412−-- ["","X","X","X",""]
1413−--
1414−-- >>> splitOn "x" "x"
1415−-- ["",""]
1317+-- > splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1318+-- > splitOn "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1319+-- > splitOn "x" "x" == ["",""]
14161320--
14171321-- and
14181322--
@@ -1448,11 +1352,8 @@ splitOn pat@(Text _ _ l) src@(Text arr off len)
14481352-- resulting components do not contain the separators. Two adjacent
14491353-- separators result in an empty component in the output. eg.
14501354--
1451−-- >>> split (=='a') "aabbaca"
1452−-- ["","","bb","c",""]
1453−--
1454−-- >>> split (=='a') ""
1455−-- [""]
1355+-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1356+-- > split (=='a') "" == [""]
14561357split :: (Char -> Bool) -> Text -> [Text]
14571358split _ t@(Text _off _arr 0) = [t]
14581359split p t = loop t
@@ -1465,11 +1366,8 @@ split p t = loop t
14651366-- element may be shorter than the other chunks, depending on the
14661367-- length of the input. Examples:
14671368--
1468−-- >>> chunksOf 3 "foobarbaz"
1469−-- ["foo","bar","baz"]
1470−--
1471−-- >>> chunksOf 4 "haskell.org"
1472−-- ["hask","ell.","org"]
1369+-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1370+-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
14731371chunksOf :: Int -> Text -> [Text]
14741372chunksOf k = go
14751373 where
@@ -1514,11 +1412,8 @@ filter p t = unstream (S.filter p (stream t))
15141412--
15151413-- Examples:
15161414--
1517−-- >>> breakOn "::" "a::b::c"
1518−-- ("a","::b::c")
1519−--
1520−-- >>> breakOn "/" "foobar"
1521−-- ("foobar","")
1415+-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1416+-- > breakOn "/" "foobar" ==> ("foobar", "")
15221417--
15231418-- Laws:
15241419--
@@ -1546,8 +1441,7 @@ breakOn pat src@(Text arr off len)
15461441-- up to and including the last match of @needle@. The second is the
15471442-- remainder of @haystack@, following the match.
15481443--
1549−-- >>> breakOnEnd "::" "a::b::c"
1550−-- ("a::b::","c")
1444+-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
15511445breakOnEnd :: Text -> Text -> (Text, Text)
15521446breakOnEnd pat src = (reverse b, reverse a)
15531447 where (a,b) = breakOn (reverse pat) (reverse src)
@@ -1562,11 +1456,10 @@ breakOnEnd pat src = (reverse b, reverse a)
15621456--
15631457-- Examples:
15641458--
1565−-- >>> breakOnAll "::" ""
1566−-- []
1567−--
1568−-- >>> breakOnAll "/" "a/b/c/"
1569−-- [("a","/b/c/"),("a/b","/c/"),("a/b/c","/")]
1459+-- > breakOnAll "::" ""
1460+-- > ==> []
1461+-- > breakOnAll "/" "a/b/c/"
1462+-- > ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
15701463--
15711464-- In (unlikely) bad cases, this function's time complexity degrades
15721465-- towards /O(n*m)/.
@@ -1765,14 +1658,9 @@ isInfixOf needle haystack
17651658--
17661659-- Examples:
17671660--
1768−-- >>> stripPrefix "foo" "foobar"
1769−-- Just "bar"
1770−--
1771−-- >>> stripPrefix "" "baz"
1772−-- Just "baz"
1773−--
1774−-- >>> stripPrefix "foo" "quux"
1775−-- Nothing
1661+-- > stripPrefix "foo" "foobar" == Just "bar"
1662+-- > stripPrefix "" "baz" == Just "baz"
1663+-- > stripPrefix "foo" "quux" == Nothing
17761664--
17771665-- This is particularly useful with the @ViewPatterns@ extension to
17781666-- GHC, as follows:
@@ -1797,14 +1685,9 @@ stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
17971685--
17981686-- Examples:
17991687--
1800−-- >>> commonPrefixes "foobar" "fooquux"
1801−-- Just ("foo","bar","quux")
1802−--
1803−-- >>> commonPrefixes "veeble" "fetzer"
1804−-- Nothing
1805−--
1806−-- >>> commonPrefixes "" "baz"
1807−-- Nothing
1688+-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
1689+-- > commonPrefixes "veeble" "fetzer" == Nothing
1690+-- > commonPrefixes "" "baz" == Nothing
18081691commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
18091692commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
18101693 where
@@ -1821,14 +1704,9 @@ commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
18211704--
18221705-- Examples:
18231706--
1824−-- >>> stripSuffix "bar" "foobar"
1825−-- Just "foo"
1826−--
1827−-- >>> stripSuffix "" "baz"
1828−-- Just "baz"
1829−--
1830−-- >>> stripSuffix "foo" "quux"
1831−-- Nothing
1707+-- > stripSuffix "bar" "foobar" == Just "foo"
1708+-- > stripSuffix "" "baz" == Just "baz"
1709+-- > stripSuffix "foo" "quux" == Nothing
18321710--
18331711-- This is particularly useful with the @ViewPatterns@ extension to
18341712-- GHC, as follows:
@@ -1875,12 +1753,3 @@ copy (Text arr off len) = Text (A.run go) 0 len
18751753 marr <- A.new len
18761754 A.copyI marr 0 arr off len
18771755 return marr
1878−
1879−
1880−-------------------------------------------------
1881−-- NOTE: the named chunk below used by doctest;
1882−-- verify the doctests via `doctest -fobject-code Data/Text.hs`
1883−
1884−-- $setup
1885−-- >>> :set -XOverloadedStrings
1886−-- >>> import qualified Data.Text as T