@@ -152,7 +152,9 @@ module Data.Text
152
152
, breakOn
153
153
, breakOnEnd
154
154
, break
155
+ , breakEnd
155
156
, span
157
+ , spanEnd
156
158
, group
157
159
, groupBy
158
160
, inits
@@ -233,7 +235,7 @@ import qualified Data.Text.Internal.Fusion as S
233
235
import qualified Data.Text.Internal.Fusion.Common as S
234
236
import Data.Text.Encoding (decodeUtf8' , encodeUtf8 )
235
237
import Data.Text.Internal.Fusion (stream , reverseStream , unstream )
236
- import Data.Text.Internal.Private (span_ )
238
+ import Data.Text.Internal.Private (span_ , spanEnd_ )
237
239
import Data.Text.Internal (Text (.. ), empty , firstf , mul , safe , text )
238
240
import Data.Text.Show (singleton , unpack , unpackCString #)
239
241
import qualified Prelude as P
@@ -1367,6 +1369,15 @@ span p t = case span_ p t of
1367
1369
(# hd,tl # ) -> (hd,tl)
1368
1370
{-# INLINE span #-}
1369
1371
1372
+ -- | /O(n)/ Similar to 'span', but searches from the end of the
1373
+ -- string.
1374
+ --
1375
+ -- >>> T.spanEnd (=='0') "AB000"
1376
+ -- ("AB", "000")
1377
+ spanEnd :: (Char -> Bool ) -> Text -> (Text , Text )
1378
+ spanEnd p t = case spanEnd_ p t of (# hd, tl # ) -> (hd, tl)
1379
+ {-# inline spanEnd #-}
1380
+
1370
1381
-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1371
1382
-- over elements that fail the predicate @p@.
1372
1383
--
@@ -1376,6 +1387,15 @@ break :: (Char -> Bool) -> Text -> (Text, Text)
1376
1387
break p = span (not . p)
1377
1388
{-# INLINE break #-}
1378
1389
1390
+ -- | /O(n)/ Similar to 'break', but searches from the end of the
1391
+ -- string.
1392
+ --
1393
+ -- >>> T.breakEnd (=='0') "180cm"
1394
+ -- ("180","cm")
1395
+ breakEnd :: (Char -> Bool ) -> Text -> (Text , Text )
1396
+ breakEnd p = spanEnd (not . p)
1397
+ {-# inline breakEnd #-}
1398
+
1379
1399
-- | /O(n)/ Group characters in a string according to a predicate.
1380
1400
groupBy :: (Char -> Char -> Bool ) -> Text -> [Text ]
1381
1401
groupBy p = loop
0 commit comments