on master help sign in
Convert Data.Text examples into machine-verifiable doctest markup
772254fHerbert Valerio Riedel
Data/Text.hs+101−42
@@ -665,7 +665,9 @@ compareLength t n = S.compareLengthI (stream t) n
665--
665--
666-- Example:
666-- Example:
667--
667--
668+-- >>> let message = pack "I am not angry. Not at all."
669+-- >>> T.map (\c -> if c == '.' then '!' else c) message
668−-- > map (\c -> if c == '.' then '!' else c) $ pack "I am not angry. Not at all." ==> "I am not angry! Not at all!"
670+-- "I am not angry! Not at all!"
669--
671--
670-- Subject to fusion. Performs replacement on invalid scalar values.
672-- Subject to fusion. Performs replacement on invalid scalar values.
671map :: (Char -> Char) -> Text -> Text
673map :: (Char -> Char) -> Text -> Text
@@ -678,9 +680,8 @@ map f t = unstream (S.map (safe . f) (stream t))
678--
680--
679-- Example:
681-- Example:
680--
682--
681−-- > let ni = pack "NI!"
682−-- > let sentence = fmap pack ["We", "seek", "the", "Holy", "Grail"]
683+-- >>> T.intercalate "NI!" ["We", "seek", "the", "Holy", "Grail"]
683−-- > intercalate ni sentence ==> ""WeNI!seekNI!theNI!HolyNI!Grail""
684+-- "WeNI!seekNI!theNI!HolyNI!Grail"
684intercalate :: Text -> [Text] -> Text
685intercalate :: Text -> [Text] -> Text
685intercalate t = concat . (F.intersperse t)
686intercalate t = concat . (F.intersperse t)
686{-# INLINE intercalate #-}
687{-# INLINE intercalate #-}
@@ -690,7 +691,8 @@ intercalate t = concat . (F.intersperse t)
690--
691--
691-- Example:
692-- Example:
692--
693--
693−-- > intersperse '.' $ pack "SHIELD" ==> "S.H.I.E.L.D"
694+-- >>> T.intersperse '.' "SHIELD"
695+-- "S.H.I.E.L.D"
694--
696--
695-- Subject to fusion. Performs replacement on invalid scalar values.
697-- Subject to fusion. Performs replacement on invalid scalar values.
696intersperse :: Char -> Text -> Text
698intersperse :: Char -> Text -> Text
@@ -701,7 +703,8 @@ intersperse c t = unstream (S.intersperse (safe c) (stream t))
701--
703--
702-- Example:
704-- Example:
703--
705--
704−-- > reverse $ pack "desrever" ==> "reversed"
706+-- >>> T.reverse "desrever"
707+-- "reversed"
705--
708--
706-- Subject to fusion.
709-- Subject to fusion.
707reverse :: Text -> Text
710reverse :: Text -> Text
@@ -722,12 +725,14 @@ reverse t = S.reverse (stream t)
722-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
725-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
723-- be replaced recursively:
726-- be replaced recursively:
724--
727--
725−-- > replace "oo" "foo" "oo" == "foo"
728+-- >>> replace "oo" "foo" "oo"
729+-- "foo"
726--
730--
727-- In cases where several instances of @needle@ overlap, only the
731-- In cases where several instances of @needle@ overlap, only the
728-- first one will be replaced:
732-- first one will be replaced:
729--
733--
730−-- > replace "ofo" "bar" "ofofo" == "barfo"
734+-- >>> replace "ofo" "bar" "ofofo"
735+-- "barfo"
731--
736--
732-- In (unlikely) bad cases, this function's time complexity degrades
737-- In (unlikely) bad cases, this function's time complexity degrades
733-- towards /O(n*m)/.
738-- towards /O(n*m)/.
@@ -853,8 +858,11 @@ toTitle t = unstream (S.toTitle (stream t))
853--
858--
854-- Examples:
859-- Examples:
855--
860--
856−-- > justifyLeft 7 'x' "foo" == "fooxxxx"
861+-- >>> justifyLeft 7 'x' "foo"
862+-- "fooxxxx"
863+--
857−-- > justifyLeft 3 'x' "foobar" == "foobar"
864+-- >>> justifyLeft 3 'x' "foobar"
865+-- "foobar"
858justifyLeft :: Int -> Char -> Text -> Text
866justifyLeft :: Int -> Char -> Text -> Text
859justifyLeft k c t
867justifyLeft k c t
860 | len >= k = t
868 | len >= k = t
@@ -875,8 +883,11 @@ justifyLeft k c t
875--
883--
876-- Examples:
884-- Examples:
877--
885--
878−-- > justifyRight 7 'x' "bar" == "xxxxbar"
886+-- >>> justifyRight 7 'x' "bar"
887+-- "xxxxbar"
888+--
879−-- > justifyRight 3 'x' "foobar" == "foobar"
889+-- >>> justifyRight 3 'x' "foobar"
890+-- "foobar"
880justifyRight :: Int -> Char -> Text -> Text
891justifyRight :: Int -> Char -> Text -> Text
881justifyRight k c t
892justifyRight k c t
882 | len >= k = t
893 | len >= k = t
@@ -890,7 +901,8 @@ justifyRight k c t
890--
901--
891-- Examples:
902-- Examples:
892--
903--
893−-- > center 8 'x' "HS" = "xxxHSxxx"
904+-- >>> center 8 'x' "HS"
905+-- "xxxHSxxx"
894center :: Int -> Char -> Text -> Text
906center :: Int -> Char -> Text -> Text
895center k c t
907center k c t
896 | len >= k = t
908 | len >= k = t
@@ -908,8 +920,11 @@ center k c t
908--
920--
909-- Examples:
921-- Examples:
910--
922--
911−-- > transpose [pack "green", pack "orange"] ==> ["go","rr","ea","en","ng","e"]
923+-- >>> transpose ["green","orange"]
924+-- ["go","rr","ea","en","ng","e"]
925+--
912−-- > transpose [pack "blue", pack "red"] ==> ["br","le","ud","e"]
926+-- >>> transpose ["blue","red"]
927+-- ["br","le","ud","e"]
913transpose :: [Text] -> [Text]
928transpose :: [Text] -> [Text]
914transpose ts = P.map pack (L.transpose (P.map unpack ts))
929transpose ts = P.map pack (L.transpose (P.map unpack ts))
915
930
@@ -1154,7 +1169,8 @@ iterN n t@(Text _arr _off len) = loop 0 0
1154--
1169--
1155-- Examples:
1170-- Examples:
1156--
1171--
1157−-- > takeEnd 3 "foobar" == "bar"
1172+-- >>> takeEnd 3 "foobar"
1173+-- "bar"
1158--
1174--
1159-- @since 1.1.1.0
1175-- @since 1.1.1.0
1160takeEnd :: Int -> Text -> Text
1176takeEnd :: Int -> Text -> Text
@@ -1195,7 +1211,8 @@ drop n t@(Text arr off len)
1195--
1211--
1196-- Examples:
1212-- Examples:
1197--
1213--
1198−-- > dropEnd 3 "foobar" == "foo"
1214+-- >>> dropEnd 3 "foobar"
1215+-- "foo"
1199--
1216--
1200-- @since 1.1.1.0
1217-- @since 1.1.1.0
1201dropEnd :: Int -> Text -> Text
1218dropEnd :: Int -> Text -> Text
@@ -1227,7 +1244,8 @@ takeWhile p t@(Text arr off len) = loop 0
1227-- satisfy @p@. Subject to fusion.
1244-- satisfy @p@. Subject to fusion.
1228-- Examples:
1245-- Examples:
1229--
1246--
1230−-- > takeWhileEnd (=='o') "foo" == "oo"
1247+-- >>> takeWhileEnd (=='o') "foo"
1248+-- "oo"
1231--
1249--
1232-- @since 1.2.2.0
1250-- @since 1.2.2.0
1233takeWhileEnd :: (Char -> Bool) -> Text -> Text
1251takeWhileEnd :: (Char -> Bool) -> Text -> Text
@@ -1268,7 +1286,8 @@ dropWhile p t@(Text arr off len) = loop 0 0
1268--
1286--
1269-- Examples:
1287-- Examples:
1270--
1288--
1271−-- > dropWhileEnd (=='.') "foo..." == "foo"
1289+-- >>> dropWhileEnd (=='.') "foo..."
1290+-- "foo"
1272dropWhileEnd :: (Char -> Bool) -> Text -> Text
1291dropWhileEnd :: (Char -> Bool) -> Text -> Text
1273dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1292dropWhileEnd p t@(Text arr off len) = loop (len-1) len
1274 where loop !i !l | l <= 0 = empty
1293 where loop !i !l | l <= 0 = empty
@@ -1386,9 +1405,14 @@ tails t | null t = [empty]
1386--
1405--
1387-- Examples:
1406-- Examples:
1388--
1407--
1389−-- > splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1408+-- >>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
1409+-- ["a","b","d","e"]
1410+--
1390−-- > splitOn "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1411+-- >>> splitOn "aaa" "aaaXaaaXaaaXaaa"
1412+-- ["","X","X","X",""]
1413+--
1391−-- > splitOn "x" "x" == ["",""]
1414+-- >>> splitOn "x" "x"
1415+-- ["",""]
1392--
1416--
1393-- and
1417-- and
1394--
1418--
@@ -1424,8 +1448,11 @@ splitOn pat@(Text _ _ l) src@(Text arr off len)
1424-- resulting components do not contain the separators. Two adjacent
1448-- resulting components do not contain the separators. Two adjacent
1425-- separators result in an empty component in the output. eg.
1449-- separators result in an empty component in the output. eg.
1426--
1450--
1427−-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1451+-- >>> split (=='a') "aabbaca"
1452+-- ["","","bb","c",""]
1453+--
1428−-- > split (=='a') "" == [""]
1454+-- >>> split (=='a') ""
1455+-- [""]
1429split :: (Char -> Bool) -> Text -> [Text]
1456split :: (Char -> Bool) -> Text -> [Text]
1430split _ t@(Text _off _arr 0) = [t]
1457split _ t@(Text _off _arr 0) = [t]
1431split p t = loop t
1458split p t = loop t
@@ -1438,8 +1465,11 @@ split p t = loop t
1438-- element may be shorter than the other chunks, depending on the
1465-- element may be shorter than the other chunks, depending on the
1439-- length of the input. Examples:
1466-- length of the input. Examples:
1440--
1467--
1441−-- > chunksOf 3 "foobarbaz" == ["foo","bar","baz"]
1468+-- >>> chunksOf 3 "foobarbaz"
1469+-- ["foo","bar","baz"]
1470+--
1442−-- > chunksOf 4 "haskell.org" == ["hask","ell.","org"]
1471+-- >>> chunksOf 4 "haskell.org"
1472+-- ["hask","ell.","org"]
1443chunksOf :: Int -> Text -> [Text]
1473chunksOf :: Int -> Text -> [Text]
1444chunksOf k = go
1474chunksOf k = go
1445 where
1475 where
@@ -1484,8 +1514,11 @@ filter p t = unstream (S.filter p (stream t))
1484--
1514--
1485-- Examples:
1515-- Examples:
1486--
1516--
1487−-- > breakOn "::" "a::b::c" ==> ("a", "::b::c")
1517+-- >>> breakOn "::" "a::b::c"
1518+-- ("a","::b::c")
1519+--
1488−-- > breakOn "/" "foobar" ==> ("foobar", "")
1520+-- >>> breakOn "/" "foobar"
1521+-- ("foobar","")
1489--
1522--
1490-- Laws:
1523-- Laws:
1491--
1524--
@@ -1513,7 +1546,8 @@ breakOn pat src@(Text arr off len)
1513-- up to and including the last match of @needle@. The second is the
1546-- up to and including the last match of @needle@. The second is the
1514-- remainder of @haystack@, following the match.
1547-- remainder of @haystack@, following the match.
1515--
1548--
1516−-- > breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
1549+-- >>> breakOnEnd "::" "a::b::c"
1550+-- ("a::b::","c")
1517breakOnEnd :: Text -> Text -> (Text, Text)
1551breakOnEnd :: Text -> Text -> (Text, Text)
1518breakOnEnd pat src = (reverse b, reverse a)
1552breakOnEnd pat src = (reverse b, reverse a)
1519 where (a,b) = breakOn (reverse pat) (reverse src)
1553 where (a,b) = breakOn (reverse pat) (reverse src)
@@ -1528,10 +1562,11 @@ breakOnEnd pat src = (reverse b, reverse a)
1528--
1562--
1529-- Examples:
1563-- Examples:
1530--
1564--
1531−-- > breakOnAll "::" ""
1565+-- >>> breakOnAll "::" ""
1532−-- > ==> []
1566+-- []
1567+--
1533−-- > breakOnAll "/" "a/b/c/"
1568+-- >>> breakOnAll "/" "a/b/c/"
1534−-- > ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
1569+-- [("a","/b/c/"),("a/b","/c/"),("a/b/c","/")]
1535--
1570--
1536-- In (unlikely) bad cases, this function's time complexity degrades
1571-- In (unlikely) bad cases, this function's time complexity degrades
1537-- towards /O(n*m)/.
1572-- towards /O(n*m)/.
@@ -1730,9 +1765,14 @@ isInfixOf needle haystack
1730--
1765--
1731-- Examples:
1766-- Examples:
1732--
1767--
1733−-- > stripPrefix "foo" "foobar" == Just "bar"
1768+-- >>> stripPrefix "foo" "foobar"
1769+-- Just "bar"
1770+--
1734−-- > stripPrefix "" "baz" == Just "baz"
1771+-- >>> stripPrefix "" "baz"
1772+-- Just "baz"
1773+--
1735−-- > stripPrefix "foo" "quux" == Nothing
1774+-- >>> stripPrefix "foo" "quux"
1775+-- Nothing
1736--
1776--
1737-- This is particularly useful with the @ViewPatterns@ extension to
1777-- This is particularly useful with the @ViewPatterns@ extension to
1738-- GHC, as follows:
1778-- GHC, as follows:
@@ -1757,9 +1797,14 @@ stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1757--
1797--
1758-- Examples:
1798-- Examples:
1759--
1799--
1760−-- > commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux")
1800+-- >>> commonPrefixes "foobar" "fooquux"
1801+-- Just ("foo","bar","quux")
1802+--
1761−-- > commonPrefixes "veeble" "fetzer" == Nothing
1803+-- >>> commonPrefixes "veeble" "fetzer"
1804+-- Nothing
1805+--
1762−-- > commonPrefixes "" "baz" == Nothing
1806+-- >>> commonPrefixes "" "baz"
1807+-- Nothing
1763commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1808commonPrefixes :: Text -> Text -> Maybe (Text,Text,Text)
1764commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1809commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1765 where
1810 where
@@ -1776,9 +1821,14 @@ commonPrefixes t0@(Text arr0 off0 len0) t1@(Text arr1 off1 len1) = go 0 0
1776--
1821--
1777-- Examples:
1822-- Examples:
1778--
1823--
1779−-- > stripSuffix "bar" "foobar" == Just "foo"
1824+-- >>> stripSuffix "bar" "foobar"
1825+-- Just "foo"
1826+--
1780−-- > stripSuffix "" "baz" == Just "baz"
1827+-- >>> stripSuffix "" "baz"
1828+-- Just "baz"
1829+--
1781−-- > stripSuffix "foo" "quux" == Nothing
1830+-- >>> stripSuffix "foo" "quux"
1831+-- Nothing
1782--
1832--
1783-- This is particularly useful with the @ViewPatterns@ extension to
1833-- This is particularly useful with the @ViewPatterns@ extension to
1784-- GHC, as follows:
1834-- GHC, as follows:
@@ -1825,3 +1875,12 @@ copy (Text arr off len) = Text (A.run go) 0 len
1825 marr <- A.new len
1875 marr <- A.new len
1826 A.copyI marr 0 arr off len
1876 A.copyI marr 0 arr off len
1827 return marr
1877 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