on master help sign in
Clarify the precondition for splitOn (gh-92)
commit5ab6fab4cc0a53f3b51eef57af669ff1071ee188 authorBryan O'Sullivan <bos@serpentine.com> dateFri 12 Dec 2014, 11:46 (12 years ago) parent666383e
2 files changed+36−15
Data/Text.hs+18−7 Data/Text/Lazy.hs+18−8
Data/Text.hs+18−7
@@ -689,9 +689,13 @@ reverse t = S.reverse (stream t)
689689--
690690-- In (unlikely) bad cases, this function's time complexity degrades
691691-- towards /O(n*m)/.
692−replace :: Text -- ^ @needle@ to search for
693− -> Text -- ^ @replacement@ to replace @needle@ with
694− -> Text -- ^ @haystack@ in which to search
692+replace :: Text
693+ -- ^ @needle@ to search for. If this string is empty, an
694+ -- error will occur.
695+ -> Text
696+ -- ^ @replacement@ to replace @needle@ with.
697+ -> Text
698+ -- ^ @haystack@ in which to search.
695699 -> Text
696700replace needle@(Text _ _ neeLen)
697701 (Text repArr repOff repLen)
@@ -1299,9 +1303,9 @@ tails t | null t = [empty]
12991303-- copies to create substrings; they just construct new 'Text's that
13001304-- are slices of the original.
13011305
1302−-- | /O(m+n)/ Break a 'Text' into pieces separated by the first
1303−-- 'Text' argument, consuming the delimiter. An empty delimiter is
1304−-- invalid, and will cause an error to be raised.
1306+-- | /O(m+n)/ Break a 'Text' into pieces separated by the first 'Text'
1307+-- argument (which cannot be empty), consuming the delimiter. An empty
1308+-- delimiter is invalid, and will cause an error to be raised.
13051309--
13061310-- Examples:
13071311--
@@ -1314,9 +1318,16 @@ tails t | null t = [empty]
13141318-- > intercalate s . splitOn s == id
13151319-- > splitOn (singleton c) == split (==c)
13161320--
1321+-- (Note: the string @s@ to split on above cannot be empty.)
1322+--
13171323-- In (unlikely) bad cases, this function's time complexity degrades
13181324-- towards /O(n*m)/.
1319−splitOn :: Text -> Text -> [Text]
1325+splitOn :: Text
1326+ -- ^ String to split on. If this string is empty, an error
1327+ -- will occur.
1328+ -> Text
1329+ -- ^ Input text.
1330+ -> [Text]
13201331splitOn pat@(Text _ _ l) src@(Text arr off len)
13211332 | l <= 0 = emptyError "splitOn"
13221333 | isSingleton pat = split (== unsafeHead pat) src
Data/Text/Lazy.hs+18−8
@@ -686,9 +686,13 @@ reverse = rev Empty
686686--
687687-- In (unlikely) bad cases, this function's time complexity degrades
688688-- towards /O(n*m)/.
689−replace :: Text -- ^ Text to search for
690− -> Text -- ^ Replacement text
691− -> Text -- ^ Input text
689+replace :: Text
690+ -- ^ @needle@ to search for. If this string is empty, an
691+ -- error will occur.
692+ -> Text
693+ -- ^ @replacement@ to replace @needle@ with.
694+ -> Text
695+ -- ^ @haystack@ in which to search.
692696 -> Text
693697replace s d = intercalate d . splitOn s
694698{-# INLINE replace #-}
@@ -1299,9 +1303,10 @@ tails ts@(Chunk t ts')
12991303-- copies to create substrings; they just construct new 'Text's that
13001304-- are slices of the original.
13011305
1302−-- | /O(m+n)/ Break a 'Text' into pieces separated by the first
1303−-- 'Text' argument, consuming the delimiter. An empty delimiter is
1304−-- invalid, and will cause an error to be raised.
1306+-- | /O(m+n)/ Break a 'Text' into pieces separated by the first 'Text'
1307+-- argument (which cannot be an empty string), consuming the
1308+-- delimiter. An empty delimiter is invalid, and will cause an error
1309+-- to be raised.
13051310--
13061311-- Examples:
13071312--
@@ -1314,13 +1319,18 @@ tails ts@(Chunk t ts')
13141319-- > intercalate s . splitOn s == id
13151320-- > splitOn (singleton c) == split (==c)
13161321--
1322+-- (Note: the string @s@ to split on above cannot be empty.)
1323+--
13171324-- This function is strict in its first argument, and lazy in its
13181325-- second.
13191326--
13201327-- In (unlikely) bad cases, this function's time complexity degrades
13211328-- towards /O(n*m)/.
1322−splitOn :: Text -- ^ Text to split on
1323− -> Text -- ^ Input text
1329+splitOn :: Text
1330+ -- ^ String to split on. If this string is empty, an error
1331+ -- will occur.
1332+ -> Text
1333+ -- ^ Input text.
13241334 -> [Text]
13251335splitOn pat src
13261336 | null pat = emptyError "splitOn"