stripPrefix

The stripPrefix function drops the given prefix from a list. It returns Nothing if the list did not start with the prefix given, or Just the list after the prefix, if it does.
Examples
>>> stripPrefix "foo" "foobar"
Just "bar"
>>> stripPrefix "foo" "foo"
Just ""
>>> stripPrefix "foo" "barfoo"
Nothing
>>> stripPrefix "foo" "barfoobaz"
Nothing
O(n) The stripPrefix function takes two ByteStrings and returns Just the remainder of the second iff the first is its prefix, and otherwise Nothing.
O(n) The stripPrefix function takes two ShortByteStrings and returns Just the remainder of the second iff the first is its prefix, and otherwise Nothing.
O(n) Return the suffix of the second string if its prefix matches the entire first string. Examples:
>>> stripPrefix "foo" "foobar"
Just "bar"
>>> stripPrefix ""    "baz"
Just "baz"
>>> stripPrefix "foo" "quux"
Nothing
This is particularly useful with the ViewPatterns extension to GHC, as follows:
{-# LANGUAGE ViewPatterns #-}
import Data.Text as T

fnordLength :: Text -> Int
fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
fnordLength _                                 = -1
O(n) Return the suffix of the second string if its prefix matches the entire first string. Examples:
stripPrefix "foo" "foobar" == Just "bar"
stripPrefix ""    "baz"    == Just "baz"
stripPrefix "foo" "quux"   == Nothing
This is particularly useful with the ViewPatterns extension to GHC, as follows:
{-# LANGUAGE ViewPatterns #-}
import Data.Text.Lazy as T

fnordLength :: Text -> Int
fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
fnordLength _                                 = -1
Remove a prefix from a path.
stripPrefix "/foo/" "/foo/bar/baz.txt" == Just "bar/baz.txt"
stripPrefix "/foo/" "/bar/baz.txt" == Nothing
This function operates on logical prefixes, rather than by counting characters. The prefix "/foo/bar/baz" is interpreted the path ("/foo/bar/", "baz"), and will be stripped accordingly:
stripPrefix "/foo/bar/baz" "/foo/bar/baz/qux" == Nothing
stripPrefix "/foo/bar/baz" "/foo/bar/baz.txt" == Just ".txt"
Since: 0.4.1
The stripPrefix function drops the given prefix from a list. It returns Nothing if the list did not start with the prefix given, or Just the list after the prefix, if it does.
>>> stripPrefix "foo" "foobar"
Just "bar"
>>> stripPrefix "foo" "foo"
Just ""
>>> stripPrefix "foo" "barfoo"
Nothing
>>> stripPrefix "foo" "barfoobaz"
Nothing
stripPrefix drops the given prefix from a sequence. It returns Nothing if the sequence did not start with the prefix given, or Just the sequence after the prefix, if it does.
> stripPrefix "foo" "foobar"
Just "bar"
> stripPrefix "abc" "foobar"
Nothing
Strip prefix from second ShortText argument. Returns Nothing if first argument is not a prefix of the second argument.
>>> stripPrefix "text-" "text-short"
Just "short"
>>> stripPrefix "test-" "text-short"
Nothing
Remove a prefix from a path
O(n) Return the suffix of the second string if its prefix matches the entire first string.
O(n) The stripPrefix function takes two ShortTexts and returns Just the remainder of the second iff the first is its prefix, and otherwise Nothing.
Remove a prefix from a listlike if possible
O(n) Return the suffix of the second string if its prefix matches the entire first string. Examples:
stripPrefix "foo" "foobar" == Just "bar"
stripPrefix ""    "baz"    == Just "baz"
stripPrefix "foo" "quux"   == Nothing
This is particularly useful with the ViewPatterns extension to GHC, as follows:
{-# LANGUAGE ViewPatterns #-}
import Data.Text as T

fnordLength :: JSString -> Int
fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf
fnordLength _                                 = -1
stripPrefix prefix stream strips prefix from stream if it is a prefix of stream. Returns Nothing if the stream does not start with the given prefix, stripped stream otherwise. Returns Just nil when the prefix is the same as the stream. See also "Streamly.Internal.Data.Stream.IsStream.Nesting.dropPrefix". Space: O(1)
O(n) The stripPrefix function takes two OsStrings and returns Just the remainder of the second iff the first is its prefix, and otherwise Nothing.
O(n) The stripPrefix function takes two OsStrings and returns Just the remainder of the second iff the first is its prefix, and otherwise Nothing.
O(n) The stripPrefix function takes two OsStrings and returns Just the remainder of the second iff the first is its prefix, and otherwise Nothing.
stripPrefix prefix input strips the prefix stream from the input stream if it is a prefix of input. Returns Nothing if the input does not start with the given prefix, stripped input otherwise. Returns Just nil when the prefix is the same as the input stream. Space: O(1)
Try to strip a prefix from the start of a String. If the prefix is not starting the string, then Nothing is returned, otherwise the striped string is returned
Try to strip a prefix from a collection
If a list is a prefix of an infinite list, strip it and return the rest. Otherwise return Nothing.
Strips a prefix from a MonoidMap. If map m1 is a prefix of map m2, then stripPrefix m1 m2 will produce a reduced map where prefix m1 is stripped from m2.

Properties

The stripPrefix function, when applied to maps m1 and m2, produces a result if (and only if) m1 is a prefix of m2:
isJust (stripPrefix m1 m2) == m1 `isPrefixOf` m2
The value for any key k in the result is identical to the result of stripping the value for k in map m1 from the value for k in map m2:
all
(\r -> Just (get k r) == stripPrefix (get k m1) (get k m2))
(stripPrefix m1 m2)
If we append prefix m1 to the left-hand side of the result, we can always recover the original map m2:
all
(\r -> m1 <> r == m2)
(stripPrefix m1 m2)
This function provides the definition of stripPrefix for the MonoidMap instance of LeftReductive.

Examples

With String values:
>>> m1 = fromList [(1, ""   ), (2, "i"  ), (3, "pq" ), (4, "xyz")]
>>> m2 = fromList [(1, "abc"), (2, "ijk"), (3, "pqr"), (4, "xyz")]
>>> m3 = fromList [(1, "abc"), (2,  "jk"), (3,   "r"), (4,    "")]
>>> stripPrefix m1 m2 == Just m3
True
>>> stripPrefix m2 m1 == Nothing
True
With Sum Natural values:
>>> m1 = fromList [("a", 0), ("b", 1), ("c", 2), ("d", 3)]
>>> m2 = fromList [("a", 3), ("b", 3), ("c", 3), ("d", 3)]
>>> m3 = fromList [("a", 3), ("b", 2), ("c", 1), ("d", 0)]
>>> stripPrefix m1 m2 == Just m3
True
>>> stripPrefix m2 m1 == Nothing
True