Safe Haskell | None |
---|---|
Language | Haskell98 |
Manipulation of JavaScript strings, API and fusion implementation based on Data.Text by Tom Harper, Duncan Coutts, Bryan O'Sullivan e.a.
- data JSString
- pack :: String -> JSString
- unpack :: JSString -> String
- unpack' :: JSString -> String
- singleton :: Char -> JSString
- empty :: JSString
- cons :: Char -> JSString -> JSString
- snoc :: JSString -> Char -> JSString
- append :: JSString -> JSString -> JSString
- uncons :: JSString -> Maybe (Char, JSString)
- head :: JSString -> Char
- last :: JSString -> Char
- tail :: JSString -> JSString
- init :: JSString -> JSString
- null :: JSString -> Bool
- length :: JSString -> Int
- compareLength :: JSString -> Int -> Ordering
- map :: (Char -> Char) -> JSString -> JSString
- intercalate :: JSString -> [JSString] -> JSString
- intersperse :: Char -> JSString -> JSString
- transpose :: [JSString] -> [JSString]
- reverse :: JSString -> JSString
- replace :: JSString -> JSString -> JSString -> JSString
- toCaseFold :: JSString -> JSString
- toLower :: JSString -> JSString
- toUpper :: JSString -> JSString
- toTitle :: JSString -> JSString
- justifyLeft :: Int -> Char -> JSString -> JSString
- justifyRight :: Int -> Char -> JSString -> JSString
- center :: Int -> Char -> JSString -> JSString
- foldl :: (a -> Char -> a) -> a -> JSString -> a
- foldl' :: (a -> Char -> a) -> a -> JSString -> a
- foldl1 :: (Char -> Char -> Char) -> JSString -> Char
- foldl1' :: (Char -> Char -> Char) -> JSString -> Char
- foldr :: (Char -> a -> a) -> a -> JSString -> a
- foldr1 :: (Char -> Char -> Char) -> JSString -> Char
- concat :: [JSString] -> JSString
- concatMap :: (Char -> JSString) -> JSString -> JSString
- any :: (Char -> Bool) -> JSString -> Bool
- all :: (Char -> Bool) -> JSString -> Bool
- maximum :: JSString -> Char
- minimum :: JSString -> Char
- scanl :: (Char -> Char -> Char) -> Char -> JSString -> JSString
- scanl1 :: (Char -> Char -> Char) -> JSString -> JSString
- scanr :: (Char -> Char -> Char) -> Char -> JSString -> JSString
- scanr1 :: (Char -> Char -> Char) -> JSString -> JSString
- mapAccumL :: (a -> Char -> (a, Char)) -> a -> JSString -> (a, JSString)
- mapAccumR :: (a -> Char -> (a, Char)) -> a -> JSString -> (a, JSString)
- replicate :: Int -> JSString -> JSString
- unfoldr :: (a -> Maybe (Char, a)) -> a -> JSString
- unfoldrN :: Int -> (a -> Maybe (Char, a)) -> a -> JSString
- take :: Int -> JSString -> JSString
- takeEnd :: Int -> JSString -> JSString
- drop :: Int -> JSString -> JSString
- dropEnd :: Int -> JSString -> JSString
- takeWhile :: (Char -> Bool) -> JSString -> JSString
- dropWhile :: (Char -> Bool) -> JSString -> JSString
- dropWhileEnd :: (Char -> Bool) -> JSString -> JSString
- dropAround :: (Char -> Bool) -> JSString -> JSString
- strip :: JSString -> JSString
- stripStart :: JSString -> JSString
- stripEnd :: JSString -> JSString
- splitAt :: Int -> JSString -> (JSString, JSString)
- breakOn :: JSString -> JSString -> (JSString, JSString)
- breakOnEnd :: JSString -> JSString -> (JSString, JSString)
- break :: (Char -> Bool) -> JSString -> (JSString, JSString)
- span :: (Char -> Bool) -> JSString -> (JSString, JSString)
- group :: JSString -> [JSString]
- group' :: JSString -> [JSString]
- groupBy :: (Char -> Char -> Bool) -> JSString -> [JSString]
- inits :: JSString -> [JSString]
- tails :: JSString -> [JSString]
- splitOn :: JSString -> JSString -> [JSString]
- splitOn' :: JSString -> JSString -> [JSString]
- split :: (Char -> Bool) -> JSString -> [JSString]
- chunksOf :: Int -> JSString -> [JSString]
- chunksOf' :: Int -> JSString -> [JSString]
- lines :: JSString -> [JSString]
- lines' :: JSString -> [JSString]
- words :: JSString -> [JSString]
- words' :: JSString -> [JSString]
- unlines :: [JSString] -> JSString
- unwords :: [JSString] -> JSString
- isPrefixOf :: JSString -> JSString -> Bool
- isSuffixOf :: JSString -> JSString -> Bool
- isInfixOf :: JSString -> JSString -> Bool
- stripPrefix :: JSString -> JSString -> Maybe JSString
- stripSuffix :: JSString -> JSString -> Maybe JSString
- commonPrefixes :: JSString -> JSString -> Maybe (JSString, JSString, JSString)
- filter :: (Char -> Bool) -> JSString -> JSString
- breakOnAll :: JSString -> JSString -> [(JSString, JSString)]
- breakOnAll' :: JSString -> JSString -> [(JSString, JSString)]
- find :: (Char -> Bool) -> JSString -> Maybe Char
- partition :: (Char -> Bool) -> JSString -> (JSString, JSString)
- index :: JSString -> Int -> Char
- findIndex :: (Char -> Bool) -> JSString -> Maybe Int
- count :: JSString -> JSString -> Int
- zip :: JSString -> JSString -> [(Char, Char)]
- zipWith :: (Char -> Char -> Char) -> JSString -> JSString -> JSString
Documentation
A wrapper around a JavaScript string
Creation and elimination
singleton :: Char -> JSString Source
O(1) Convert a character into a JSString
. Subject to fusion.
Performs replacement on invalid scalar values.
Basic interface
cons :: Char -> JSString -> JSString infixr 5 Source
O(n) Adds a character to the front of a JSString
. This function
is more costly than its List
counterpart because it requires
copying a new array. Subject to fusion. Performs replacement on
invalid scalar values.
snoc :: JSString -> Char -> JSString Source
O(n) Adds a character to the end of a JSString
. This copies the
entire array in the process, unless fused. Subject to fusion.
Performs replacement on invalid scalar values.
head :: JSString -> Char Source
O(1) Returns the first character of a JSString
, which must be
non-empty. Subject to fusion.
last :: JSString -> Char Source
O(1) Returns the last character of a JSString
, which must be
non-empty. Subject to fusion.
tail :: JSString -> JSString Source
O(1) Returns all characters after the head of a JSString
, which
must be non-empty. Subject to fusion.
init :: JSString -> JSString Source
O(1) Returns all but the last character of a JSString
, which must
be non-empty. Subject to fusion.
length :: JSString -> Int Source
O(n) Returns the number of characters in a JSString
.
Subject to fusion.
compareLength :: JSString -> Int -> Ordering Source
Transformations
intercalate :: JSString -> [JSString] -> JSString Source
O(n) The intercalate
function takes a JSString
and a list of
JSString
s and concatenates the list after interspersing the first
argument between each element of the list.
intersperse :: Char -> JSString -> JSString Source
O(n) The intersperse
function takes a character and places it
between the characters of a JSString
. Subject to fusion. Performs
replacement on invalid scalar values.
:: JSString |
|
-> JSString |
|
-> JSString |
|
-> JSString |
O(m+n) Replace every non-overlapping occurrence of needle
in
haystack
with replacement
.
This function behaves as though it was defined as follows:
replace needle replacement haystack =intercalate
replacement (splitOn
needle haystack)
As this suggests, each occurrence is replaced exactly once. So if
needle
occurs in replacement
, that occurrence will not itself
be replaced recursively:
replace "oo" "foo" "oo" == "foo"
In cases where several instances of needle
overlap, only the
first one will be replaced:
replace "ofo" "bar" "ofofo" == "barfo"
In (unlikely) bad cases, this function's time complexity degrades towards O(n*m).
Case conversion
toCaseFold :: JSString -> JSString Source
O(n) Convert a string to folded case. Subject to fusion.
This function is mainly useful for performing caseless (also known as case insensitive) string comparisons.
A string x
is a caseless match for a string y
if and only if:
toCaseFold x == toCaseFold y
The result string may be longer than the input string, and may
differ from applying toLower
to the input string. For instance,
the Armenian small ligature "ﬓ" (men now, U+FB13) is case
folded to the sequence "մ" (men, U+0574) followed by
"ն" (now, U+0576), while the Greek "µ" (micro sign,
U+00B5) is case folded to "μ" (small letter mu, U+03BC)
instead of itself.
toLower :: JSString -> JSString Source
O(n) Convert a string to lower case, using simple case conversion. Subject to fusion.
The result string may be longer than the input string. For instance, "İ" (Latin capital letter I with dot above, U+0130) maps to the sequence "i" (Latin small letter i, U+0069) followed by " ̇" (combining dot above, U+0307).
toUpper :: JSString -> JSString Source
O(n) Convert a string to upper case, using simple case conversion. Subject to fusion.
The result string may be longer than the input string. For instance, the German "ß" (eszett, U+00DF) maps to the two-letter sequence "SS".
toTitle :: JSString -> JSString Source
O(n) Convert a string to title case, using simple case conversion. Subject to fusion.
The first letter of the input is converted to title case, as is every subsequent letter that immediately follows a non-letter. Every letter that immediately follows another letter is converted to lower case.
The result string may be longer than the input string. For example, the Latin small ligature fl (U+FB02) is converted to the sequence Latin capital letter F (U+0046) followed by Latin small letter l (U+006C).
Note: this function does not take language or culture specific rules into account. For instance, in English, different style guides disagree on whether the book name "The Hill of the Red Fox" is correctly title cased—but this function will capitalize every word.
Justification
justifyLeft :: Int -> Char -> JSString -> JSString Source
O(n) Left-justify a string to the given length, using the specified fill character on the right. Subject to fusion. Performs replacement on invalid scalar values.
Examples:
justifyLeft 7 'x' "foo" == "fooxxxx" justifyLeft 3 'x' "foobar" == "foobar"
justifyRight :: Int -> Char -> JSString -> JSString Source
O(n) Right-justify a string to the given length, using the specified fill character on the left. Performs replacement on invalid scalar values.
Examples:
justifyRight 7 'x' "bar" == "xxxxbar" justifyRight 3 'x' "foobar" == "foobar"
center :: Int -> Char -> JSString -> JSString Source
O(n) Center a string to the given length, using the specified fill character on either side. Performs replacement on invalid scalar values.
Examples:
center 8 'x' "HS" = "xxxHSxxx"
Folds
foldl' :: (a -> Char -> a) -> a -> JSString -> a Source
O(n) A strict version of foldl
. Subject to fusion.
foldl1' :: (Char -> Char -> Char) -> JSString -> Char Source
O(n) A strict version of foldl1
. Subject to fusion.
Special folds
Construction
Scans
Accumulating maps
mapAccumR :: (a -> Char -> (a, Char)) -> a -> JSString -> (a, JSString) Source
The mapAccumR
function behaves like a combination of map
and
a strict foldr
; it applies a function to each element of a
JSString
, passing an accumulating parameter from right to left, and
returning a final value of this accumulator together with the new
JSString
.
Performs replacement on invalid scalar values.
Generation and unfolding
unfoldr :: (a -> Maybe (Char, a)) -> a -> JSString Source
O(n), where n
is the length of the result. The unfoldr
function is analogous to the List unfoldr
. unfoldr
builds a
JSString
from a seed value. The function takes the element and
returns Nothing
if it is done producing the JSString
, otherwise
Just
(a,b)
. In this case, a
is the next Char
in the
string, and b
is the seed value for further production. Subject
to fusion. Performs replacement on invalid scalar values.
unfoldrN :: Int -> (a -> Maybe (Char, a)) -> a -> JSString Source
O(n) Like unfoldr
, unfoldrN
builds a JSString
from a seed
value. However, the length of the result should be limited by the
first argument to unfoldrN
. This function is more efficient than
unfoldr
when the maximum length of the result is known and
correct, otherwise its performance is similar to unfoldr
. Subject
to fusion. Performs replacement on invalid scalar values.
Substrings
Breaking strings
takeEnd :: Int -> JSString -> JSString Source
O(n) takeEnd
n
t
returns the suffix remaining after
taking n
characters from the end of t
.
Examples:
takeEnd 3 "foobar" == "bar"
dropEnd :: Int -> JSString -> JSString Source
O(n) dropEnd
n
t
returns the prefix remaining after
dropping n
characters from the end of t
.
Examples:
dropEnd 3 "foobar" == "foo"
dropWhileEnd :: (Char -> Bool) -> JSString -> JSString Source
O(n) dropWhileEnd
p
t
returns the prefix remaining after
dropping characters that fail the predicate p
from the end of
t
. Subject to fusion.
Examples:
dropWhileEnd (=='.') "foo..." == "foo"
dropAround :: (Char -> Bool) -> JSString -> JSString Source
O(n) dropAround
p
t
returns the substring remaining after
dropping characters that fail the predicate p
from both the
beginning and end of t
. Subject to fusion.
strip :: JSString -> JSString Source
O(n) Remove leading and trailing white space from a string. Equivalent to:
dropAround isSpace
stripStart :: JSString -> JSString Source
O(n) Remove leading white space from a string. Equivalent to:
dropWhile isSpace
stripEnd :: JSString -> JSString Source
O(n) Remove trailing white space from a string. Equivalent to:
dropWhileEnd isSpace
breakOn :: JSString -> JSString -> (JSString, JSString) Source
O(n+m) Find the first instance of needle
(which must be
non-null
) in haystack
. The first element of the returned tuple
is the prefix of haystack
before needle
is matched. The second
is the remainder of haystack
, starting with the match.
Examples:
breakOn "::" "a::b::c" ==> ("a", "::b::c") breakOn "/" "foobar" ==> ("foobar", "")
Laws:
append prefix match == haystack where (prefix, match) = breakOn needle haystack
If you need to break a string by a substring repeatedly (e.g. you
want to break on every instance of a substring), use breakOnAll
instead, as it has lower startup overhead.
In (unlikely) bad cases, this function's time complexity degrades towards O(n*m).
breakOnEnd :: JSString -> JSString -> (JSString, JSString) Source
O(n+m) Similar to breakOn
, but searches from the end of the
string.
The first element of the returned tuple is the prefix of haystack
up to and including the last match of needle
. The second is the
remainder of haystack
, following the match.
breakOnEnd "::" "a::b::c" ==> ("a::b::", "c")
span :: (Char -> Bool) -> JSString -> (JSString, JSString) Source
O(n) span
, applied to a predicate p
and text t
, returns
a pair whose first element is the longest prefix (possibly empty)
of t
of elements that satisfy p
, and whose second is the
remainder of the list.
groupBy :: (Char -> Char -> Bool) -> JSString -> [JSString] Source
O(n) Group characters in a string according to a predicate.
inits :: JSString -> [JSString] Source
O(n^2) Return all initial segments of the given JSString
, shortest
first.
tails :: JSString -> [JSString] Source
O(n^2) Return all final segments of the given JSString
, longest
first.
Breaking into many substrings
:: JSString | String to split on. If this string is empty, an error will occur. |
-> JSString | Input text. |
-> [JSString] |
O(m+n) Break a JSString
into pieces separated by the first JSString
argument (which cannot be empty), consuming the delimiter. An empty
delimiter is invalid, and will cause an error to be raised.
Examples:
splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"] splitOn "aaa" "aaaXaaaXaaaXaaa" == ["","X","X","X",""] splitOn "x" "x" == ["",""]
and
intercalate s . splitOn s == id splitOn (singleton c) == split (==c)
(Note: the string s
to split on above cannot be empty.)
In (unlikely) bad cases, this function's time complexity degrades towards O(n*m).
split :: (Char -> Bool) -> JSString -> [JSString] Source
O(n) Splits a JSString
into components delimited by separators,
where the predicate returns True for a separator element. The
resulting components do not contain the separators. Two adjacent
separators result in an empty component in the output. eg.
split (=='a') "aabbaca" == ["","","bb","c",""] split (=='a') "" == [""]
chunksOf :: Int -> JSString -> [JSString] Source
O(n) Splits a JSString
into components of length k
. The last
element may be shorter than the other chunks, depending on the
length of the input. Examples:
chunksOf 3 "foobarbaz" == ["foo","bar","baz"] chunksOf 4 "haskell.org" == ["hask","ell.","org"]
chunksOf' :: Int -> JSString -> [JSString] Source
O(n) Splits a JSString
into components of length k
. The last
element may be shorter than the other chunks, depending on the
length of the input. Examples:
chunksOf 3 "foobarbaz" == ["foo","bar","baz"] chunksOf 4 "haskell.org" == ["hask","ell.","org"]
Breaking into lines and words
unlines :: [JSString] -> JSString Source
O(n) Joins lines, after appending a terminating newline to each.
Predicates
isPrefixOf :: JSString -> JSString -> Bool Source
O(n) The isPrefixOf
function takes two JSString
s and returns
True
iff the first is a prefix of the second. Subject to fusion.
isSuffixOf :: JSString -> JSString -> Bool Source
O(n) The isSuffixOf
function takes two JSString
s and returns
True
iff the first is a suffix of the second.
View patterns
stripPrefix :: JSString -> JSString -> Maybe JSString Source
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
stripSuffix :: JSString -> JSString -> Maybe JSString Source
O(n) Return the prefix of the second string if its suffix matches the entire first string.
Examples:
stripSuffix "bar" "foobar" == Just "foo" stripSuffix "" "baz" == Just "baz" stripSuffix "foo" "quux" == Nothing
This is particularly useful with the ViewPatterns
extension to
GHC, as follows:
{-# LANGUAGE ViewPatterns #-} import Data.Text as T quuxLength :: Text -> Int quuxLength (stripSuffix "quux" -> Just pre) = T.length pre quuxLength _ = -1
commonPrefixes :: JSString -> JSString -> Maybe (JSString, JSString, JSString) Source
O(n) Find the longest non-empty common prefix of two strings and return it, along with the suffixes of each string at which they no longer match.
If the strings do not have a common prefix or either one is empty,
this function returns Nothing
.
Examples:
commonPrefixes "foobar" "fooquux" == Just ("foo","bar","quux") commonPrefixes "veeble" "fetzer" == Nothing commonPrefixes "" "baz" == Nothing
Searching
O(n+m) Find all non-overlapping instances of needle
in
haystack
. Each element of the returned list consists of a pair:
- The entire string prior to the kth match (i.e. the prefix)
- The kth match, followed by the remainder of the string
Examples:
breakOnAll "::" "" ==> [] breakOnAll "/" "a/b/c/" ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
In (unlikely) bad cases, this function's time complexity degrades towards O(n*m).
The needle
parameter may not be empty.