Skip to content

Commit

Permalink
Add lazy variants of breakEnd and spanEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMatten committed Mar 20, 2021
1 parent 013a68d commit f2a3080
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Data/Text/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ module Data.Text.Lazy
, stripEnd
, splitAt
, span
, spanEnd
, breakOn
, breakOnEnd
, break
, breakEnd
, group
, groupBy
, inits
Expand Down Expand Up @@ -1404,6 +1406,15 @@ break p t0 = break' t0
| otherwise -> let (a,b) = T.splitAt n t
in (Chunk a Empty, Chunk b ts)

-- | /O(n)/ Similar to 'break', but searches from the end of the string.
--
-- >>> T.breakEnd (=='0') "180cm"
-- ("180","cm")
breakEnd :: (Char -> Bool) -> Text -> (Text, Text)
breakEnd p src = let (a,b) = break p (reverse src)
in (reverse b, reverse a)
{-# INLINE breakEnd #-}

-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
-- a pair whose first element is the longest prefix (possibly empty)
-- of @t@ of elements that satisfy @p@, and whose second is the
Expand All @@ -1415,6 +1426,14 @@ span :: (Char -> Bool) -> Text -> (Text, Text)
span p = break (not . p)
{-# INLINE span #-}

-- | /O(n)/ Similar to 'span', but searches from the end of the string.
--
-- >>> T.spanEnd Data.Char.isAlpha "000AB"
-- ("000","AB")
spanEnd :: (Char -> Bool) -> Text -> (Text, Text)
spanEnd p = breakEnd (not . p)
{-# INLINE spanEnd #-}

-- | The 'group' function takes a 'Text' and returns a list of 'Text's
-- such that the concatenation of the result is equal to the argument.
-- Moreover, each sublist in the result contains only equal elements.
Expand Down

0 comments on commit f2a3080

Please sign in to comment.