Commits

Bryan O'Sullivan committed 2ffea40

Rename splitWith to splitBy.

Comments (0)

Files changed (4)

     , split
     , splitTimes
     , splitTimesEnd
-    , splitWith
+    , splitBy
     , chunksOf
 
     -- ** Breaking into lines and words
 -- and
 --
 -- > intercalate s . split s         == id
--- > split (singleton c)             == splitWith (==c)
+-- > split (singleton c)             == splitBy (==c)
 --
 -- In (unlikely) bad cases, this function's time complexity
 -- degenerates towards /O(n*m)/.
 split :: Text -> Text -> [Text]
 split pat@(Text _ _ l) src@(Text arr off len)
     | l <= 0          = emptyError "split"
-    | isSingleton pat = splitWith (== unsafeHead pat) src
+    | isSingleton pat = splitBy (== unsafeHead pat) src
     | otherwise       = go 0 (indices pat src)
   where
     go !s (x:xs) =  textP arr (s+off) (x-s) : go (x+l) xs
 {-# INLINE [1] split #-}
 
 {-# RULES
-"TEXT split/singleton -> splitWith/==" [~1] forall c t.
-    split (singleton c) t = splitWith (==c) t
+"TEXT split/singleton -> splitBy/==" [~1] forall c t.
+    split (singleton c) t = splitBy (==c) t
   #-}
 
 -- | /O(m+n)/ Break a 'Text' into pieces at most @k@ times,
 -- resulting components do not contain the separators.  Two adjacent
 -- separators result in an empty component in the output.  eg.
 --
--- > splitWith (=='a') "aabbaca" == ["","","bb","c",""]
--- > splitWith (=='a') ""        == [""]
-splitWith :: (Char -> Bool) -> Text -> [Text]
-splitWith _ t@(Text _off _arr 0) = [t]
-splitWith p t = loop t
+-- > splitBy (=='a') "aabbaca" == ["","","bb","c",""]
+-- > splitBy (=='a') ""        == [""]
+splitBy :: (Char -> Bool) -> Text -> [Text]
+splitBy _ t@(Text _off _arr 0) = [t]
+splitBy p t = loop t
     where loop s | null s'   = [l]
                  | otherwise = l : loop (unsafeTail s')
               where (l, s') = break p s
-{-# INLINE splitWith #-}
+{-# INLINE splitBy #-}
 
 -- | /O(n)/ Splits a 'Text' into components of length @k@.  The last
 -- element may be shorter than the other chunks, depending on the

Data/Text/Lazy.hs

     , split
     , splitTimes
     , splitTimesEnd
-    , splitWith
+    , splitBy
     , chunksOf
     -- , breakSubstring
 
 -- and
 --
 -- > intercalate s . split s         == id
--- > split (singleton c)             == splitWith (==c)
+-- > split (singleton c)             == splitBy (==c)
 split :: Text                   -- ^ Text to split on
       -> Text                   -- ^ Input text
       -> [Text]
 split pat src0
     | l == 0    = emptyError "split"
-    | l == 1    = splitWith (== (head pat)) src0
+    | l == 1    = splitBy (== (head pat)) src0
     | otherwise = go src0
   where
     l      = length pat
 {-# INLINE [1] split #-}
 
 {-# RULES
-"LAZY TEXT split/singleton -> splitWith/==" [~1] forall c t.
-    split (singleton c) t = splitWith (==c) t
+"LAZY TEXT split/singleton -> splitBy/==" [~1] forall c t.
+    split (singleton c) t = splitBy (==c) t
   #-}
 
 -- | /O(m)*O(n)/ Break a 'Text' into pieces at most @k@ times,
 -- resulting components do not contain the separators.  Two adjacent
 -- separators result in an empty component in the output.  eg.
 --
--- > splitWith (=='a') "aabbaca" == ["","","bb","c",""]
--- > splitWith (=='a') []        == [""]
-splitWith :: (Char -> Bool) -> Text -> [Text]
-splitWith _ Empty = [Empty]
-splitWith p (Chunk t0 ts0) = comb [] (T.splitWith p t0) ts0
+-- > splitBy (=='a') "aabbaca" == ["","","bb","c",""]
+-- > splitBy (=='a') []        == [""]
+splitBy :: (Char -> Bool) -> Text -> [Text]
+splitBy _ Empty = [Empty]
+splitBy p (Chunk t0 ts0) = comb [] (T.splitBy p t0) ts0
   where comb acc (s:[]) Empty        = revChunks (s:acc) : []
-        comb acc (s:[]) (Chunk t ts) = comb (s:acc) (T.splitWith p t) ts
+        comb acc (s:[]) (Chunk t ts) = comb (s:acc) (T.splitBy p t) ts
         comb acc (s:ss) ts           = revChunks (s:acc) : comb [] ss ts
-        comb _   []     _            = impossibleError "splitWith"
-{-# INLINE splitWith #-}
+        comb _   []     _            = impossibleError "splitBy"
+{-# INLINE splitBy #-}
 
 -- | /O(n)/ Splits a 'Text' into components of length @k@.  The last
 -- element may be shorter than the other chunks, depending on the
 -- | /O(n)/ Breaks a 'Text' up into a list of words, delimited by 'Char's
 -- representing white space.
 words :: Text -> [Text]
-words = L.filter (not . null) . splitWith isSpace
+words = L.filter (not . null) . splitBy isSpace
 {-# INLINE words #-}
 
 -- | /O(n)/ Joins lines, after appending a terminating newline to

tests/Properties.hs

 t_splitTimesEnd_split (NotEmpty t) = T.splitTimesEnd maxBound t `eq` T.split t
 tl_splitTimesEnd_split (NotEmpty t) = TL.splitTimesEnd maxBound t `eq` TL.split t
 
-t_splitWith p     = splitWith p `eqP` (map unpackS . T.splitWith p)
-t_splitWith_count c = (L.length . T.splitWith (==c)) `eq` ((1+) . T.count (T.singleton c))
-t_splitWith_split c = T.splitWith (==c) `eq` T.split (T.singleton c)
-tl_splitWith p    = splitWith p `eqP` (map unpackS . TL.splitWith p)
+t_splitBy p       = splitBy p `eqP` (map unpackS . T.splitBy p)
+t_splitBy_count c = (L.length . T.splitBy (==c)) `eq` ((1+) . T.count (T.singleton c))
+t_splitBy_split c = T.splitBy (==c) `eq` T.split (T.singleton c)
+tl_splitBy p      = splitBy p `eqP` (map unpackS . TL.splitBy p)
 
-splitWith :: (a -> Bool) -> [a] -> [[a]]
-splitWith _ [] =  [[]]
-splitWith p xs = loop xs
+splitBy :: (a -> Bool) -> [a] -> [[a]]
+splitBy _ [] =  [[]]
+splitBy p xs = loop xs
     where loop s | null s'   = [l]
                  | otherwise = l : loop (tail s')
               where (l, s') = break p s
       testProperty "t_splitTimesEnd_split" t_splitTimesEnd_split,
       testProperty "tl_splitTimesEnd_split" tl_splitTimesEnd_split,
       testProperty "tl_split_i" tl_split_i,
-      testProperty "t_splitWith" t_splitWith,
-      testProperty "t_splitWith_count" t_splitWith_count,
-      testProperty "t_splitWith_split" t_splitWith_split,
-      testProperty "tl_splitWith" tl_splitWith,
+      testProperty "t_splitBy" t_splitBy,
+      testProperty "t_splitBy_count" t_splitBy_count,
+      testProperty "t_splitBy_split" t_splitBy_split,
+      testProperty "tl_splitBy" tl_splitBy,
       testProperty "t_chunksOf_same_lengths" t_chunksOf_same_lengths,
       testProperty "t_chunksOf_length" t_chunksOf_length,
       testProperty "tl_chunksOf" tl_chunksOf

tests/SlowFunctions.hs

       -> [Text]
 split pat src0
     | T.null pat  = error "split: empty"
-    | l == 1      = T.splitWith (== (unsafeHead pat)) src0
+    | l == 1      = T.splitBy (== (unsafeHead pat)) src0
     | otherwise   = go src0
   where
     l      = T.length pat