on master help sign in
Implement isSuffixOf

--HG-- extra : convert_revision : f3272835a5566c9f3073d855e7691290c8ac3bb5

commit98b83d8d9ee4d71d8a7ec7eed4abbf81ca5aa8e0 authorBryan O'Sullivan <bos@serpentine.com> dateThu 12 Feb 2009, 07:38 (18 years ago) parent9bbfa0d
2 files changed+14−2
Data/Text.hs+11−1 tests/Properties.hs+3−1
Data/Text.hs+11−1
@@ -116,7 +116,7 @@ module Data.Text
116116
117117 -- * Predicates
118118 , isPrefixOf
119− -- , isSuffixOf
119+ , isSuffixOf
120120 -- , isInfixOf
121121
122122 -- * Searching
@@ -851,5 +851,15 @@ isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
851851 S.isPrefixOf (stream s) (stream t) = isPrefixOf s t
852852 #-}
853853
854+-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
855+-- 'True' iff the first is a suffix of the second.
856+isSuffixOf :: Text -> Text -> Bool
857+isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
858+ d >= 0 && a == b'
859+ where d = blen - alen
860+ b' | d == 0 = b
861+ | otherwise = Text barr (boff+d) alen
862+{-# INLINE isSuffixOf #-}
863+
854864errorEmptyList :: String -> a
855865errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")
tests/Properties.hs+3−1
@@ -141,7 +141,8 @@ prop_unlines = L.unlines `eq` (unpack . T.unlines . map pack)
141141prop_unwords = L.unwords `eq` (unpack . T.unwords . map pack)
142142
143143prop_isPrefixOf s = L.isPrefixOf s`eqP` T.isPrefixOf (pack s)
144−prop_isPrefixOfS s = L.isPrefixOf s`eqP` (S.isPrefixOf (stream $ pack s) . stream)
144+prop_isPrefixOfS s = L.isPrefixOf s`eqP` (S.isPrefixOf (stream $ pack s) . stream)
145+prop_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (pack s)
145146
146147prop_elem c = L.elem c `eqP` T.elem c
147148prop_filter p = L.filter p `eqP` (unpack . T.filter p)
@@ -240,6 +241,7 @@ tests = [
240241
241242 ("prop_isPrefixOf", mytest prop_isPrefixOf),
242243 ("prop_isPrefixOfS", mytest prop_isPrefixOfS),
244+ ("prop_isSuffixOf", mytest prop_isSuffixOf),
243245
244246 ("prop_elem", mytest prop_elem),
245247 ("prop_filter", mytest prop_filter),