Skip to content

Commit f2a3080

Browse files
committed
Add lazy variants of breakEnd and spanEnd
1 parent 013a68d commit f2a3080

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/Data/Text/Lazy.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,11 @@ module Data.Text.Lazy
157157
, stripEnd
158158
, splitAt
159159
, span
160+
, spanEnd
160161
, breakOn
161162
, breakOnEnd
162163
, break
164+
, breakEnd
163165
, group
164166
, groupBy
165167
, inits
@@ -1404,6 +1406,15 @@ break p t0 = break' t0
14041406
| otherwise -> let (a,b) = T.splitAt n t
14051407
in (Chunk a Empty, Chunk b ts)
14061408

1409+
-- | /O(n)/ Similar to 'break', but searches from the end of the string.
1410+
--
1411+
-- >>> T.breakEnd (=='0') "180cm"
1412+
-- ("180","cm")
1413+
breakEnd :: (Char -> Bool) -> Text -> (Text, Text)
1414+
breakEnd p src = let (a,b) = break p (reverse src)
1415+
in (reverse b, reverse a)
1416+
{-# INLINE breakEnd #-}
1417+
14071418
-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
14081419
-- a pair whose first element is the longest prefix (possibly empty)
14091420
-- of @t@ of elements that satisfy @p@, and whose second is the
@@ -1415,6 +1426,14 @@ span :: (Char -> Bool) -> Text -> (Text, Text)
14151426
span p = break (not . p)
14161427
{-# INLINE span #-}
14171428

1429+
-- | /O(n)/ Similar to 'span', but searches from the end of the string.
1430+
--
1431+
-- >>> T.spanEnd Data.Char.isAlpha "000AB"
1432+
-- ("000","AB")
1433+
spanEnd :: (Char -> Bool) -> Text -> (Text, Text)
1434+
spanEnd p = breakEnd (not . p)
1435+
{-# INLINE spanEnd #-}
1436+
14181437
-- | The 'group' function takes a 'Text' and returns a list of 'Text's
14191438
-- such that the concatenation of the result is equal to the argument.
14201439
-- Moreover, each sublist in the result contains only equal elements.

0 commit comments

Comments
 (0)