on master help sign in
Implement and test lazy isSuffixOf

--HG-- extra : convert_revision : bb59d174a5060f53a0bc2abef88634e065595fc7

commit29f046b75b907d5e51f7227f90ec41f292e9add0 authorBryan O'Sullivan <bos@serpentine.com> dateSun 8 Mar 2009, 03:25 (18 years ago) parent1f4aef2
2 files changed+10−1
Data/Text/Lazy.hs+8−1 tests/Properties.hs+2
Data/Text/Lazy.hs+8−1
@@ -121,7 +121,7 @@ module Data.Text.Lazy
121121
122122 -- * Predicates
123123 , isPrefixOf
124− -- , isSuffixOf
124+ , isSuffixOf
125125 -- , isInfixOf
126126
127127 -- * Searching
@@ -766,6 +766,13 @@ isPrefixOf (Chunk x xs) (Chunk y ys)
766766 S.isPrefixOf (stream s) (stream t) = isPrefixOf s t
767767 #-}
768768
769+-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
770+-- 'True' iff the first is a suffix of the second.
771+isSuffixOf :: Text -> Text -> Bool
772+isSuffixOf x y = reverse x `isPrefixOf` reverse y
773+{-# INLINE isSuffixOf #-}
774+-- TODO: a better implementation
775+
769776revChunks :: [T.Text] -> Text
770777revChunks = L.foldl' (flip chunk) Empty
771778
tests/Properties.hs+2
@@ -304,6 +304,7 @@ prop_S_isPrefixOf s = L.isPrefixOf s`eqP` (S.isPrefixOf (S.stream $ packT s)
304304prop_T_isPrefixOf s = L.isPrefixOf s`eqP` T.isPrefixOf (packT s)
305305prop_TL_isPrefixOf s = L.isPrefixOf s`eqP` TL.isPrefixOf (packT s)
306306prop_T_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (packT s)
307+prop_TL_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packT s)
307308prop_T_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packT s)
308309
309310prop_T_elem c = L.elem c `eqP` T.elem c
@@ -501,6 +502,7 @@ tests = [
501502 ("prop_T_isPrefixOf", mytest prop_T_isPrefixOf),
502503 ("prop_TL_isPrefixOf", mytest prop_TL_isPrefixOf),
503504 ("prop_T_isSuffixOf", mytest prop_T_isSuffixOf),
505+ ("prop_TL_isSuffixOf", mytest prop_TL_isSuffixOf),
504506 ("prop_T_isInfixOf", mytest prop_T_isInfixOf),
505507
506508 ("prop_T_elem", mytest prop_T_elem),