Skip to content

Commit 1b93856

Browse files
committed
Add lazy variants of breakEnd and spanEnd
1 parent 5d4eafc commit 1b93856

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
@@ -145,9 +145,11 @@ module Data.Text.Lazy
145145
, stripEnd
146146
, splitAt
147147
, span
148+
, spanEnd
148149
, breakOn
149150
, breakOnEnd
150151
, break
152+
, breakEnd
151153
, group
152154
, groupBy
153155
, inits
@@ -1365,6 +1367,15 @@ break p t0 = break' t0
13651367
| otherwise -> let (a,b) = T.splitAt n t
13661368
in (Chunk a Empty, Chunk b ts)
13671369

1370+
-- | /O(n)/ Similar to 'break', but searches from the end of the string.
1371+
--
1372+
-- >>> T.breakEnd (=='0') "180cm"
1373+
-- ("180","cm")
1374+
breakEnd :: (Char -> Bool) -> Text -> (Text, Text)
1375+
breakEnd p src = let (a,b) = break p (reverse src)
1376+
in (reverse b, reverse a)
1377+
{-# INLINE breakEnd #-}
1378+
13681379
-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns
13691380
-- a pair whose first element is the longest prefix (possibly empty)
13701381
-- of @t@ of elements that satisfy @p@, and whose second is the
@@ -1376,6 +1387,14 @@ span :: (Char -> Bool) -> Text -> (Text, Text)
13761387
span p = break (not . p)
13771388
{-# INLINE span #-}
13781389

1390+
-- | /O(n)/ Similar to 'span', but searches from the end of the string.
1391+
--
1392+
-- >>> T.spanEnd Data.Char.isAlpha "000AB"
1393+
-- ("000","AB")
1394+
spanEnd :: (Char -> Bool) -> Text -> (Text, Text)
1395+
spanEnd p = breakEnd (not . p)
1396+
{-# INLINE spanEnd #-}
1397+
13791398
-- | The 'group' function takes a 'Text' and returns a list of 'Text's
13801399
-- such that the concatenation of the result is equal to the argument.
13811400
-- Moreover, each sublist in the result contains only equal elements.

0 commit comments

Comments
 (0)