on master help sign in
Implement lazy dropEnd

--HG-- extra : amend_source : 5ee6b5387747797e771947b62e2bddc33806069f

commit6207edffbe61952b44912655bd239269ca5c1ed1 authorBryan O'Sullivan <bos@serpentine.com> dateThu 6 Mar 2014, 20:17 (13 years ago) parent65b6ea5
2 files changed+21−3
Data/Text/Lazy.hs+18 tests/Tests/Properties.hs+3−3
Data/Text/Lazy.hs+18
@@ -130,6 +130,7 @@ module Data.Text.Lazy
130130 -- ** Breaking strings
131131 , take
132132 , drop
133+ , dropEnd
133134 , takeWhile
134135 , dropWhile
135136 , dropWhileEnd
@@ -957,6 +958,23 @@ drop i t0
957958 unstream (S.drop n (stream t)) = drop n t
958959 #-}
959960
961+-- | /O(n)/ 'dropEnd' @n@ @t@ returns the prefix remaining after
962+-- dropping @n@ characters from the end of @t@.
963+--
964+-- Examples:
965+--
966+-- > dropEnd 3 "foobar" == "foo"
967+dropEnd :: Int64 -> Text -> Text
968+dropEnd n t0
969+ | n <= 0 = t0
970+ | otherwise = dropChunk n . L.reverse . toChunks $ t0
971+ where dropChunk m [] = empty
972+ dropChunk m (t:ts)
973+ | m >= l = dropChunk (m-l) ts
974+ | otherwise = fromChunks . L.reverse $
975+ T.dropEnd (fromIntegral m) t : ts
976+ where l = fromIntegral (T.length t)
977+
960978-- | /O(n)/ 'dropWords' @n@ returns the suffix with @n@ 'Word16'
961979-- values dropped, or the empty 'Text' if @n@ is greater than the
962980-- number of 'Word16' values present.
tests/Tests/Properties.hs+3−3
@@ -472,8 +472,8 @@ t_drop n = L.drop n `eqP` (unpackS . T.drop n)
472472t_dropEnd n = (L.reverse . L.drop n . L.reverse) `eqP`
473473 (unpackS . T.dropEnd n)
474474tl_drop n = L.drop n `eqP` (unpackS . TL.drop (fromIntegral n))
475−--tl_dropEnd n = (L.reverse . L.drop n . L.reverse) `eqP`
476−-- (unpackS . TL.dropEnd n)
475+tl_dropEnd n = (L.reverse . L.drop n . L.reverse) `eqP`
476+ (unpackS . TL.dropEnd (fromIntegral n))
477477s_take_drop m = (L.take n . L.drop n) `eqP` (unpackS . S.take n . S.drop n)
478478 where n = small m
479479s_take_drop_s m = (L.take n . L.drop n) `eqP`
@@ -1067,7 +1067,7 @@ tests =
10671067 testProperty "t_drop" t_drop,
10681068 testProperty "t_dropEnd" t_dropEnd,
10691069 testProperty "tl_drop" tl_drop,
1070− -- testProperty "tl_dropEnd" tl_dropEnd,
1070+ testProperty "tl_dropEnd" tl_dropEnd,
10711071 testProperty "s_take_drop" s_take_drop,
10721072 testProperty "s_take_drop_s" s_take_drop_s,
10731073 testProperty "s_takeWhile" s_takeWhile,