on master help sign in
Implement split and splitWith

--HG-- extra : convert_revision : b480fa0482b72d62933619d1af841ca30facc9b8

commit3b523ff32558426321ced7e2e4c3858fe9b6937c authorBryan O'Sullivan <bos@serpentine.com> dateTue 10 Feb 2009, 08:03 (18 years ago) parent4a94bd4
2 files changed+54−2
Data/Text.hs+39−2 tests/Properties.hs+15
Data/Text.hs+39−2
@@ -104,8 +104,8 @@ module Data.Text
104104 , tails
105105
106106 -- ** Breaking into many substrings
107− -- , split
108− -- , splitWith
107+ , split
108+ , splitWith
109109 -- , breakSubstring
110110
111111 -- ** Breaking into lines and words
@@ -701,6 +701,43 @@ tails :: Text -> [Text]
701701tails t | null t = [empty]
702702 | otherwise = t : tails (tail t)
703703
704+-- | /O(n)/ Break a 'Text' into pieces separated by the 'Char'
705+-- argument, consuming the delimiter. I.e.
706+--
707+-- > split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
708+-- > split 'a' "aXaXaXa" == ["","X","X","X",""]
709+-- > split 'x' "x" == ["",""]
710+--
711+-- and
712+--
713+-- > intercalate (singleton c) . split c == id
714+-- > split == splitWith . (==)
715+--
716+-- As for all splitting functions in this library, this function does
717+-- not copy the substrings, it just constructs new 'Text's that are
718+-- slices of the original.
719+split :: Char -> Text -> [Text]
720+split c = splitWith (==c)
721+{-# INLINE split #-}
722+
723+-- | /O(n)/ Splits a 'Text' into components delimited by separators,
724+-- where the predicate returns True for a separator element. The
725+-- resulting components do not contain the separators. Two adjacent
726+-- separators result in an empty component in the output. eg.
727+--
728+-- > splitWith (=='a') "aabbaca" == ["","","bb","c",""]
729+-- > splitWith (=='a') [] == []
730+splitWith :: (Char -> Bool) -> Text -> [Text]
731+splitWith p = loop
732+ where loop t@(Text arr off len)
733+ | null t = [empty]
734+ | otherwise = text arr off n : rest
735+ where n = findAIndexOrEnd p t
736+ m = n + iter_ t n
737+ rest | n >= len = []
738+ | otherwise = loop (text arr (off+m) (len-m))
739+{-# INLINE splitWith #-}
740+
704741-- ----------------------------------------------------------------------------
705742-- * Searching
706743
tests/Properties.hs+15
@@ -123,6 +123,18 @@ prop_groupBy p = L.groupBy p `eqP` (map unpack . T.groupBy p)
123123prop_inits = L.inits `eqP` (map unpack . T.inits)
124124prop_tails = L.tails `eqP` (map unpack . T.tails)
125125
126+prop_split_i c = id `eq` (T.intercalate (T.singleton c) . T.split c)
127+
128+{-
129+prop_splitWith p = splitWith p `eqP` (map unpack . T.splitWith p)
130+
131+splitWith _ [] = [""]
132+splitWith p xs = h : splitWith p (whale t)
133+ where (h,t) = break p xs
134+ whale (_:xs) = xs
135+ whale xs = xs
136+-}
137+
126138prop_lines = L.lines `eqP` (map unpack . T.lines)
127139prop_words = L.words `eqP` (map unpack . T.words)
128140prop_unlines = L.unlines `eq` (unpack . T.unlines . map pack)
@@ -215,6 +227,9 @@ tests = [
215227 ("prop_inits", mytest prop_inits),
216228 ("prop_tails", mytest prop_tails),
217229
230+ ("prop_split_i", mytest prop_split_i),
231+--("prop_splitWith", mytest prop_splitWith),
232+
218233 ("prop_lines", mytest prop_lines),
219234 ("prop_words", mytest prop_words),
220235 ("prop_unlines", mytest prop_unlines),