on master help sign in
Rename split to bytestring-like name
commit6fa1d91f25b6967a67f83b8e9a36e84fe842ea55 authorBryan O'Sullivan <bos@serpentine.com> dateMon 8 Nov 2010, 14:09 (16 years ago) parent15eaf1f
4 files changed+81−81
Data/Text.hs+20−20 Data/Text/Lazy.hs+25−25 tests/Properties.hs+29−29 tests/SlowFunctions.hs+7−7
Data/Text.hs+20−20
@@ -128,8 +128,8 @@ module Data.Text
128128
129129 -- ** Breaking into many substrings
130130 -- $split
131+ , splitOn
131132 , split
132− , splitBy
133133 , chunksOf
134134
135135 -- ** Breaking into lines and words
@@ -554,7 +554,7 @@ replace :: Text -- ^ Text to search for
554554 -> Text -- ^ Replacement text
555555 -> Text -- ^ Input text
556556 -> Text
557−replace s d = intercalate d . split s
557+replace s d = intercalate d . splitOn s
558558{-# INLINE replace #-}
559559
560560-- ----------------------------------------------------------------------------
@@ -1075,30 +1075,30 @@ tails t | null t = [empty]
10751075--
10761076-- Examples:
10771077--
1078−-- > split "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1079−-- > split "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1080−-- > split "x" "x" == ["",""]
1078+-- > splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1079+-- > splitOn "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1080+-- > splitOn "x" "x" == ["",""]
10811081--
10821082-- and
10831083--
1084−-- > intercalate s . split s == id
1085−-- > split (singleton c) == splitBy (==c)
1084+-- > intercalate s . splitOn s == id
1085+-- > splitOn (singleton c) == split (==c)
10861086--
10871087-- In (unlikely) bad cases, this function's time complexity degrades
10881088-- towards /O(n*m)/.
1089−split :: Text -> Text -> [Text]
1090−split pat@(Text _ _ l) src@(Text arr off len)
1091− | l <= 0 = emptyError "split"
1092− | isSingleton pat = splitBy (== unsafeHead pat) src
1089+splitOn :: Text -> Text -> [Text]
1090+splitOn pat@(Text _ _ l) src@(Text arr off len)
1091+ | l <= 0 = emptyError "splitOn"
1092+ | isSingleton pat = split (== unsafeHead pat) src
10931093 | otherwise = go 0 (indices pat src)
10941094 where
10951095 go !s (x:xs) = textP arr (s+off) (x-s) : go (x+l) xs
10961096 go s _ = [textP arr (s+off) (len-s)]
1097−{-# INLINE [1] split #-}
1097+{-# INLINE [1] splitOn #-}
10981098
10991099{-# RULES
1100−"TEXT split/singleton -> splitBy/==" [~1] forall c t.
1101− split (singleton c) t = splitBy (==c) t
1100+"TEXT splitOn/singleton -> split/==" [~1] forall c t.
1101+ splitOn (singleton c) t = split (==c) t
11021102 #-}
11031103
11041104-- | /O(n)/ Splits a 'Text' into components delimited by separators,
@@ -1106,15 +1106,15 @@ split pat@(Text _ _ l) src@(Text arr off len)
11061106-- resulting components do not contain the separators. Two adjacent
11071107-- separators result in an empty component in the output. eg.
11081108--
1109−-- > splitBy (=='a') "aabbaca" == ["","","bb","c",""]
1110−-- > splitBy (=='a') "" == [""]
1111−splitBy :: (Char -> Bool) -> Text -> [Text]
1112−splitBy _ t@(Text _off _arr 0) = [t]
1113−splitBy p t = loop t
1109+-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1110+-- > split (=='a') "" == [""]
1111+split :: (Char -> Bool) -> Text -> [Text]
1112+split _ t@(Text _off _arr 0) = [t]
1113+split p t = loop t
11141114 where loop s | null s' = [l]
11151115 | otherwise = l : loop (unsafeTail s')
11161116 where (l, s') = break p s
1117−{-# INLINE splitBy #-}
1117+{-# INLINE split #-}
11181118
11191119-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
11201120-- 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
133133
134134 -- ** Breaking into many substrings
135135 -- $split
136+ , splitOn
136137 , split
137− , splitBy
138138 , chunksOf
139139 -- , breakSubstring
140140
@@ -555,7 +555,7 @@ replace :: Text -- ^ Text to search for
555555 -> Text -- ^ Replacement text
556556 -> Text -- ^ Input text
557557 -> Text
558−replace s d = intercalate d . split s
558+replace s d = intercalate d . splitOn s
559559{-# INLINE replace #-}
560560
561561-- ----------------------------------------------------------------------------
@@ -1104,37 +1104,37 @@ tails ts@(Chunk t ts')
11041104--
11051105-- Examples:
11061106--
1107−-- > split "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1108−-- > split "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1109−-- > split "x" "x" == ["",""]
1107+-- > splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
1108+-- > splitOn "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
1109+-- > splitOn "x" "x" == ["",""]
11101110--
11111111-- and
11121112--
1113−-- > intercalate s . split s == id
1114−-- > split (singleton c) == splitBy (==c)
1113+-- > intercalate s . splitOn s == id
1114+-- > splitOn (singleton c) == split (==c)
11151115--
11161116-- This function is strict in its first argument, and lazy in its
11171117-- second.
11181118--
11191119-- In (unlikely) bad cases, this function's time complexity degrades
11201120-- towards /O(n*m)/.
1121−split :: Text -- ^ Text to split on
1122− -> Text -- ^ Input text
1123− -> [Text]
1124−split pat src
1125− | null pat = emptyError "split"
1126− | isSingleton pat = splitBy (== head pat) src
1121+splitOn :: Text -- ^ Text to split on
1122+ -> Text -- ^ Input text
1123+ -> [Text]
1124+splitOn pat src
1125+ | null pat = emptyError "splitOn"
1126+ | isSingleton pat = split (== head pat) src
11271127 | otherwise = go 0 (indices pat src) src
11281128 where
11291129 go _ [] cs = [cs]
11301130 go !i (x:xs) cs = let h :*: t = splitAtWord (x-i) cs
11311131 in h : go (x+l) xs (dropWords l t)
11321132 l = foldlChunks (\a (T.Text _ _ b) -> a + fromIntegral b) 0 pat
1133−{-# INLINE [1] split #-}
1133+{-# INLINE [1] splitOn #-}
11341134
11351135{-# RULES
1136−"LAZY TEXT split/singleton -> splitBy/==" [~1] forall c t.
1137− split (singleton c) t = splitBy (==c) t
1136+"LAZY TEXT splitOn/singleton -> split/==" [~1] forall c t.
1137+ splitOn (singleton c) t = split (==c) t
11381138 #-}
11391139
11401140-- | /O(n)/ Splits a 'Text' into components delimited by separators,
@@ -1142,16 +1142,16 @@ split pat src
11421142-- resulting components do not contain the separators. Two adjacent
11431143-- separators result in an empty component in the output. eg.
11441144--
1145−-- > splitBy (=='a') "aabbaca" == ["","","bb","c",""]
1146−-- > splitBy (=='a') [] == [""]
1147−splitBy :: (Char -> Bool) -> Text -> [Text]
1148−splitBy _ Empty = [Empty]
1149−splitBy p (Chunk t0 ts0) = comb [] (T.splitBy p t0) ts0
1145+-- > split (=='a') "aabbaca" == ["","","bb","c",""]
1146+-- > split (=='a') [] == [""]
1147+split :: (Char -> Bool) -> Text -> [Text]
1148+split _ Empty = [Empty]
1149+split p (Chunk t0 ts0) = comb [] (T.split p t0) ts0
11501150 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
11521152 comb acc (s:ss) ts = revChunks (s:acc) : comb [] ss ts
1153− comb _ [] _ = impossibleError "splitBy"
1154−{-# INLINE splitBy #-}
1153+ comb _ [] _ = impossibleError "split"
1154+{-# INLINE split #-}
11551155
11561156-- | /O(n)/ Splits a 'Text' into components of length @k@. The last
11571157-- element may be shorter than the other chunks, depending on the
@@ -1178,7 +1178,7 @@ lines t = let (l,t') = break ((==) '\n') t
11781178-- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
11791179-- representing white space.
11801180words :: Text -> [Text]
1181−words = L.filter (not . null) . splitBy isSpace
1181+words = L.filter (not . null) . split isSpace
11821182{-# INLINE words #-}
11831183
11841184-- | /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)
265265tl_reverse = L.reverse `eqP` (unpackS . TL.reverse)
266266t_reverse_short n = L.reverse `eqP` (unpackS . S.reverse . shorten n . S.stream)
267267
268−t_replace s d = (L.intercalate d . split s) `eqP`
268+t_replace s d = (L.intercalate d . splitOn s) `eqP`
269269 (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`
271271 (unpackS . TL.replace (TL.pack s) (TL.pack d))
272272
273−split :: (Eq a) => [a] -> [a] -> [[a]]
274−split pat src0
273+splitOn :: (Eq a) => [a] -> [a] -> [[a]]
274+splitOn pat src0
275275 | l == 0 = error "empty"
276276 | otherwise = go src0
277277 where
@@ -523,21 +523,21 @@ sl_filterCount c = (L.genericLength . L.filter (==c)) `eqP` SL.countChar c
523523t_findCount s = (L.length . T.breakOnAll s) `eq` T.count s
524524tl_findCount s = (L.genericLength . TL.breakOnAll s) `eq` TL.count s
525525
526−t_split_split s = (T.split s `eq` Slow.split s) . T.intercalate s
527−tl_split_split s = ((TL.split (TL.fromStrict s) . TL.fromStrict) `eq`
528− (map TL.fromStrict . T.split s)) . T.intercalate s
529−t_split_i (NotEmpty t) = id `eq` (T.intercalate t . T.split t)
530−tl_split_i (NotEmpty t) = id `eq` (TL.intercalate t . TL.split t)
531−
532−t_splitBy p = splitBy p `eqP` (map unpackS . T.splitBy p)
533−t_splitBy_count c = (L.length . T.splitBy (==c)) `eq`
534− ((1+) . T.count (T.singleton c))
535−t_splitBy_split c = T.splitBy (==c) `eq` T.split (T.singleton c)
536−tl_splitBy p = splitBy p `eqP` (map unpackS . TL.splitBy p)
537−
538−splitBy :: (a -> Bool) -> [a] -> [[a]]
539−splitBy _ [] = [[]]
540−splitBy p xs = loop xs
526+t_splitOn_split s = (T.splitOn s `eq` Slow.splitOn s) . T.intercalate s
527+tl_splitOn_split s = ((TL.splitOn (TL.fromStrict s) . TL.fromStrict) `eq`
528+ (map TL.fromStrict . T.splitOn s)) . T.intercalate s
529+t_splitOn_i (NotEmpty t) = id `eq` (T.intercalate t . T.splitOn t)
530+tl_splitOn_i (NotEmpty t) = id `eq` (TL.intercalate t . TL.splitOn t)
531+
532+t_split p = split p `eqP` (map unpackS . T.split p)
533+t_split_count c = (L.length . T.split (==c)) `eq`
534+ ((1+) . T.count (T.singleton c))
535+t_split_splitOn c = T.split (==c) `eq` T.splitOn (T.singleton c)
536+tl_split p = split p `eqP` (map unpackS . TL.split p)
537+
538+split :: (a -> Bool) -> [a] -> [[a]]
539+split _ [] = [[]]
540+split p xs = loop xs
541541 where loop s | null s' = [l]
542542 | otherwise = l : loop (tail s')
543543 where (l, s') = break p s
@@ -613,8 +613,8 @@ tl_index s = forAll (choose (-l,l*2))
613613 where l = L.length s
614614
615615t_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
617−tl_count (NotEmpty t) = (subtract 1 . L.genericLength . TL.split t) `eq`
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.splitOn t) `eq`
618618 TL.count t
619619t_zip s = L.zip s `eqP` T.zip (packS s)
620620tl_zip s = L.zip s `eqP` TL.zip (packS s)
@@ -1080,14 +1080,14 @@ tests = [
10801080 testProperty "sl_filterCount" sl_filterCount,
10811081 testProperty "t_findCount" t_findCount,
10821082 testProperty "tl_findCount" tl_findCount,
1083− testProperty "t_split_split" t_split_split,
1084− testProperty "tl_split_split" tl_split_split,
1085− testProperty "t_split_i" t_split_i,
1086− testProperty "tl_split_i" tl_split_i,
1087− testProperty "t_splitBy" t_splitBy,
1088− testProperty "t_splitBy_count" t_splitBy_count,
1089− testProperty "t_splitBy_split" t_splitBy_split,
1090− testProperty "tl_splitBy" tl_splitBy,
1083+ testProperty "t_splitOn_split" t_splitOn_split,
1084+ testProperty "tl_splitOn_split" tl_splitOn_split,
1085+ testProperty "t_splitOn_i" t_splitOn_i,
1086+ testProperty "tl_splitOn_i" tl_splitOn_i,
1087+ testProperty "t_split" t_split,
1088+ testProperty "t_split_count" t_split_count,
1089+ testProperty "t_split_splitOn" t_split_splitOn,
1090+ testProperty "tl_split" tl_split,
10911091 testProperty "t_chunksOf_same_lengths" t_chunksOf_same_lengths,
10921092 testProperty "t_chunksOf_length" t_chunksOf_length,
10931093 testProperty "tl_chunksOf" tl_chunksOf
tests/SlowFunctions.hs+7−7
@@ -3,7 +3,7 @@
33module SlowFunctions
44 (
55 indices
6− , split
6+ , splitOn
77 ) where
88
99import qualified Data.Text as T
@@ -23,12 +23,12 @@ indices needle@(Text _narr _noff nlen) haystack@(Text harr hoff hlen)
2323 where t = Text harr (hoff+i) (hlen-i)
2424 d = iter_ haystack i
2525
26−split :: T.Text -- ^ Text to split on
27− -> T.Text -- ^ Input text
28− -> [T.Text]
29−split pat src0
30− | T.null pat = error "split: empty"
31− | l == 1 = T.splitBy (== (unsafeHead pat)) src0
26+splitOn :: T.Text -- ^ Text to split on
27+ -> T.Text -- ^ Input text
28+ -> [T.Text]
29+splitOn pat src0
30+ | T.null pat = error "splitOn: empty"
31+ | l == 1 = T.split (== (unsafeHead pat)) src0
3232 | otherwise = go src0
3333 where
3434 l = T.length pat