@@ -156,6 +156,7 @@ module Data.Text
156 , findIndices
156 , findIndices
157 , elemIndex
157 , elemIndex
158 , elemIndices
158 , elemIndices
159 , countChar
160 , countChar
161 -- * Zipping and unzipping
162 -- * Zipping and unzipping
@@ -899,7 +900,10 @@ tails t | null t = [empty]
899-- are slices of the original.
900-- are slices of the original.
901-- | /O(m)*O(n)/ Break a 'Text' into pieces separated by the first
902-- | /O(m)*O(n)/ Break a 'Text' into pieces separated by the first
902−-- 'Text' argument, consuming the delimiter. Examples:
903+-- 'Text' argument, consuming the delimiter. An empty delimiter is
904+-- invalid, and will cause an error to be raised.
904-- > split "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
908-- > split "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
905-- > split "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
909-- > split "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""]
@@ -913,8 +917,8 @@ split :: Text -- ^ Text to split on
913 -> Text -- ^ Input text
917 -> Text -- ^ Input text
914 -> [Text]
918 -> [Text]
915split pat src0
919split pat src0
916− | l == 0 = [src0]
920+ | l == 0 = emptyError "split"
917− | l == 1 = splitWith (== (head pat)) src0
921+ | l == 1 = splitWith (== (unsafeHead pat)) src0
918 | otherwise = go src0
922 | otherwise = go src0
920 l = length pat
924 l = length pat
@@ -936,7 +940,8 @@ split pat src0
936-- and consuming the delimiter. The last element of the list contains
940-- and consuming the delimiter. The last element of the list contains
937-- the remaining text after the number of times to split has been
941-- the remaining text after the number of times to split has been
938-- reached. A value of zero or less for @k@ causes no splitting to
942-- reached. A value of zero or less for @k@ causes no splitting to
939−-- occur.
943+-- occur. An empty delimiter is invalid, and will cause an error to be
941-- Examples:
946-- Examples:
@@ -952,8 +957,9 @@ splitTimes :: Int -- ^ Maximum number of times to split
952 -> Text -- ^ Input text
957 -> Text -- ^ Input text
953 -> [Text]
958 -> [Text]
954splitTimes k pat src0
959splitTimes k pat src0
955− | k <= 0 || l == 0 = [src0]
960+ | k <= 0 = [src0]
961+ | l == 0 = emptyError "splitTimes"
956− | otherwise = go k src0
962+ | otherwise = go k src0
958 l = length pat
964 l = length pat
959 go !i src = search 0 src
965 go !i src = search 0 src
@@ -1100,6 +1106,22 @@ elemIndices :: Char -> Text -> [Int]
1100elemIndices c t = S.elemIndices c (stream t)
1106elemIndices c t = S.elemIndices c (stream t)
1101{-# INLINE elemIndices #-}
1107{-# INLINE elemIndices #-}
1109+-- | /O(n*m)/ The 'count' function returns the number of times the
1110+-- query string appears in the given 'Text'.
1111+count :: Text -> Text -> Int
1113+ | l == 0 = length src0 + 1
1114+ | l == 1 = countChar (unsafeHead pat) src0
1115+ | otherwise = go 0 src0
1118+ go !n src = search src
1120+ search s | null s = n
1121+ | pat `isPrefixOf` s = go (n+1) (drop l s)
1122+ | otherwise = search (unsafeTail s)
1123+{-# INLINE [1] count #-}
1103-- | /O(n)/ The 'countChar' function returns the number of times the
1125-- | /O(n)/ The 'countChar' function returns the number of times the
1104-- query element appears in the given 'Text'. Subject to fusion.
1126-- query element appears in the given 'Text'. Subject to fusion.
1105countChar :: Char -> Text -> Int
1127countChar :: Char -> Text -> Int