@@ -141,7 +141,9 @@ module Data.Text
141
141
, breakOn
142
142
, breakOnEnd
143
143
, break
144
+ , breakEnd
144
145
, span
146
+ , spanEnd
145
147
, group
146
148
, groupBy
147
149
, inits
@@ -221,7 +223,7 @@ import qualified Data.Text.Internal.Fusion as S
221
223
import qualified Data.Text.Internal.Fusion.Common as S
222
224
import Data.Text.Encoding (decodeUtf8' , encodeUtf8 )
223
225
import Data.Text.Internal.Fusion (stream , reverseStream , unstream )
224
- import Data.Text.Internal.Private (span_ )
226
+ import Data.Text.Internal.Private (span_ , spanEnd_ )
225
227
import Data.Text.Internal (Text (.. ), empty , firstf , mul , safe , text )
226
228
import Data.Text.Show (singleton , unpack , unpackCString #)
227
229
import qualified Prelude as P
@@ -1333,6 +1335,15 @@ span p t = case span_ p t of
1333
1335
(# hd,tl # ) -> (hd,tl)
1334
1336
{-# INLINE span #-}
1335
1337
1338
+ -- | /O(n)/ Similar to 'span', but searches from the end of the
1339
+ -- string.
1340
+ --
1341
+ -- >>> T.spanEnd (=='0') "AB000"
1342
+ -- ("AB", "000")
1343
+ spanEnd :: (Char -> Bool ) -> Text -> (Text , Text )
1344
+ spanEnd p t = case spanEnd_ p t of (# hd, tl # ) -> (hd, tl)
1345
+ {-# inline spanEnd #-}
1346
+
1336
1347
-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1337
1348
-- over elements that fail the predicate @p@.
1338
1349
--
@@ -1342,6 +1353,15 @@ break :: (Char -> Bool) -> Text -> (Text, Text)
1342
1353
break p = span (not . p)
1343
1354
{-# INLINE break #-}
1344
1355
1356
+ -- | /O(n)/ Similar to 'break', but searches from the end of the
1357
+ -- string.
1358
+ --
1359
+ -- >>> T.breakEnd (=='0') "180cm"
1360
+ -- ("180","cm")
1361
+ breakEnd :: (Char -> Bool ) -> Text -> (Text , Text )
1362
+ breakEnd p = spanEnd (not . p)
1363
+ {-# inline breakEnd #-}
1364
+
1345
1365
-- | /O(n)/ Group characters in a string according to a predicate.
1346
1366
groupBy :: (Char -> Char -> Bool ) -> Text -> [Text ]
1347
1367
groupBy p = loop
0 commit comments