on master help sign in
Rename prefixed -> stripPrefix, suffixed -> stripSuffix

Gives us consistency with Data.List --HG-- extra : convert_revision : 7b496f8cff0fbfa84c8a416eac690b8faa0e7d1b

commit9cebb6ad29f9cb0e46bb262efb0fe1fb90cdbf0e authorBryan O'Sullivan <bos@serpentine.com> dateWed 8 Sep 2010, 05:45 (16 years ago) parent6f2c867
3 files changed+33−38
Data/Text.hs+12−12 Data/Text/Lazy.hs+12−12 tests/Properties.hs+9−14
Data/Text.hs+12−12
@@ -145,8 +145,8 @@ module Data.Text
145145 , isInfixOf
146146
147147 -- ** View patterns
148− , prefixed
149− , suffixed
148+ , stripPrefix
149+ , stripSuffix
150150
151151 -- * Searching
152152 , filter
@@ -1408,8 +1408,8 @@ isInfixOf needle haystack
14081408--
14091409-- Examples:
14101410--
1411−-- > prefixed "foo" "foobar" == Just "bar"
1412−-- > prefixed "foo" "quux" == Nothing
1411+-- > stripPrefix "foo" "foobar" == Just "bar"
1412+-- > stripPrefix "foo" "quux" == Nothing
14131413--
14141414-- This is particularly useful with the @ViewPatterns@ extension to
14151415-- GHC, as follows:
@@ -1418,10 +1418,10 @@ isInfixOf needle haystack
14181418-- > import Data.Text as T
14191419-- >
14201420-- > fnordLength :: Text -> Int
1421−-- > fnordLength (prefixed "fnord" -> Just suf) = T.length suf
1421+-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
14221422-- > fnordLength _ = -1
1423−prefixed :: Text -> Text -> Maybe Text
1424−prefixed p@(Text _arr _off plen) t@(Text arr off len)
1423+stripPrefix :: Text -> Text -> Maybe Text
1424+stripPrefix p@(Text _arr _off plen) t@(Text arr off len)
14251425 | p `isPrefixOf` t = Just $! textP arr (off+plen) (len-plen)
14261426 | otherwise = Nothing
14271427
@@ -1430,8 +1430,8 @@ prefixed p@(Text _arr _off plen) t@(Text arr off len)
14301430--
14311431-- Examples:
14321432--
1433−-- > suffixed "bar" "foobar" == Just "foo"
1434−-- > suffixed "foo" "quux" == Nothing
1433+-- > stripSuffix "bar" "foobar" == Just "foo"
1434+-- > stripSuffix "foo" "quux" == Nothing
14351435--
14361436-- This is particularly useful with the @ViewPatterns@ extension to
14371437-- GHC, as follows:
@@ -1440,10 +1440,10 @@ prefixed p@(Text _arr _off plen) t@(Text arr off len)
14401440-- > import Data.Text as T
14411441-- >
14421442-- > quuxLength :: Text -> Int
1443−-- > quuxLength (suffixed "quux" -> Just pre) = T.length pre
1443+-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
14441444-- > quuxLength _ = -1
1445−suffixed :: Text -> Text -> Maybe Text
1446−suffixed p@(Text _arr _off plen) t@(Text arr off len)
1445+stripSuffix :: Text -> Text -> Maybe Text
1446+stripSuffix p@(Text _arr _off plen) t@(Text arr off len)
14471447 | p `isSuffixOf` t = Just $! textP arr off (len-plen)
14481448 | otherwise = Nothing
14491449
Data/Text/Lazy.hs+12−12
@@ -150,8 +150,8 @@ module Data.Text.Lazy
150150 , isInfixOf
151151
152152 -- ** View patterns
153− , prefixed
154− , suffixed
153+ , stripPrefix
154+ , stripSuffix
155155
156156 -- * Searching
157157 , filter
@@ -1217,8 +1217,8 @@ isInfixOf needle haystack
12171217--
12181218-- Examples:
12191219--
1220−-- > prefixed "foo" "foobar" == Just "bar"
1221−-- > prefixed "foo" "quux" == Nothing
1220+-- > stripPrefix "foo" "foobar" == Just "bar"
1221+-- > stripPrefix "foo" "quux" == Nothing
12221222--
12231223-- This is particularly useful with the @ViewPatterns@ extension to
12241224-- GHC, as follows:
@@ -1227,11 +1227,11 @@ isInfixOf needle haystack
12271227-- > import Data.Text as T
12281228-- >
12291229-- > fnordLength :: Text -> Int
1230−-- > fnordLength (prefixed "fnord" -> Just suf) = T.length suf
1230+-- > fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
12311231-- > fnordLength _ = -1
1232−prefixed :: Text -> Text -> Maybe Text
1232+stripPrefix :: Text -> Text -> Maybe Text
12331233-- Yes, this could be much more efficient.
1234−prefixed p t
1234+stripPrefix p t
12351235 | p `isPrefixOf` t = Just (drop (length p) t)
12361236 | otherwise = Nothing
12371237
@@ -1240,8 +1240,8 @@ prefixed p t
12401240--
12411241-- Examples:
12421242--
1243−-- > suffixed "bar" "foobar" == Just "foo"
1244−-- > suffixed "foo" "quux" == Nothing
1243+-- > stripSuffix "bar" "foobar" == Just "foo"
1244+-- > stripSuffix "foo" "quux" == Nothing
12451245--
12461246-- This is particularly useful with the @ViewPatterns@ extension to
12471247-- GHC, as follows:
@@ -1250,11 +1250,11 @@ prefixed p t
12501250-- > import Data.Text as T
12511251-- >
12521252-- > quuxLength :: Text -> Int
1253−-- > quuxLength (suffixed "quux" -> Just pre) = T.length pre
1253+-- > quuxLength (stripSuffix "quux" -> Just pre) = T.length pre
12541254-- > quuxLength _ = -1
1255−suffixed :: Text -> Text -> Maybe Text
1255+stripSuffix :: Text -> Text -> Maybe Text
12561256-- Yes, this could be much more efficient.
1257−suffixed p t
1257+stripSuffix p t
12581258 | p `isSuffixOf` t = Just (take (length t - length p) t)
12591259 | otherwise = Nothing
12601260
tests/Properties.hs+9−14
@@ -583,18 +583,13 @@ tl_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packS s)
583583t_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packS s)
584584tl_isInfixOf s = L.isInfixOf s `eqP` TL.isInfixOf (packS s)
585585
586−prefixed (p:ps) (t:ts)
587− | p == t = prefixed ps ts
588−prefixed [] ts = Just ts
589−prefixed _ _ = Nothing
586+t_stripPrefix s = (fmap packS . L.stripPrefix s) `eqP` T.stripPrefix (packS s)
587+tl_stripPrefix s = (fmap packS . L.stripPrefix s) `eqP` TL.stripPrefix (packS s)
590588
591−t_prefixed s = (fmap packS . prefixed s) `eqP` T.prefixed (packS s)
592−tl_prefixed s = (fmap packS . prefixed s) `eqP` TL.prefixed (packS s)
589+stripSuffix p t = reverse `fmap` L.stripPrefix (reverse p) (reverse t)
593590
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)
597−tl_suffixed s = (fmap packS . suffixed s) `eqP` TL.suffixed (packS s)
591+t_stripSuffix s = (fmap packS . stripSuffix s) `eqP` T.stripSuffix (packS s)
592+tl_stripSuffix s = (fmap packS . stripSuffix s) `eqP` TL.stripSuffix (packS s)
598593
599594sf_elem p c = (L.elem c . L.filter p) `eqP` (S.elem c . S.filter p)
600595sf_filter q p = (L.filter p . L.filter q) `eqP`
@@ -1086,10 +1081,10 @@ tests = [
10861081 testProperty "tl_isInfixOf" tl_isInfixOf,
10871082
10881083 testGroup "view" [
1089− testProperty "t_prefixed" t_prefixed,
1090− testProperty "tl_prefixed" tl_prefixed,
1091− testProperty "t_suffixed" t_suffixed,
1092− testProperty "tl_suffixed" tl_suffixed
1084+ testProperty "t_stripPrefix" t_stripPrefix,
1085+ testProperty "tl_stripPrefix" tl_stripPrefix,
1086+ testProperty "t_stripSuffix" t_stripSuffix,
1087+ testProperty "tl_stripSuffix" tl_stripSuffix
10931088 ]
10941089 ],
10951090