mcsp-algorithms-0.1.0: Algorithms for Minimum Common String Partition (MCSP) in Haskell.
Safe HaskellSafe-Inferred
LanguageGHC2021

MCSP.Data.String.Extra.Radix

Description

Radix operations on String.

Synopsis

Prefix

stripPrefix :: Eq a => String a -> String a -> Maybe (String a) Source #

O(min(m,n)) Removes the given prefix from a string.

Returns Nothing if the string does not start with the prefix given, or Just the string after the prefix.

>>> stripPrefix "foo" "foobar"
Just bar
>>> stripPrefix "foo" "foo"
Just
>>> stripPrefix "foo" "barfoo"
Nothing

isPrefixOf :: Eq a => String a -> String a -> Bool Source #

O(min(m,n)) Returns True iff the first string is a prefix of the second.

>>> "Hello" `isPrefixOf` "Hello World!"
True
>>> "Hello" `isPrefixOf` "Wello Horld!"
False

commonPrefix :: Eq a => String a -> String a -> String a Source #

O(min(m,n)) The maximum common prefix of two strings.

>>> commonPrefix "abc" "abd"
ab
>>> commonPrefix "xyz" "xyz"
xyz
>>> commonPrefix "def" "ghi"

splitCommonPrefix :: Eq a => String a -> String a -> (String a, String a, String a) Source #

O(min(m,n)) Returns the maximum common prefix of two strings and the remainder of each string.

Note that splitCommonPrefix a b is equivalent to let p = commonPrefix a b in (p, fromJust (splitPrefix p a), fromJust (splitPrefix p b)), but slightly more efficient and cannot fail.

>>> splitCommonPrefix "abc" "abd"
(ab,c,d)
>>> splitCommonPrefix "xyz" "xyz"
(xyz,,)
>>> splitCommonPrefix "def" "ghi"
(,def,ghi)

Infix

stripInfix :: Eq a => String a -> String a -> Maybe (String a, String a) Source #

O(n m) Return the the string before (prefix) and after (suffix) the search string, or Nothing if the search string is not present.

A result of Just (s1, s2) = stripInfix s r means that s1 ++ r ++ s2 == s.

>>> stripInfix "el" "Hello"
Just (H,lo)
>>> stripInfix "World!" "Hello"
Nothing
>>> stripInfix "si" "mississipi"
Just (mis,ssipi)
>>> stripInfix "chat" "hat"
Nothing
>>> stripInfix "word" "word"
Just (,)

isInfixOf :: Eq a => String a -> String a -> Bool Source #

O(n m) Check if the first string is contained anywhere within the second string.

>>> isInfixOf "Haskell" "I really like Haskell."
True
>>> isInfixOf "Ial" "I really like Haskell."
False

Suffix

stripSuffix :: Eq a => String a -> String a -> Maybe (String a) Source #

O(min(m,n)) Returns the prefix of the second string if its suffix matches the first string.

Returns Nothing if the string does not end with the suffix given, or Just the string before the suffix.

>>> stripSuffix "bar" "foobar"
Just foo
>>> stripSuffix "" "baz"
Just baz
>>> stripSuffix "foo" "quux"
Nothing

isSuffixOf :: Eq a => String a -> String a -> Bool Source #

O(min(m,n)) Returns True iff the first string is a prefix of the second.

>>> "ld!" `isSuffixOf` "Hello World!"
True
>>> "World" `isSuffixOf` "Hello World!"
False

suffixes :: String a -> [String a] Source #

O(n) Extract all non-empty suffixes of a string.

>>> suffixes "Hello"
[Hello,ello,llo,lo,o]