on master help sign in
Rename prefixed -> stripPrefix, suffixed -> stripSuffix
9cebb6aBryan O'Sullivan
Data/Text.hs+12−12
@@ -145,8 +145,8 @@ module Data.Text
145 , isInfixOf
145 , isInfixOf
146
146
147 -- ** View patterns
147 -- ** View patterns
148− , prefixed
148+ , stripPrefix
149− , suffixed
149+ , stripSuffix
150
150
151 -- * Searching
151 -- * Searching
152 , filter
152 , filter
@@ -1408,8 +1408,8 @@ isInfixOf needle haystack
1408--
1408--
1409-- Examples:
1409-- Examples:
1410--
1410--
1411−-- > prefixed "foo" "foobar" == Just "bar"
1411+-- > stripPrefix "foo" "foobar" == Just "bar"
1412−-- > prefixed "foo" "quux" == Nothing
1412+-- > stripPrefix "foo" "quux" == Nothing
1413--
1413--
1414-- This is particularly useful with the @ViewPatterns@ extension to
1414-- This is particularly useful with the @ViewPatterns@ extension to
1415-- GHC, as follows:
1415-- GHC, as follows:
@@ -1418,10 +1418,10 @@ isInfixOf needle haystack
1418-- > import Data.Text as T
1418-- > import Data.Text as T
1419-- >
1419-- >
1420-- > fnordLength :: Text -> Int
1420-- > fnordLength :: Text -> Int
1421−-- > fnordLength (prefixed "fnord" -> Just suf) = T.length suf
1421+-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1422-- > fnordLength _ = -1
1422-- > fnordLength _ = -1
1423−prefixed :: Text -> Text -> Maybe Text
1423+stripPrefix :: Text -> Text -> Maybe Text
1424−prefixed p@(Text _arr _off plen) t@(Text arr off len)
1424+stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
1425 | p `isPrefixOf` t = Just $! textP arr (off+plen) (len-plen)
1425 | p `isPrefixOf` t = Just $! textP arr (off+plen) (len-plen)
1426 | otherwise = Nothing
1426 | otherwise = Nothing
1427
1427
@@ -1430,8 +1430,8 @@ prefixed p@(Text _arr _off plen) t@(Text arr off len)
1430--
1430--
1431-- Examples:
1431-- Examples:
1432--
1432--
1433−-- > suffixed "bar" "foobar" == Just "foo"
1433+-- > stripSuffix "bar" "foobar" == Just "foo"
1434−-- > suffixed "foo" "quux" == Nothing
1434+-- > stripSuffix "foo" "quux" == Nothing
1435--
1435--
1436-- This is particularly useful with the @ViewPatterns@ extension to
1436-- This is particularly useful with the @ViewPatterns@ extension to
1437-- GHC, as follows:
1437-- GHC, as follows:
@@ -1440,10 +1440,10 @@ prefixed p@(Text _arr _off plen) t@(Text arr off len)
1440-- > import Data.Text as T
1440-- > import Data.Text as T
1441-- >
1441-- >
1442-- > quuxLength :: Text -> Int
1442-- > quuxLength :: Text -> Int
1443−-- > quuxLength (suffixed "quux" -> Just pre) = T.length pre
1443+-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
1444-- > quuxLength _ = -1
1444-- > quuxLength _ = -1
1445−suffixed :: Text -> Text -> Maybe Text
1445+stripSuffix :: Text -> Text -> Maybe Text
1446−suffixed p@(Text _arr _off plen) t@(Text arr off len)
1446+stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
1447 | p `isSuffixOf` t = Just $! textP arr off (len-plen)
1447 | p `isSuffixOf` t = Just $! textP arr off (len-plen)
1448 | otherwise = Nothing
1448 | otherwise = Nothing
1449
1449
Data/Text/Lazy.hs+12−12
@@ -150,8 +150,8 @@ module Data.Text.Lazy
150 , isInfixOf
150 , isInfixOf
151
151
152 -- ** View patterns
152 -- ** View patterns
153− , prefixed
153+ , stripPrefix
154− , suffixed
154+ , stripSuffix
155
155
156 -- * Searching
156 -- * Searching
157 , filter
157 , filter
@@ -1217,8 +1217,8 @@ isInfixOf needle haystack
1217--
1217--
1218-- Examples:
1218-- Examples:
1219--
1219--
1220−-- > prefixed "foo" "foobar" == Just "bar"
1220+-- > stripPrefix "foo" "foobar" == Just "bar"
1221−-- > prefixed "foo" "quux" == Nothing
1221+-- > stripPrefix "foo" "quux" == Nothing
1222--
1222--
1223-- This is particularly useful with the @ViewPatterns@ extension to
1223-- This is particularly useful with the @ViewPatterns@ extension to
1224-- GHC, as follows:
1224-- GHC, as follows:
@@ -1227,11 +1227,11 @@ isInfixOf needle haystack
1227-- > import Data.Text as T
1227-- > import Data.Text as T
1228-- >
1228-- >
1229-- > fnordLength :: Text -> Int
1229-- > fnordLength :: Text -> Int
1230−-- > fnordLength (prefixed "fnord" -> Just suf) = T.length suf
1230+-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
1231-- > fnordLength _ = -1
1231-- > fnordLength _ = -1
1232−prefixed :: Text -> Text -> Maybe Text
1232+stripPrefix :: Text -> Text -> Maybe Text
1233-- Yes, this could be much more efficient.
1233-- Yes, this could be much more efficient.
1234−prefixed p t
1234+stripPrefix p t
1235 | p `isPrefixOf` t = Just (drop (length p) t)
1235 | p `isPrefixOf` t = Just (drop (length p) t)
1236 | otherwise = Nothing
1236 | otherwise = Nothing
1237
1237
@@ -1240,8 +1240,8 @@ prefixed p t
1240--
1240--
1241-- Examples:
1241-- Examples:
1242--
1242--
1243−-- > suffixed "bar" "foobar" == Just "foo"
1243+-- > stripSuffix "bar" "foobar" == Just "foo"
1244−-- > suffixed "foo" "quux" == Nothing
1244+-- > stripSuffix "foo" "quux" == Nothing
1245--
1245--
1246-- This is particularly useful with the @ViewPatterns@ extension to
1246-- This is particularly useful with the @ViewPatterns@ extension to
1247-- GHC, as follows:
1247-- GHC, as follows:
@@ -1250,11 +1250,11 @@ prefixed p t
1250-- > import Data.Text as T
1250-- > import Data.Text as T
1251-- >
1251-- >
1252-- > quuxLength :: Text -> Int
1252-- > quuxLength :: Text -> Int
1253−-- > quuxLength (suffixed "quux" -> Just pre) = T.length pre
1253+-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
1254-- > quuxLength _ = -1
1254-- > quuxLength _ = -1
1255−suffixed :: Text -> Text -> Maybe Text
1255+stripSuffix :: Text -> Text -> Maybe Text
1256-- Yes, this could be much more efficient.
1256-- Yes, this could be much more efficient.
1257−suffixed p t
1257+stripSuffix p t
1258 | p `isSuffixOf` t = Just (take (length t - length p) t)
1258 | p `isSuffixOf` t = Just (take (length t - length p) t)
1259 | otherwise = Nothing
1259 | otherwise = Nothing
1260
1260
tests/Properties.hs+9−14
@@ -583,18 +583,13 @@ tl_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packS s)
583t_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packS s)
583t_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packS s)
584tl_isInfixOf s = L.isInfixOf s `eqP` TL.isInfixOf (packS s)
584tl_isInfixOf s = L.isInfixOf s `eqP` TL.isInfixOf (packS s)
585
585
586−prefixed (p:ps) (t:ts)
586+t_stripPrefix s = (fmap packS . L.stripPrefix s) `eqP` T.stripPrefix (packS s)
587− | p == t = prefixed ps ts
587+tl_stripPrefix s = (fmap packS . L.stripPrefix s) `eqP` TL.stripPrefix (packS s)
588−prefixed [] ts = Just ts
589−prefixed _ _ = Nothing
590
588
591−t_prefixed s = (fmap packS . prefixed s) `eqP` T.prefixed (packS s)
589+stripSuffix p t = reverse `fmap` L.stripPrefix (reverse p) (reverse t)
592−tl_prefixed s = (fmap packS . prefixed s) `eqP` TL.prefixed (packS s)
593
590
594−suffixed p t = reverse `fmap` prefixed (reverse p) (reverse t)
595−
596−t_suffixed s = (fmap packS . suffixed s) `eqP` T.suffixed (packS s)
591+t_stripSuffix s = (fmap packS . stripSuffix s) `eqP` T.stripSuffix (packS s)
597−tl_suffixed s = (fmap packS . suffixed s) `eqP` TL.suffixed (packS s)
592+tl_stripSuffix s = (fmap packS . stripSuffix s) `eqP` TL.stripSuffix (packS s)
598
593
599sf_elem p c = (L.elem c . L.filter p) `eqP` (S.elem c . S.filter p)
594sf_elem p c = (L.elem c . L.filter p) `eqP` (S.elem c . S.filter p)
600sf_filter q p = (L.filter p . L.filter q) `eqP`
595sf_filter q p = (L.filter p . L.filter q) `eqP`
@@ -1086,10 +1081,10 @@ tests = [
1086 testProperty "tl_isInfixOf" tl_isInfixOf,
1081 testProperty "tl_isInfixOf" tl_isInfixOf,
1087
1082
1088 testGroup "view" [
1083 testGroup "view" [
1089− testProperty "t_prefixed" t_prefixed,
1084+ testProperty "t_stripPrefix" t_stripPrefix,
1090− testProperty "tl_prefixed" tl_prefixed,
1085+ testProperty "tl_stripPrefix" tl_stripPrefix,
1091− testProperty "t_suffixed" t_suffixed,
1086+ testProperty "t_stripSuffix" t_stripSuffix,
1092− testProperty "tl_suffixed" tl_suffixed
1087+ testProperty "tl_stripSuffix" tl_stripSuffix
1093 ]
1088 ]
1094 ],
1089 ],
1095
1090