Commits

Bryan O'Sullivan committed d0e8a51

Get rid of splitChar.

  • Participants
  • Parent commits 3521997

Comments (0)

Files changed (3)

File Data/Text.hs

     , split
     , splitTimes
     , splitTimesEnd
-    , splitChar
     , splitWith
     , chunksOf
 
 tails t | null t    = [empty]
         | otherwise = t : tails (unsafeTail t)
 
--- | /O(n)/ Break a 'Text' into pieces separated by the 'Char'
--- argument, consuming the delimiter. I.e.
---
--- > splitChar '\n' "a\nb\nd\ne" == ["a","b","d","e"]
--- > splitChar 'a'  "aXaXaXa"    == ["","X","X","X",""]
--- > splitChar 'x'  "x"          == ["",""]
--- 
--- and
---
--- > intercalate (singleton c) . splitChar c == id
--- > splitChar                               == splitWith . (==)
-splitChar :: Char -> Text -> [Text]
-splitChar c = splitWith (==c)
-{-# INLINE splitChar #-}
-
 -- $split
 --
 -- Splitting functions in this library do not perform character-wise
       -> [Text]
 split pat src0
     | l == 0    = [src0]
+    | l == 1    = splitWith (== (head pat)) src0
     | otherwise = go src0
   where
-    l         = length pat
+    l      = length pat
     go src = search 0 src
       where
         search !n !s
             | null s             = [src]      -- not found
             | pat `isPrefixOf` s = take n src : go (drop l s)
             | otherwise          = search (n+1) (unsafeTail s)
-{-# INLINE split #-}
+{-# INLINE [1] split #-}
+
+{-# RULES
+"TEXT split/singleton -> splitWith/==" [~1] forall c t.
+    split (singleton c) t = splitWith (==c) t
+  #-}
 
 -- | /O(m)*O(n)/ Break a 'Text' into pieces at most @k@ times,
 -- treating the first 'Text' argument as the delimiter to break on,

File Data/Text/Compat.hs

 
 import Data.ByteString (ByteString)
 import Data.Text hiding (split)
+import qualified Data.Text as T
 import Data.Text.Unsafe (unsafeTail)
 import Prelude (Char, (+), otherwise)
 
 split :: Char -> Text -> [Text]
-split = splitChar
+split c = T.split (singleton c)
 {-# INLINE split #-}
 
 -- | /O(n)/ Break a string on a substring, returning a pair of the

File tests/Properties.hs

 prop_T_split_i t       = id `eq` (T.intercalate t . T.split t)
 prop_T_splitTimes_i k t= id `eq` (T.intercalate t . T.splitTimes k t)
 prop_T_splitTimesEnd_i k t = id `eq` (T.intercalate t . T.splitTimesEnd k t)
-prop_T_splitChar_i c   = id `eq` (T.intercalate (T.singleton c) . T.splitChar c)
 prop_TL_split_i c      = id `eq` (TL.intercalate (TL.singleton c) . TL.split c)
 
 prop_T_splitWith p     = splitWith p `eqP` (map unpackS . T.splitWith p)
   ("prop_T_split_i", mytest prop_T_split_i),
   ("prop_T_splitTimes_i", mytest prop_T_splitTimes_i),
   ("prop_T_splitTimesEnd_i", mytest prop_T_splitTimesEnd_i),
-  ("prop_T_splitChar_i", mytest prop_T_splitChar_i),
   ("prop_TL_split_i", mytest prop_TL_split_i),
   ("prop_T_splitWith", mytest prop_T_splitWith),
   ("prop_T_splitWith_split", mytest prop_T_splitWith_split),