Commits

Bryan O'Sullivan committed 8aad66e

Rename prefixed -> stripPrefix, suffixed -> stripSuffix

Gives us consistency with Data.List

Comments (0)

Files changed (3)

     , isInfixOf
 
     -- ** View patterns
-    , prefixed
-    , suffixed
+    , stripPrefix
+    , stripSuffix
 
     -- * Searching
     , filter
 --
 -- Examples:
 --
--- > prefixed "foo" "foobar" == Just "bar"
--- > prefixed "foo" "quux"   == Nothing
+-- > stripPrefix "foo" "foobar" == Just "bar"
+-- > stripPrefix "foo" "quux"   == Nothing
 --
 -- This is particularly useful with the @ViewPatterns@ extension to
 -- GHC, as follows:
 -- > import Data.Text as T
 -- >
 -- > fnordLength :: Text -> Int
--- > fnordLength (prefixed "fnord" -> Just suf) = T.length suf
+-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
 -- > fnordLength _                              = -1
-prefixed :: Text -> Text -> Maybe Text
-prefixed p@(Text _arr _off plen) t@(Text arr off len)
+stripPrefix :: Text -> Text -> Maybe Text
+stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
     | p `isPrefixOf` t = Just $! textP arr (off+plen) (len-plen)
     | otherwise        = Nothing
 
 --
 -- Examples:
 --
--- > suffixed "bar" "foobar" == Just "foo"
--- > suffixed "foo" "quux"   == Nothing
+-- > stripSuffix "bar" "foobar" == Just "foo"
+-- > stripSuffix "foo" "quux"   == Nothing
 --
 -- This is particularly useful with the @ViewPatterns@ extension to
 -- GHC, as follows:
 -- > import Data.Text as T
 -- >
 -- > quuxLength :: Text -> Int
--- > quuxLength (suffixed "quux" -> Just pre) = T.length pre
+-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
 -- > quuxLength _                             = -1
-suffixed :: Text -> Text -> Maybe Text
-suffixed p@(Text _arr _off plen) t@(Text arr off len)
+stripSuffix :: Text -> Text -> Maybe Text
+stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
     | p `isSuffixOf` t = Just $! textP arr off (len-plen)
     | otherwise        = Nothing
 

Data/Text/Lazy.hs

     , isInfixOf
 
     -- ** View patterns
-    , prefixed
-    , suffixed
+    , stripPrefix
+    , stripSuffix
 
     -- * Searching
     , filter
 --
 -- Examples:
 --
--- > prefixed "foo" "foobar" == Just "bar"
--- > prefixed "foo" "quux"   == Nothing
+-- > stripPrefix "foo" "foobar" == Just "bar"
+-- > stripPrefix "foo" "quux"   == Nothing
 --
 -- This is particularly useful with the @ViewPatterns@ extension to
 -- GHC, as follows:
 -- > import Data.Text as T
 -- >
 -- > fnordLength :: Text -> Int
--- > fnordLength (prefixed "fnord" -> Just suf) = T.length suf
+-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
 -- > fnordLength _                              = -1
-prefixed :: Text -> Text -> Maybe Text
+stripPrefix :: Text -> Text -> Maybe Text
 -- Yes, this could be much more efficient.
-prefixed p t
+stripPrefix p t
     | p `isPrefixOf` t = Just (drop (length p) t)
     | otherwise        = Nothing
 
 --
 -- Examples:
 --
--- > suffixed "bar" "foobar" == Just "foo"
--- > suffixed "foo" "quux"   == Nothing
+-- > stripSuffix "bar" "foobar" == Just "foo"
+-- > stripSuffix "foo" "quux"   == Nothing
 --
 -- This is particularly useful with the @ViewPatterns@ extension to
 -- GHC, as follows:
 -- > import Data.Text as T
 -- >
 -- > quuxLength :: Text -> Int
--- > quuxLength (suffixed "quux" -> Just pre) = T.length pre
+-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
 -- > quuxLength _                             = -1
-suffixed :: Text -> Text -> Maybe Text
+stripSuffix :: Text -> Text -> Maybe Text
 -- Yes, this could be much more efficient.
-suffixed p t
+stripSuffix p t
     | p `isSuffixOf` t = Just (take (length t - length p) t)
     | otherwise        = Nothing
 

tests/Properties.hs

 t_isInfixOf s     = L.isInfixOf s `eqP` T.isInfixOf (packS s)
 tl_isInfixOf s    = L.isInfixOf s `eqP` TL.isInfixOf (packS s)
 
-prefixed (p:ps) (t:ts)
-    | p == t = prefixed ps ts
-prefixed [] ts = Just ts
-prefixed _  _  = Nothing
+t_stripPrefix s      = (fmap packS . L.stripPrefix s) `eqP` T.stripPrefix (packS s)
+tl_stripPrefix s     = (fmap packS . L.stripPrefix s) `eqP` TL.stripPrefix (packS s)
 
-t_prefixed s      = (fmap packS . prefixed s) `eqP` T.prefixed (packS s)
-tl_prefixed s     = (fmap packS . prefixed s) `eqP` TL.prefixed (packS s)
+stripSuffix p t = reverse `fmap` L.stripPrefix (reverse p) (reverse t)
 
-suffixed p t = reverse `fmap` prefixed (reverse p) (reverse t)
-
-t_suffixed s      = (fmap packS . suffixed s) `eqP` T.suffixed (packS s)
-tl_suffixed s     = (fmap packS . suffixed s) `eqP` TL.suffixed (packS s)
+t_stripSuffix s      = (fmap packS . stripSuffix s) `eqP` T.stripSuffix (packS s)
+tl_stripSuffix s     = (fmap packS . stripSuffix s) `eqP` TL.stripSuffix (packS s)
 
 sf_elem p c       = (L.elem c . L.filter p) `eqP` (S.elem c . S.filter p)
 sf_filter q p     = (L.filter p . L.filter q) `eqP`
     testProperty "tl_isInfixOf" tl_isInfixOf,
 
     testGroup "view" [
-      testProperty "t_prefixed" t_prefixed,
-      testProperty "tl_prefixed" tl_prefixed,
-      testProperty "t_suffixed" t_suffixed,
-      testProperty "tl_suffixed" tl_suffixed
+      testProperty "t_stripPrefix" t_stripPrefix,
+      testProperty "tl_stripPrefix" tl_stripPrefix,
+      testProperty "t_stripSuffix" t_stripSuffix,
+      testProperty "tl_stripSuffix" tl_stripSuffix
     ]
   ],