diff --git a/Data/Text.hs b/Data/Text.hs index 8e9a69d2..41853fb4 100644 --- a/Data/Text.hs +++ b/Data/Text.hs @@ -145,8 +145,8 @@ module Data.Text , isInfixOf -- ** View patterns - , prefixed - , suffixed + , stripPrefix + , stripSuffix -- * Searching , filter @@ -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: @@ -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 @@ -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: @@ -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 diff --git a/Data/Text/Lazy.hs b/Data/Text/Lazy.hs index ed74c5ea..e640ee10 100644 --- a/Data/Text/Lazy.hs +++ b/Data/Text/Lazy.hs @@ -150,8 +150,8 @@ module Data.Text.Lazy , isInfixOf -- ** View patterns - , prefixed - , suffixed + , stripPrefix + , stripSuffix -- * Searching , filter @@ -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: @@ -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 @@ -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: @@ -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 diff --git a/tests/Properties.hs b/tests/Properties.hs index 5f952190..7a995e4e 100644 --- a/tests/Properties.hs +++ b/tests/Properties.hs @@ -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` @@ -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 ] ],