Commits

Bryan O'Sullivan  committed 111c840

Add breakEnd.

  • Participants
  • Parent commits b5b51f2

Comments (0)

Files changed (2)

File Data/Text.hs

     , splitAt
     , spanBy
     , break
+    , breakEnd
     , breakBy
     , group
     , groupBy
                     (x:_) -> (textP arr off x, textP arr (off+x) (len-x))
 {-# INLINE break #-}
 
+-- | /O(n+m)/ Similar to 'break', but searches from the end of the string.
+--
+-- The first element of the returned tuple is the prefix of @haystack@
+-- up to and including the last match of @needle@.  The second is the
+-- remainder of @haystack@, following the match.
+--
+-- > breakEnd "::" "a::b::c" ==> ("a::b::", "c")
+breakEnd :: Text -> Text -> (Text, Text)
+breakEnd pat src = let (a,b) = break (reverse pat) (reverse src)
+                   in  (reverse b, reverse a)
+{-# INLINE breakEnd #-}
+
 -- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
 -- @haystack@.  The first element of the returned pair is the prefix
 -- of @haystack@ prior to any matches of @needle@.  The second is a

File Data/Text/Lazy.hs

     , splitAt
     , spanBy
     , break
+    , breakEnd
     , breakBy
     , group
     , groupBy
                     (x:_) -> let h :*: t = splitAtWord x src
                              in  (h, t)
 
+-- | /O(n+m)/ Similar to 'break', but searches from the end of the string.
+--
+-- The first element of the returned tuple is the prefix of @haystack@
+-- up to and including the last match of @needle@.  The second is the
+-- remainder of @haystack@, following the match.
+--
+-- > breakEnd "::" "a::b::c" ==> ("a::b::", "c")
+breakEnd :: Text -> Text -> (Text, Text)
+breakEnd pat src = let (a,b) = break (reverse pat) (reverse src)
+                   in  (reverse b, reverse a)
+{-# INLINE breakEnd #-}
+
 -- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
 -- @haystack@.  The first element of the returned pair is the prefix
 -- of @haystack@ prior to any matches of @needle@.  The second is a