1. Bryan O'Sullivan
  2. text

Commits

Bryan O'Sullivan  committed 7eff5ab

Implement isPrefixOf
Still need to write fused version

  • Participants
  • Parent commits 9faf4ee
  • Branches default

Comments (0)

Files changed (2)

File Data/Text.hs

View file
  • Ignore whitespace
     , unwords
 
     -- * Predicates
-    -- , isPrefixOf
+    , isPrefixOf
     -- , isSuffixOf
     -- , isInfixOf
 
     , -- sort
     ) where
 
-import Prelude (Char, Bool, Functor(..), Int, Maybe(..), String,
+import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
                 Eq, (==), (++), error,
                 Read(..), Show(..),
                 (&&), (||), (+), (-), (<), (>), (<=), (>=), (.), ($),
 unwords = intercalate (singleton ' ')
 {-# INLINE unwords #-}
 
+isPrefixOf :: Text -> Text -> Bool
+isPrefixOf a@(Text _ _ alen) b@(Text _ _ blen) = alen <= blen && loop 0 0
+    where loop !i !j | i >= alen = True
+                     | c == e    = loop (i+d) (j+f)
+                     | otherwise = False
+                     where (c,d) = iter a i
+                           (e,f) = iter b j
+{-# INLINE [1] isPrefixOf #-}
+
 errorEmptyList :: String -> a
 errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")

File tests/Properties.hs

View file
  • Ignore whitespace
 prop_unlines         = L.unlines     `eq`  (unpack . T.unlines . map pack)
 prop_unwords         = L.unwords     `eq`  (unpack . T.unwords . map pack)
 
+prop_isPrefixOf s    = L.isPrefixOf s`eqP` T.isPrefixOf (pack s)
+
 prop_elem c          = L.elem c      `eqP` T.elem c
 prop_filter p        = L.filter p    `eqP` (unpack . T.filter p)
 prop_find p          = L.find p      `eqP` T.find p
   ("prop_unlines", mytest prop_unlines),
   ("prop_unwords", mytest prop_unwords),
 
+  ("prop_isPrefixOf", mytest prop_isPrefixOf),
+
   ("prop_elem", mytest prop_elem),
   ("prop_filter", mytest prop_filter),
   ("prop_find", mytest prop_find),