on master help sign in
Implement isPrefixOf

Still need to write fused version --HG-- extra : convert_revision : 9d238e05aa4d52e77ba4f2bf997e56b9cb7d26df

commit26830b55383b3ef777a27a3e3ce72c67bb47ee09 authorBryan O'Sullivan <bos@serpentine.com> dateWed 11 Feb 2009, 07:30 (18 years ago) parent3b523ff
2 files changed+15−2
Data/Text.hs+11−2 tests/Properties.hs+4
Data/Text.hs+11−2
@@ -115,7 +115,7 @@ module Data.Text
115115 , unwords
116116
117117 -- * Predicates
118− -- , isPrefixOf
118+ , isPrefixOf
119119 -- , isSuffixOf
120120 -- , isInfixOf
121121
@@ -143,7 +143,7 @@ module Data.Text
143143 , -- sort
144144 ) where
145145
146−import Prelude (Char, Bool, Functor(..), Int, Maybe(..), String,
146+import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
147147 Eq, (==), (++), error,
148148 Read(..), Show(..),
149149 (&&), (||), (+), (-), (<), (>), (<=), (>=), (.), ($),
@@ -836,5 +836,14 @@ unwords :: [Text] -> Text
836836unwords = intercalate (singleton ' ')
837837{-# INLINE unwords #-}
838838
839+isPrefixOf :: Text -> Text -> Bool
840+isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) = alen <= blen && loop 0 0
841+ where loop !i !j | i >= alen = True
842+ | c == e = loop (i+d) (j+f)
843+ | otherwise = False
844+ where (c,d) = iter a i
845+ (e,f) = iter b j
846+{-# INLINE [1] isPrefixOf #-}
847+
839848errorEmptyList :: String -> a
840849errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")
tests/Properties.hs+4
@@ -140,6 +140,8 @@ prop_words = L.words `eqP` (map unpack . T.words)
140140prop_unlines = L.unlines `eq` (unpack . T.unlines . map pack)
141141prop_unwords = L.unwords `eq` (unpack . T.unwords . map pack)
142142
143+prop_isPrefixOf s = L.isPrefixOf s`eqP` T.isPrefixOf (pack s)
144+
143145prop_elem c = L.elem c `eqP` T.elem c
144146prop_filter p = L.filter p `eqP` (unpack . T.filter p)
145147prop_find p = L.find p `eqP` T.find p
@@ -235,6 +237,8 @@ tests = [
235237 ("prop_unlines", mytest prop_unlines),
236238 ("prop_unwords", mytest prop_unwords),
237239
240+ ("prop_isPrefixOf", mytest prop_isPrefixOf),
241+
238242 ("prop_elem", mytest prop_elem),
239243 ("prop_filter", mytest prop_filter),
240244 ("prop_find", mytest prop_find),