on master help sign in
Rename split to bytestring-like name
6fa1d91Bryan O'Sullivan
Data/Text.hs+20−20
@@ -128,8 +128,8 @@ module Data.Text
128
128
129 -- ** Breaking into many substrings
129 -- ** Breaking into many substrings
130 -- $split
130 -- $split
131+ , splitOn
131 , split
132 , split
132− , splitBy
133 , chunksOf
133 , chunksOf
134
134
135 -- ** Breaking into lines and words
135 -- ** Breaking into lines and words
@@ -554,7 +554,7 @@ replace :: Text -- ^ Text to search for
554 -> Text -- ^ Replacement text
554 -> Text -- ^ Replacement text
555 -> Text -- ^ Input text
555 -> Text -- ^ Input text
556 -> Text
556 -> Text
557−replace s d = intercalate d . split s
557+replace s d = intercalate d . splitOn s
558{-# INLINE replace #-}
558{-# INLINE replace #-}
559
559
560-- ----------------------------------------------------------------------------
560-- ----------------------------------------------------------------------------
@@ -1075,30 +1075,30 @@ tails t | null t = [empty]
1075--
1075--
1076-- Examples:
1076-- Examples:
1077--
1077--
1078−-- > split "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1078+-- > splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1079−-- > split "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1079+-- > splitOn "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1080−-- > split "x" "x" == ["",""]
1080+-- > splitOn "x" "x" == ["",""]
1081--
1081--
1082-- and
1082-- and
1083--
1083--
1084−-- > intercalate s . split s == id
1084+-- > intercalate s . splitOn s == id
1085−-- > split (singleton c) == splitBy (==c)
1085+-- > splitOn (singleton c) == split (==c)
1086--
1086--
1087-- In (unlikely) bad cases, this function's time complexity degrades
1087-- In (unlikely) bad cases, this function's time complexity degrades
1088-- towards /O(n*m)/.
1088-- towards /O(n*m)/.
1089−split :: Text -> Text -> [Text]
1089+splitOn :: Text -> Text -> [Text]
1090−split pat@(Text _ _ l) src@(Text arr off len)
1090+splitOn pat@(Text _ _ l) src@(Text arr off len)
1091− | l <= 0 = emptyError "split"
1091+ | l <= 0 = emptyError "splitOn"
1092− | isSingleton pat = splitBy (== unsafeHead pat) src
1092+ | isSingleton pat = split (== unsafeHead pat) src
1093 | otherwise = go 0 (indices pat src)
1093 | otherwise = go 0 (indices pat src)
1094 where
1094 where
1095 go !s (x:xs) = textP arr (s+off) (x-s) : go (x+l) xs
1095 go !s (x:xs) = textP arr (s+off) (x-s) : go (x+l) xs
1096 go s _ = [textP arr (s+off) (len-s)]
1096 go s _ = [textP arr (s+off) (len-s)]
1097−{-# INLINE [1] split #-}
1097+{-# INLINE [1] splitOn #-}
1098
1098
1099{-# RULES
1099{-# RULES
1100−"TEXT split/singleton -> splitBy/==" [~1] forall c t.
1100+"TEXT splitOn/singleton -> split/==" [~1] forall c t.
1101− split (singleton c) t = splitBy (==c) t
1101+ splitOn (singleton c) t = split (==c) t
1102 #-}
1102 #-}
1103
1103
1104-- | /O(n)/ Splits a 'Text' into components delimited by separators,
1104-- | /O(n)/ Splits a 'Text' into components delimited by separators,
@@ -1106,15 +1106,15 @@ split pat@(Text _ _ l) src@(Text arr off len)
1106-- resulting components do not contain the separators. Two adjacent
1106-- resulting components do not contain the separators. Two adjacent
1107-- separators result in an empty component in the output. eg.
1107-- separators result in an empty component in the output. eg.
1108--
1108--
1109−-- > splitBy (=='a') "aabbaca" == ["","","bb","c",""]
1109+-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1110−-- > splitBy (=='a') "" == [""]
1110+-- > split (=='a') "" == [""]
1111−splitBy :: (Char -> Bool) -> Text -> [Text]
1111+split :: (Char -> Bool) -> Text -> [Text]
1112−splitBy _ t@(Text _off _arr 0) = [t]
1112+split _ t@(Text _off _arr 0) = [t]
1113−splitBy p t = loop t
1113+split p t = loop t
1114 where loop s | null s' = [l]
1114 where loop s | null s' = [l]
1115 | otherwise = l : loop (unsafeTail s')
1115 | otherwise = l : loop (unsafeTail s')
1116 where (l, s') = break p s
1116 where (l, s') = break p s
1117−{-# INLINE splitBy #-}
1117+{-# INLINE split #-}
1118
1118
1119-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
1119-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
1120-- element may be shorter than the other chunks, depending on the
1120-- element may be shorter than the other chunks, depending on the
Data/Text/Lazy.hs+25−25
@@ -133,8 +133,8 @@ module Data.Text.Lazy
133
133
134 -- ** Breaking into many substrings
134 -- ** Breaking into many substrings
135 -- $split
135 -- $split
136+ , splitOn
136 , split
137 , split
137− , splitBy
138 , chunksOf
138 , chunksOf
139 -- , breakSubstring
139 -- , breakSubstring
140
140
@@ -555,7 +555,7 @@ replace :: Text -- ^ Text to search for
555 -> Text -- ^ Replacement text
555 -> Text -- ^ Replacement text
556 -> Text -- ^ Input text
556 -> Text -- ^ Input text
557 -> Text
557 -> Text
558−replace s d = intercalate d . split s
558+replace s d = intercalate d . splitOn s
559{-# INLINE replace #-}
559{-# INLINE replace #-}
560
560
561-- ----------------------------------------------------------------------------
561-- ----------------------------------------------------------------------------
@@ -1104,37 +1104,37 @@ tails ts@(Chunk t ts')
1104--
1104--
1105-- Examples:
1105-- Examples:
1106--
1106--
1107−-- > split "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1107+-- > splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1108−-- > split "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1108+-- > splitOn "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1109−-- > split "x" "x" == ["",""]
1109+-- > splitOn "x" "x" == ["",""]
1110--
1110--
1111-- and
1111-- and
1112--
1112--
1113−-- > intercalate s . split s == id
1113+-- > intercalate s . splitOn s == id
1114−-- > split (singleton c) == splitBy (==c)
1114+-- > splitOn (singleton c) == split (==c)
1115--
1115--
1116-- This function is strict in its first argument, and lazy in its
1116-- This function is strict in its first argument, and lazy in its
1117-- second.
1117-- second.
1118--
1118--
1119-- In (unlikely) bad cases, this function's time complexity degrades
1119-- In (unlikely) bad cases, this function's time complexity degrades
1120-- towards /O(n*m)/.
1120-- towards /O(n*m)/.
1121−split :: Text -- ^ Text to split on
1121+splitOn :: Text -- ^ Text to split on
1122− -> Text -- ^ Input text
1122+ -> Text -- ^ Input text
1123− -> [Text]
1123+ -> [Text]
1124−split pat src
1124+splitOn pat src
1125− | null pat = emptyError "split"
1125+ | null pat = emptyError "splitOn"
1126− | isSingleton pat = splitBy (== head pat) src
1126+ | isSingleton pat = split (== head pat) src
1127 | otherwise = go 0 (indices pat src) src
1127 | otherwise = go 0 (indices pat src) src
1128 where
1128 where
1129 go _ [] cs = [cs]
1129 go _ [] cs = [cs]
1130 go !i (x:xs) cs = let h :*: t = splitAtWord (x-i) cs
1130 go !i (x:xs) cs = let h :*: t = splitAtWord (x-i) cs
1131 in h : go (x+l) xs (dropWords l t)
1131 in h : go (x+l) xs (dropWords l t)
1132 l = foldlChunks (\a (T.Text _ _ b) -> a + fromIntegral b) 0 pat
1132 l = foldlChunks (\a (T.Text _ _ b) -> a + fromIntegral b) 0 pat
1133−{-# INLINE [1] split #-}
1133+{-# INLINE [1] splitOn #-}
1134
1134
1135{-# RULES
1135{-# RULES
1136−"LAZY TEXT split/singleton -> splitBy/==" [~1] forall c t.
1136+"LAZY TEXT splitOn/singleton -> split/==" [~1] forall c t.
1137− split (singleton c) t = splitBy (==c) t
1137+ splitOn (singleton c) t = split (==c) t
1138 #-}
1138 #-}
1139
1139
1140-- | /O(n)/ Splits a 'Text' into components delimited by separators,
1140-- | /O(n)/ Splits a 'Text' into components delimited by separators,
@@ -1142,16 +1142,16 @@ split pat src
1142-- resulting components do not contain the separators. Two adjacent
1142-- resulting components do not contain the separators. Two adjacent
1143-- separators result in an empty component in the output. eg.
1143-- separators result in an empty component in the output. eg.
1144--
1144--
1145−-- > splitBy (=='a') "aabbaca" == ["","","bb","c",""]
1145+-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1146−-- > splitBy (=='a') [] == [""]
1146+-- > split (=='a') [] == [""]
1147−splitBy :: (Char -> Bool) -> Text -> [Text]
1147+split :: (Char -> Bool) -> Text -> [Text]
1148−splitBy _ Empty = [Empty]
1148+split _ Empty = [Empty]
1149−splitBy p (Chunk t0 ts0) = comb [] (T.splitBy p t0) ts0
1149+split p (Chunk t0 ts0) = comb [] (T.split p t0) ts0
1150 where comb acc (s:[]) Empty = revChunks (s:acc) : []
1150 where comb acc (s:[]) Empty = revChunks (s:acc) : []
1151− comb acc (s:[]) (Chunk t ts) = comb (s:acc) (T.splitBy p t) ts
1151+ comb acc (s:[]) (Chunk t ts) = comb (s:acc) (T.split p t) ts
1152 comb acc (s:ss) ts = revChunks (s:acc) : comb [] ss ts
1152 comb acc (s:ss) ts = revChunks (s:acc) : comb [] ss ts
1153− comb _ [] _ = impossibleError "splitBy"
1153+ comb _ [] _ = impossibleError "split"
1154−{-# INLINE splitBy #-}
1154+{-# INLINE split #-}
1155
1155
1156-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
1156-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
1157-- element may be shorter than the other chunks, depending on the
1157-- element may be shorter than the other chunks, depending on the
@@ -1178,7 +1178,7 @@ lines t = let (l,t') = break ((==) '\n') t
1178-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1178-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
1179-- representing white space.
1179-- representing white space.
1180words :: Text -> [Text]
1180words :: Text -> [Text]
1181−words = L.filter (not . null) . splitBy isSpace
1181+words = L.filter (not . null) . split isSpace
1182{-# INLINE words #-}
1182{-# INLINE words #-}
1183
1183
1184-- | /O(n)/ Joins lines, after appending a terminating newline to
1184-- | /O(n)/ Joins lines, after appending a terminating newline to
tests/Properties.hs+29−29
@@ -265,13 +265,13 @@ t_reverse = L.reverse `eqP` (unpackS . T.reverse)
265tl_reverse = L.reverse `eqP` (unpackS . TL.reverse)
265tl_reverse = L.reverse `eqP` (unpackS . TL.reverse)
266t_reverse_short n = L.reverse `eqP` (unpackS . S.reverse . shorten n . S.stream)
266t_reverse_short n = L.reverse `eqP` (unpackS . S.reverse . shorten n . S.stream)
267
267
268−t_replace s d = (L.intercalate d . split s) `eqP`
268+t_replace s d = (L.intercalate d . splitOn s) `eqP`
269 (unpackS . T.replace (T.pack s) (T.pack d))
269 (unpackS . T.replace (T.pack s) (T.pack d))
270−tl_replace s d = (L.intercalate d . split s) `eqP`
270+tl_replace s d = (L.intercalate d . splitOn s) `eqP`
271 (unpackS . TL.replace (TL.pack s) (TL.pack d))
271 (unpackS . TL.replace (TL.pack s) (TL.pack d))
272
272
273−split :: (Eq a) => [a] -> [a] -> [[a]]
273+splitOn :: (Eq a) => [a] -> [a] -> [[a]]
274−split pat src0
274+splitOn pat src0
275 | l == 0 = error "empty"
275 | l == 0 = error "empty"
276 | otherwise = go src0
276 | otherwise = go src0
277 where
277 where
@@ -523,21 +523,21 @@ sl_filterCount c = (L.genericLength . L.filter (==c)) `eqP` SL.countChar c
523t_findCount s = (L.length . T.breakOnAll s) `eq` T.count s
523t_findCount s = (L.length . T.breakOnAll s) `eq` T.count s
524tl_findCount s = (L.genericLength . TL.breakOnAll s) `eq` TL.count s
524tl_findCount s = (L.genericLength . TL.breakOnAll s) `eq` TL.count s
525
525
526−t_split_split s = (T.split s `eq` Slow.split s) . T.intercalate s
526+t_splitOn_split s = (T.splitOn s `eq` Slow.splitOn s) . T.intercalate s
527−tl_split_split s = ((TL.split (TL.fromStrict s) . TL.fromStrict) `eq`
527+tl_splitOn_split s = ((TL.splitOn (TL.fromStrict s) . TL.fromStrict) `eq`
528− (map TL.fromStrict . T.split s)) . T.intercalate s
528+ (map TL.fromStrict . T.splitOn s)) . T.intercalate s
529−t_split_i (NotEmpty t) = id `eq` (T.intercalate t . T.split t)
529+t_splitOn_i (NotEmpty t) = id `eq` (T.intercalate t . T.splitOn t)
530−tl_split_i (NotEmpty t) = id `eq` (TL.intercalate t . TL.split t)
530+tl_splitOn_i (NotEmpty t) = id `eq` (TL.intercalate t . TL.splitOn t)
531−
531+
532−t_splitBy p = splitBy p `eqP` (map unpackS . T.splitBy p)
532+t_split p = split p `eqP` (map unpackS . T.split p)
533−t_splitBy_count c = (L.length . T.splitBy (==c)) `eq`
533+t_split_count c = (L.length . T.split (==c)) `eq`
534− ((1+) . T.count (T.singleton c))
534+ ((1+) . T.count (T.singleton c))
535−t_splitBy_split c = T.splitBy (==c) `eq` T.split (T.singleton c)
535+t_split_splitOn c = T.split (==c) `eq` T.splitOn (T.singleton c)
536−tl_splitBy p = splitBy p `eqP` (map unpackS . TL.splitBy p)
536+tl_split p = split p `eqP` (map unpackS . TL.split p)
537−
537+
538−splitBy :: (a -> Bool) -> [a] -> [[a]]
538+split :: (a -> Bool) -> [a] -> [[a]]
539−splitBy _ [] = [[]]
539+split _ [] = [[]]
540−splitBy p xs = loop xs
540+split p xs = loop xs
541 where loop s | null s' = [l]
541 where loop s | null s' = [l]
542 | otherwise = l : loop (tail s')
542 | otherwise = l : loop (tail s')
543 where (l, s') = break p s
543 where (l, s') = break p s
@@ -613,8 +613,8 @@ tl_index s = forAll (choose (-l,l*2))
613 where l = L.length s
613 where l = L.length s
614
614
615t_findIndex p = L.findIndex p `eqP` T.findIndex p
615t_findIndex p = L.findIndex p `eqP` T.findIndex p
616−t_count (NotEmpty t) = (subtract 1 . L.length . T.split t) `eq` T.count t
616+t_count (NotEmpty t) = (subtract 1 . L.length . T.splitOn t) `eq` T.count t
617−tl_count (NotEmpty t) = (subtract 1 . L.genericLength . TL.split t) `eq`
617+tl_count (NotEmpty t) = (subtract 1 . L.genericLength . TL.splitOn t) `eq`
618 TL.count t
618 TL.count t
619t_zip s = L.zip s `eqP` T.zip (packS s)
619t_zip s = L.zip s `eqP` T.zip (packS s)
620tl_zip s = L.zip s `eqP` TL.zip (packS s)
620tl_zip s = L.zip s `eqP` TL.zip (packS s)
@@ -1080,14 +1080,14 @@ tests = [
1080 testProperty "sl_filterCount" sl_filterCount,
1080 testProperty "sl_filterCount" sl_filterCount,
1081 testProperty "t_findCount" t_findCount,
1081 testProperty "t_findCount" t_findCount,
1082 testProperty "tl_findCount" tl_findCount,
1082 testProperty "tl_findCount" tl_findCount,
1083− testProperty "t_split_split" t_split_split,
1083+ testProperty "t_splitOn_split" t_splitOn_split,
1084− testProperty "tl_split_split" tl_split_split,
1084+ testProperty "tl_splitOn_split" tl_splitOn_split,
1085− testProperty "t_split_i" t_split_i,
1085+ testProperty "t_splitOn_i" t_splitOn_i,
1086− testProperty "tl_split_i" tl_split_i,
1086+ testProperty "tl_splitOn_i" tl_splitOn_i,
1087− testProperty "t_splitBy" t_splitBy,
1087+ testProperty "t_split" t_split,
1088− testProperty "t_splitBy_count" t_splitBy_count,
1088+ testProperty "t_split_count" t_split_count,
1089− testProperty "t_splitBy_split" t_splitBy_split,
1089+ testProperty "t_split_splitOn" t_split_splitOn,
1090− testProperty "tl_splitBy" tl_splitBy,
1090+ testProperty "tl_split" tl_split,
1091 testProperty "t_chunksOf_same_lengths" t_chunksOf_same_lengths,
1091 testProperty "t_chunksOf_same_lengths" t_chunksOf_same_lengths,
1092 testProperty "t_chunksOf_length" t_chunksOf_length,
1092 testProperty "t_chunksOf_length" t_chunksOf_length,
1093 testProperty "tl_chunksOf" tl_chunksOf
1093 testProperty "tl_chunksOf" tl_chunksOf
tests/SlowFunctions.hs+7−7
@@ -3,7 +3,7 @@
3module SlowFunctions
3module SlowFunctions
4 (
4 (
5 indices
5 indices
6− , split
6+ , splitOn
7 ) where
7 ) where
8
8
9import qualified Data.Text as T
9import qualified Data.Text as T
@@ -23,12 +23,12 @@ indices needle@(Text _narr _noff nlen) haystack@(Text harr hoff hlen)
23 where t = Text harr (hoff+i) (hlen-i)
23 where t = Text harr (hoff+i) (hlen-i)
24 d = iter_ haystack i
24 d = iter_ haystack i
25
25
26−split :: T.Text -- ^ Text to split on
26+splitOn :: T.Text -- ^ Text to split on
27− -> T.Text -- ^ Input text
27+ -> T.Text -- ^ Input text
28− -> [T.Text]
28+ -> [T.Text]
29−split pat src0
29+splitOn pat src0
30− | T.null pat = error "split: empty"
30+ | T.null pat = error "splitOn: empty"
31− | l == 1 = T.splitBy (== (unsafeHead pat)) src0
31+ | l == 1 = T.split (== (unsafeHead pat)) src0
32 | otherwise = go src0
32 | otherwise = go src0
33 where
33 where
34 l = T.length pat
34 l = T.length pat