on master help sign in
Add isSingleton function.
f8e8603Bryan O'Sullivan
Data/Text.hs+12−8
@@ -368,6 +368,12 @@ null (Text _arr _off len) = assert (len >= 0) $ len <= 0
368 S.null (stream t) = null t
368 S.null (stream t) = null t
369 #-}
369 #-}
370
370
371+-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
372+-- Subject to fusion.
373+isSingleton :: Text -> Bool
374+isSingleton = S.isSingleton . stream
375+{-# INLINE isSingleton #-}
376+
371-- | /O(n)/ Returns the number of characters in a 'Text'.
377-- | /O(n)/ Returns the number of characters in a 'Text'.
372-- Subject to fusion.
378-- Subject to fusion.
373length :: Text -> Int
379length :: Text -> Int
@@ -916,12 +922,11 @@ tails t | null t = [empty]
916-- In (unlikely) bad cases, this function's time complexity
922-- In (unlikely) bad cases, this function's time complexity
917-- degenerates towards /O(n*m)/.
923-- degenerates towards /O(n*m)/.
918split :: Text -> Text -> [Text]
924split :: Text -> Text -> [Text]
919−split pat src@(Text arr off len)
925+split pat@(Text _ _ l) src@(Text arr off len)
920− | null pat = emptyError "split"
926+ | l <= 0 = emptyError "split"
921− | l == 1 = splitWith (== unsafeHead pat) src
927+ | isSingleton pat = splitWith (== unsafeHead pat) src
922− | otherwise = go 0 (indices pat src)
928+ | otherwise = go 0 (indices pat src)
923 where
929 where
924− l = length pat
925 go !s (x:xs) = textP arr (s+off) (x-s) : go (x+l) xs
930 go !s (x:xs) = textP arr (s+off) (x-s) : go (x+l) xs
926 go s _ = [textP arr (s+off) (len-s)]
931 go s _ = [textP arr (s+off) (len-s)]
927{-# INLINE [1] split #-}
932{-# INLINE [1] split #-}
@@ -955,14 +960,13 @@ splitTimes :: Int -- ^ Maximum number of times to split
955 -> Text -- ^ Text to split on
960 -> Text -- ^ Text to split on
956 -> Text -- ^ Input text
961 -> Text -- ^ Input text
957 -> [Text]
962 -> [Text]
958−splitTimes k pat src@(Text arr off len)
963+splitTimes k pat@(Text _ _ l) src@(Text arr off len)
959− | null pat = emptyError "splitTimes"
964+ | l <= 0 = emptyError "splitTimes"
960 | otherwise = go 0 0 (indices pat src)
965 | otherwise = go 0 0 (indices pat src)
961 where
966 where
962 go !s !i _ | i >= k = [textP arr (s+off) (len-s)]
967 go !s !i _ | i >= k = [textP arr (s+off) (len-s)]
963 go !s _ [] = [textP arr (s+off) (len-s)]
968 go !s _ [] = [textP arr (s+off) (len-s)]
964 go !s !i (x:xs) = textP arr (s+off) (x-s) : go (x+l) (i+1) xs
969 go !s !i (x:xs) = textP arr (s+off) (x-s) : go (x+l) (i+1) xs
965− l = length pat
966{-# INLINE splitTimes #-}
970{-# INLINE splitTimes #-}
967
971
968-- | /O(m+n)/ Break a 'Text' into pieces at most @k@ times, like
972-- | /O(m+n)/ Break a 'Text' into pieces at most @k@ times, like
Data/Text/Fusion/Common.hs+14−1
@@ -29,6 +29,7 @@ module Data.Text.Fusion.Common
29 , init
29 , init
30 , null
30 , null
31 , lengthI
31 , lengthI
32+ , isSingleton
32
33
33 -- * Transformations
34 -- * Transformations
34 , map
35 , map
@@ -255,7 +256,7 @@ null (Stream next s0 _len) = loop_null s0
255 Skip s' -> loop_null s'
256 Skip s' -> loop_null s'
256{-# INLINE[0] null #-}
257{-# INLINE[0] null #-}
257
258
258−-- | /O(n)/ Returns the number of characters in a text.
259+-- | /O(n)/ Returns the number of characters in a string.
259lengthI :: Integral a => Stream Char -> a
260lengthI :: Integral a => Stream Char -> a
260lengthI (Stream next s0 _len) = loop_length 0 s0
261lengthI (Stream next s0 _len) = loop_length 0 s0
261 where
262 where
@@ -265,6 +266,18 @@ lengthI (Stream next s0 _len) = loop_length 0 s0
265 Yield _ s' -> loop_length (z + 1) s'
266 Yield _ s' -> loop_length (z + 1) s'
266{-# INLINE[0] lengthI #-}
267{-# INLINE[0] lengthI #-}
267
268
269+-- | /O(n)/ Indicate whether a string contains exactly one element.
270+isSingleton :: Stream Char -> Bool
271+isSingleton (Stream next s0 _len) = loop 0 s0
272+ where
273+ loop !z s = case next s of
274+ Done -> z == (1::Int)
275+ Skip s' -> loop z s'
276+ Yield _ s'
277+ | z >= 1 -> False
278+ | otherwise -> loop (z+1) s'
279+{-# INLINE[0] isSingleton #-}
280+
268-- ----------------------------------------------------------------------------
281-- ----------------------------------------------------------------------------
269-- * Stream transformations
282-- * Stream transformations
270
283