on master help sign in
Implement splitTimesEnd.

--HG-- extra : convert_revision : 144d6ec983ee377038b6860483dfd3a9a9d53688

commit1ef6b04db1837d4326ae35b6a4d2f9726bb2d153 authorBryan O'Sullivan <bos@serpentine.com> dateFri 14 Aug 2009, 04:55 (17 years ago) parentc6c7796
2 files changed+21
Data/Text.hs+17 tests/Properties.hs+4
Data/Text.hs+17
@@ -124,6 +124,7 @@ module Data.Text
124124 -- $split
125125 , split
126126 , splitTimes
127+ , splitTimesEnd
127128 , splitChar
128129 , splitWith
129130 , chunksOf
@@ -961,6 +962,22 @@ splitTimes k pat src0
961962 | otherwise = search (n+1) (unsafeTail s)
962963{-# INLINE splitTimes #-}
963964
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+
964981-- | /O(n)/ Splits a 'Text' into components delimited by separators,
965982-- where the predicate returns True for a separator element. The
966983-- 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)
299299prop_TL_tails = L.tails `eqP` (map unpackS . TL.tails)
300300
301301prop_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)
302304prop_T_splitChar_i c = id `eq` (T.intercalate (T.singleton c) . T.splitChar c)
303305prop_TL_split_i c = id `eq` (TL.intercalate (TL.singleton c) . TL.split c)
304306
@@ -562,6 +564,8 @@ tests = [
562564 ("prop_TL_tails", mytest prop_TL_tails),
563565
564566 ("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),
565569 ("prop_T_splitChar_i", mytest prop_T_splitChar_i),
566570 ("prop_TL_split_i", mytest prop_TL_split_i),
567571 ("prop_T_splitWith", mytest prop_T_splitWith),