Commits

Bryan O'Sullivan  committed eca3279

Implement isSuffixOf

  • Participants
  • Parent commits 4c1602f

Comments (0)

Files changed (2)

File Data/Text.hs

 
     -- * Predicates
     , isPrefixOf
-    -- , isSuffixOf
+    , isSuffixOf
     -- , isInfixOf
 
     -- * Searching
     S.isPrefixOf (stream s) (stream t) = isPrefixOf s t
   #-}
 
+-- | /O(n)/ The 'isSuffixOf' function takes two 'Text's and returns
+-- 'True' iff the first is a suffix of the second.
+isSuffixOf :: Text -> Text -> Bool
+isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
+    d >= 0 && a == b'
+  where d              = blen - alen
+        b' | d == 0    = b
+           | otherwise = Text barr (boff+d) alen
+{-# INLINE isSuffixOf #-}
+
 errorEmptyList :: String -> a
 errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")

File tests/Properties.hs

 prop_unwords         = L.unwords     `eq`  (unpack . T.unwords . map pack)
 
 prop_isPrefixOf s    = L.isPrefixOf s`eqP` T.isPrefixOf (pack s)
-prop_isPrefixOfS s    = L.isPrefixOf s`eqP` (S.isPrefixOf (stream $ pack s) . stream)
+prop_isPrefixOfS s   = L.isPrefixOf s`eqP` (S.isPrefixOf (stream $ pack s) . stream)
+prop_isSuffixOf s    = L.isSuffixOf s`eqP` T.isSuffixOf (pack s)
 
 prop_elem c          = L.elem c      `eqP` T.elem c
 prop_filter p        = L.filter p    `eqP` (unpack . T.filter p)
 
   ("prop_isPrefixOf", mytest prop_isPrefixOf),
   ("prop_isPrefixOfS", mytest prop_isPrefixOfS),
+  ("prop_isSuffixOf", mytest prop_isSuffixOf),
 
   ("prop_elem", mytest prop_elem),
   ("prop_filter", mytest prop_filter),