on master help sign in
Implement and test lazy isSuffixOf
29f046bBryan O'Sullivan
Data/Text/Lazy.hs+8−1
@@ -121,7 +121,7 @@ module Data.Text.Lazy
121
121
122 -- * Predicates
122 -- * Predicates
123 , isPrefixOf
123 , isPrefixOf
124− -- , isSuffixOf
124+ , isSuffixOf
125 -- , isInfixOf
125 -- , isInfixOf
126
126
127 -- * Searching
127 -- * Searching
@@ -766,6 +766,13 @@ isPrefixOf (Chunk x xs) (Chunk y ys)
766 S.isPrefixOf (stream s) (stream t) = isPrefixOf s t
766 S.isPrefixOf (stream s) (stream t) = isPrefixOf s t
767 #-}
767 #-}
768
768
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+
769revChunks :: [T.Text] -> Text
776revChunks :: [T.Text] -> Text
770revChunks = L.foldl' (flip chunk) Empty
777revChunks = L.foldl' (flip chunk) Empty
771
778
tests/Properties.hs+2
@@ -304,6 +304,7 @@ prop_S_isPrefixOf s = L.isPrefixOf s`eqP` (S.isPrefixOf (S.stream $ packT s)
304prop_T_isPrefixOf s = L.isPrefixOf s`eqP` T.isPrefixOf (packT s)
304prop_T_isPrefixOf s = L.isPrefixOf s`eqP` T.isPrefixOf (packT s)
305prop_TL_isPrefixOf s = L.isPrefixOf s`eqP` TL.isPrefixOf (packT s)
305prop_TL_isPrefixOf s = L.isPrefixOf s`eqP` TL.isPrefixOf (packT s)
306prop_T_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (packT s)
306prop_T_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (packT s)
307+prop_TL_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packT s)
307prop_T_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packT s)
308prop_T_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packT s)
308
309
309prop_T_elem c = L.elem c `eqP` T.elem c
310prop_T_elem c = L.elem c `eqP` T.elem c
@@ -501,6 +502,7 @@ tests = [
501 ("prop_T_isPrefixOf", mytest prop_T_isPrefixOf),
502 ("prop_T_isPrefixOf", mytest prop_T_isPrefixOf),
502 ("prop_TL_isPrefixOf", mytest prop_TL_isPrefixOf),
503 ("prop_TL_isPrefixOf", mytest prop_TL_isPrefixOf),
503 ("prop_T_isSuffixOf", mytest prop_T_isSuffixOf),
504 ("prop_T_isSuffixOf", mytest prop_T_isSuffixOf),
505+ ("prop_TL_isSuffixOf", mytest prop_TL_isSuffixOf),
504 ("prop_T_isInfixOf", mytest prop_T_isInfixOf),
506 ("prop_T_isInfixOf", mytest prop_T_isInfixOf),
505
507
506 ("prop_T_elem", mytest prop_T_elem),
508 ("prop_T_elem", mytest prop_T_elem),