on master help sign in
Add breakEnd.

--HG-- extra : convert_revision : 722e5b49994f8809e284dff81c62f06f6229a167

commit3a86c2d4273a8acd9d5a3a12232a97dc1c132bc9 authorBryan O'Sullivan <bos@serpentine.com> dateMon 7 Dec 2009, 05:47 (17 years ago) parente635b4a
2 files changed+26
Data/Text.hs+13 Data/Text/Lazy.hs+13
Data/Text.hs+13
@@ -115,6 +115,7 @@ module Data.Text
115115 , splitAt
116116 , spanBy
117117 , break
118+ , breakEnd
118119 , breakBy
119120 , group
120121 , groupBy
@@ -1040,6 +1041,18 @@ break pat src@(Text arr off len)
10401041 (x:_) -> (textP arr off x, textP arr (off+x) (len-x))
10411042{-# INLINE break #-}
10421043
1044+-- | /O(n+m)/ Similar to 'break', but searches from the end of the string.
1045+--
1046+-- The first element of the returned tuple is the prefix of @haystack@
1047+-- up to and including the last match of @needle@. The second is the
1048+-- remainder of @haystack@, following the match.
1049+--
1050+-- > breakEnd "::" "a::b::c" ==> ("a::b::", "c")
1051+breakEnd :: Text -> Text -> (Text, Text)
1052+breakEnd pat src = let (a,b) = break (reverse pat) (reverse src)
1053+ in (reverse b, reverse a)
1054+{-# INLINE breakEnd #-}
1055+
10431056-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
10441057-- @haystack@. The first element of the returned pair is the prefix
10451058-- of @haystack@ prior to any matches of @needle@. The second is a
Data/Text/Lazy.hs+13
@@ -119,6 +119,7 @@ module Data.Text.Lazy
119119 , splitAt
120120 , spanBy
121121 , break
122+ , breakEnd
122123 , breakBy
123124 , group
124125 , groupBy
@@ -881,6 +882,18 @@ break pat src
881882 (x:_) -> let h :*: t = splitAtWord x src
882883 in (h, t)
883884
885+-- | /O(n+m)/ Similar to 'break', but searches from the end of the string.
886+--
887+-- The first element of the returned tuple is the prefix of @haystack@
888+-- up to and including the last match of @needle@. The second is the
889+-- remainder of @haystack@, following the match.
890+--
891+-- > breakEnd "::" "a::b::c" ==> ("a::b::", "c")
892+breakEnd :: Text -> Text -> (Text, Text)
893+breakEnd pat src = let (a,b) = break (reverse pat) (reverse src)
894+ in (reverse b, reverse a)
895+{-# INLINE breakEnd #-}
896+
884897-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
885898-- @haystack@. The first element of the returned pair is the prefix
886899-- of @haystack@ prior to any matches of @needle@. The second is a