on master help sign in
Implement isSuffixOf
98b83d8Bryan O'Sullivan
Data/Text.hs+11−1
@@ -116,7 +116,7 @@ module Data.Text
116
116
117 -- * Predicates
117 -- * Predicates
118 , isPrefixOf
118 , isPrefixOf
119− -- , isSuffixOf
119+ , isSuffixOf
120 -- , isInfixOf
120 -- , isInfixOf
121
121
122 -- * Searching
122 -- * Searching
@@ -851,5 +851,15 @@ isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) =
851 S.isPrefixOf (stream s) (stream t) = isPrefixOf s t
851 S.isPrefixOf (stream s) (stream t) = isPrefixOf s t
852 #-}
852 #-}
853
853
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+
854errorEmptyList :: String -> a
864errorEmptyList :: String -> a
855errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")
865errorEmptyList 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)
141prop_unwords = L.unwords `eq` (unpack . T.unwords . map pack)
141prop_unwords = L.unwords `eq` (unpack . T.unwords . map pack)
142
142
143prop_isPrefixOf s = L.isPrefixOf s`eqP` T.isPrefixOf (pack s)
143prop_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)
145
146
146prop_elem c = L.elem c `eqP` T.elem c
147prop_elem c = L.elem c `eqP` T.elem c
147prop_filter p = L.filter p `eqP` (unpack . T.filter p)
148prop_filter p = L.filter p `eqP` (unpack . T.filter p)
@@ -240,6 +241,7 @@ tests = [
240
241
241 ("prop_isPrefixOf", mytest prop_isPrefixOf),
242 ("prop_isPrefixOf", mytest prop_isPrefixOf),
242 ("prop_isPrefixOfS", mytest prop_isPrefixOfS),
243 ("prop_isPrefixOfS", mytest prop_isPrefixOfS),
244+ ("prop_isSuffixOf", mytest prop_isSuffixOf),
243
245
244 ("prop_elem", mytest prop_elem),
246 ("prop_elem", mytest prop_elem),
245 ("prop_filter", mytest prop_filter),
247 ("prop_filter", mytest prop_filter),