Skip to content

Commit

Permalink
Rename prefixed -> stripPrefix, suffixed -> stripSuffix
Browse files Browse the repository at this point in the history
Gives us consistency with Data.List

--HG--
extra : convert_revision : 7b496f8cff0fbfa84c8a416eac690b8faa0e7d1b
  • Loading branch information
bos committed Sep 8, 2010
1 parent 6f2c867 commit 9cebb6a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
24 changes: 12 additions & 12 deletions Data/Text.hs
Expand Up @@ -145,8 +145,8 @@ module Data.Text
, isInfixOf

-- ** View patterns
, prefixed
, suffixed
, stripPrefix
, stripSuffix

-- * Searching
, filter
Expand Down Expand Up @@ -1408,8 +1408,8 @@ isInfixOf needle haystack
--
-- 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:
Expand All @@ -1418,10 +1418,10 @@ isInfixOf needle haystack
-- > 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

Expand All @@ -1430,8 +1430,8 @@ prefixed p@(Text _arr _off plen) t@(Text arr off len)
--
-- 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:
Expand All @@ -1440,10 +1440,10 @@ prefixed p@(Text _arr _off plen) t@(Text arr off len)
-- > 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

Expand Down
24 changes: 12 additions & 12 deletions Data/Text/Lazy.hs
Expand Up @@ -150,8 +150,8 @@ module Data.Text.Lazy
, isInfixOf

-- ** View patterns
, prefixed
, suffixed
, stripPrefix
, stripSuffix

-- * Searching
, filter
Expand Down Expand Up @@ -1217,8 +1217,8 @@ isInfixOf needle haystack
--
-- 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:
Expand All @@ -1227,11 +1227,11 @@ isInfixOf needle haystack
-- > 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

Expand All @@ -1240,8 +1240,8 @@ prefixed p t
--
-- 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:
Expand All @@ -1250,11 +1250,11 @@ prefixed p t
-- > 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

Expand Down
23 changes: 9 additions & 14 deletions tests/Properties.hs
Expand Up @@ -583,18 +583,13 @@ tl_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packS s)
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`
Expand Down Expand Up @@ -1086,10 +1081,10 @@ tests = [
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
]
],

Expand Down

0 comments on commit 9cebb6a

Please sign in to comment.