on master help sign in
Add breakEnd.
3a86c2dBryan O'Sullivan
Data/Text.hs+13
@@ -115,6 +115,7 @@ module Data.Text
115 , splitAt
115 , splitAt
116 , spanBy
116 , spanBy
117 , break
117 , break
118+ , breakEnd
118 , breakBy
119 , breakBy
119 , group
120 , group
120 , groupBy
121 , groupBy
@@ -1040,6 +1041,18 @@ break pat src@(Text arr off len)
1040 (x:_) -> (textP arr off x, textP arr (off+x) (len-x))
1041 (x:_) -> (textP arr off x, textP arr (off+x) (len-x))
1041{-# INLINE break #-}
1042{-# INLINE break #-}
1042
1043
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+
1043-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1056-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
1044-- @haystack@. The first element of the returned pair is the prefix
1057-- @haystack@. The first element of the returned pair is the prefix
1045-- of @haystack@ prior to any matches of @needle@. The second is a
1058-- 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
119 , splitAt
119 , splitAt
120 , spanBy
120 , spanBy
121 , break
121 , break
122+ , breakEnd
122 , breakBy
123 , breakBy
123 , group
124 , group
124 , groupBy
125 , groupBy
@@ -881,6 +882,18 @@ break pat src
881 (x:_) -> let h :*: t = splitAtWord x src
882 (x:_) -> let h :*: t = splitAtWord x src
882 in (h, t)
883 in (h, t)
883
884
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+
884-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
897-- | /O(n+m)/ Find all non-overlapping instances of @needle@ in
885-- @haystack@. The first element of the returned pair is the prefix
898-- @haystack@. The first element of the returned pair is the prefix
886-- of @haystack@ prior to any matches of @needle@. The second is a
899-- of @haystack@ prior to any matches of @needle@. The second is a