on master help sign in
Implement splitTimesEnd.
1ef6b04Bryan O'Sullivan
Data/Text.hs+17
@@ -124,6 +124,7 @@ module Data.Text
124 -- $split
124 -- $split
125 , split
125 , split
126 , splitTimes
126 , splitTimes
127+ , splitTimesEnd
127 , splitChar
128 , splitChar
128 , splitWith
129 , splitWith
129 , chunksOf
130 , chunksOf
@@ -961,6 +962,22 @@ splitTimes k pat src0
961 | otherwise = search (n+1) (unsafeTail s)
962 | otherwise = search (n+1) (unsafeTail s)
962{-# INLINE splitTimes #-}
963{-# INLINE splitTimes #-}
963
964
965+-- | /O(m)*O(n)/ Break a 'Text' into pieces at most @k@ times, like
966+-- 'splitTimes', but start from the end of the input and work towards
967+-- the start.
968+--
969+-- Examples:
970+--
971+-- > splitTimes 2 "::" "a::b::c::d::e" == ["a","b","c::d::e"]
972+-- > splitTimesEnd 2 "::" "a::b::c::d::e" == ["a::b::c","d","e"]
973+splitTimesEnd :: Int -- ^ Maximum number of times to split
974+ -> Text -- ^ Text to split on
975+ -> Text -- ^ Input text
976+ -> [Text]
977+splitTimesEnd k pat src =
978+ L.reverse . L.map reverse $ splitTimes k (reverse pat) (reverse src)
979+{-# INLINE splitTimesEnd #-}
980+
964-- | /O(n)/ Splits a 'Text' into components delimited by separators,
981-- | /O(n)/ Splits a 'Text' into components delimited by separators,
965-- where the predicate returns True for a separator element. The
982-- where the predicate returns True for a separator element. The
966-- resulting components do not contain the separators. Two adjacent
983-- resulting components do not contain the separators. Two adjacent
tests/Properties.hs+4
@@ -299,6 +299,8 @@ prop_T_tails = L.tails `eqP` (map unpackS . T.tails)
299prop_TL_tails = L.tails `eqP` (map unpackS . TL.tails)
299prop_TL_tails = L.tails `eqP` (map unpackS . TL.tails)
300
300
301prop_T_split_i t = id `eq` (T.intercalate t . T.split t)
301prop_T_split_i t = id `eq` (T.intercalate t . T.split t)
302+prop_T_splitTimes_i k t= id `eq` (T.intercalate t . T.splitTimes k t)
303+prop_T_splitTimesEnd_i k t = id `eq` (T.intercalate t . T.splitTimesEnd k t)
302prop_T_splitChar_i c = id `eq` (T.intercalate (T.singleton c) . T.splitChar c)
304prop_T_splitChar_i c = id `eq` (T.intercalate (T.singleton c) . T.splitChar c)
303prop_TL_split_i c = id `eq` (TL.intercalate (TL.singleton c) . TL.split c)
305prop_TL_split_i c = id `eq` (TL.intercalate (TL.singleton c) . TL.split c)
304
306
@@ -562,6 +564,8 @@ tests = [
562 ("prop_TL_tails", mytest prop_TL_tails),
564 ("prop_TL_tails", mytest prop_TL_tails),
563
565
564 ("prop_T_split_i", mytest prop_T_split_i),
566 ("prop_T_split_i", mytest prop_T_split_i),
567+ ("prop_T_splitTimes_i", mytest prop_T_splitTimes_i),
568+ ("prop_T_splitTimesEnd_i", mytest prop_T_splitTimesEnd_i),
565 ("prop_T_splitChar_i", mytest prop_T_splitChar_i),
569 ("prop_T_splitChar_i", mytest prop_T_splitChar_i),
566 ("prop_TL_split_i", mytest prop_TL_split_i),
570 ("prop_TL_split_i", mytest prop_TL_split_i),
567 ("prop_T_splitWith", mytest prop_T_splitWith),
571 ("prop_T_splitWith", mytest prop_T_splitWith),