Module

Data.String

Package
purescript-strings
Repository
purescript/purescript-strings

Re-exports from Data.String.CodePoints

#CodePoint Source

newtype CodePoint

CodePoint is an Int bounded between 0 and 0x10FFFF, corresponding to Unicode code points.

Instances

#uncons Source

uncons :: String -> Maybe { head :: CodePoint, tail :: String }

Returns a record with the first code point and the remaining code points of the string. Returns Nothing if the string is empty. Operates in constant space and time.

>>> uncons "๐€๐€ c ๐€"
Just { head: CodePoint 0x1D400, tail: "๐€ c ๐€" }
>>> uncons ""
Nothing

#toCodePointArray Source

toCodePointArray :: String -> Array CodePoint

Creates an array of code points from a string. Operates in space and time linear to the length of the string.

>>> codePointArray = toCodePointArray "b ๐€๐€"
>>> codePointArray
[CodePoint 0x62, CodePoint 0x20, CodePoint 0x1D400, CodePoint 0x1D400]
>>> map singleton codePointArray
["b", " ", "๐€", "๐€"]

#takeWhile Source

takeWhile :: (CodePoint -> Boolean) -> String -> String

Returns a string containing the leading sequence of code points which all match the given predicate from the string. Operates in constant space and in time linear to the length of the string.

>>> takeWhile (\c -> codePointToInt c == 0x1D400) "๐€๐€ b c ๐€"
"๐€๐€"

#take Source

take :: Int -> String -> String

Returns a string containing the given number of code points from the beginning of the given string. If the string does not have that many code points, returns the empty string. Operates in constant space and in time linear to the given number.

>>> take 3 "b ๐€๐€ c ๐€"
"b ๐€"
-- compare to Data.String:
>>> take 3 "b ๐€๐€ c ๐€"
"b ๏ฟฝ"

#stripSuffix Source

stripSuffix :: Pattern -> String -> Maybe String

If the string ends with the given suffix, return the portion of the string left after removing it, as a Just value. Otherwise, return Nothing.

stripSuffix (Pattern ".exe") "psc.exe" == Just "psc"
stripSuffix (Pattern ".exe") "psc" == Nothing

#stripPrefix Source

stripPrefix :: Pattern -> String -> Maybe String

If the string starts with the given prefix, return the portion of the string left after removing it, as a Just value. Otherwise, return Nothing.

stripPrefix (Pattern "http:") "http://purescript.org" == Just "//purescript.org"
stripPrefix (Pattern "http:") "https://purescript.org" == Nothing

#splitAt Source

splitAt :: Int -> String -> { after :: String, before :: String }

Splits a string into two substrings, where before contains the code points up to (but not including) the given index, and after contains the rest of the string, from that index on.

>>> splitAt 3 "b ๐€๐€ c ๐€"
{ before: "b ๐€", after: "๐€ c ๐€" }

Thus the length of (splitAt i s).before will equal either i or length s, if that is shorter. (Or if i is negative the length will be 0.)

In code:

length (splitAt i s).before == min (max i 0) (length s)
(splitAt i s).before <> (splitAt i s).after == s
splitAt i s == {before: take i s, after: drop i s}

#singleton Source

singleton :: CodePoint -> String

Creates a string containing just the given code point. Operates in constant space and time.

>>> map singleton (codePointFromInt 0x1D400)
Just "๐€"

#length Source

length :: String -> Int

Returns the number of code points in the string. Operates in constant space and in time linear to the length of the string.

>>> length "b ๐€๐€ c ๐€"
8
-- compare to Data.String:
>>> length "b ๐€๐€ c ๐€"
11

#lastIndexOf' Source

lastIndexOf' :: Pattern -> Int -> String -> Maybe Int

Returns the number of code points preceding the first match of the given pattern in the string. Pattern matches following the given index will be ignored. Returns Nothing when no matches are found.

>>> lastIndexOf' (Pattern "๐€") 5 "b ๐€๐€ c ๐€"
Just 3
>>> lastIndexOf' (Pattern "o") 5 "b ๐€๐€ c ๐€"
Nothing

#lastIndexOf Source

lastIndexOf :: Pattern -> String -> Maybe Int

Returns the number of code points preceding the last match of the given pattern in the string. Returns Nothing when no matches are found.

>>> lastIndexOf (Pattern "๐€") "b ๐€๐€ c ๐€"
Just 7
>>> lastIndexOf (Pattern "o") "b ๐€๐€ c ๐€"
Nothing

#indexOf' Source

indexOf' :: Pattern -> Int -> String -> Maybe Int

Returns the number of code points preceding the first match of the given pattern in the string. Pattern matches preceding the given index will be ignored. Returns Nothing when no matches are found.

>>> indexOf' (Pattern "๐€") 4 "b ๐€๐€ c ๐€"
Just 7
>>> indexOf' (Pattern "o") 4 "b ๐€๐€ c ๐€"
Nothing

#indexOf Source

indexOf :: Pattern -> String -> Maybe Int

Returns the number of code points preceding the first match of the given pattern in the string. Returns Nothing when no matches are found.

>>> indexOf (Pattern "๐€") "b ๐€๐€ c ๐€"
Just 2
>>> indexOf (Pattern "o") "b ๐€๐€ c ๐€"
Nothing

#fromCodePointArray Source

fromCodePointArray :: Array CodePoint -> String

Creates a string from an array of code points. Operates in space and time linear to the length of the array.

>>> codePointArray = toCodePointArray "c ๐€"
>>> codePointArray
[CodePoint 0x63, CodePoint 0x20, CodePoint 0x1D400]
>>> fromCodePointArray codePointArray
"c ๐€"

#dropWhile Source

dropWhile :: (CodePoint -> Boolean) -> String -> String

Drops the leading sequence of code points which all match the given predicate from the string. Operates in constant space and in time linear to the length of the string.

>>> dropWhile (\c -> codePointToInt c == 0x1D400) "๐€๐€ b c ๐€"
" b c ๐€"

#drop Source

drop :: Int -> String -> String

Drops the given number of code points from the beginning of the string. If the string does not have that many code points, returns the empty string. Operates in constant space and in time linear to the given number.

>>> drop 5 "๐€๐€ b c"
"c"
-- compared to Data.String:
>>> drop 5 "๐€๐€ b c"
"b c" -- because "๐€" occupies 2 code units

#countPrefix Source

countPrefix :: (CodePoint -> Boolean) -> String -> Int

Returns the number of code points in the leading sequence of code points which all match the given predicate. Operates in constant space and in time linear to the length of the string.

>>> countPrefix (\c -> codePointToInt c == 0x1D400) "๐€๐€ b c ๐€"
2

#contains Source

contains :: Pattern -> String -> Boolean

Checks whether the pattern appears in the given string.

contains (Pattern "needle") "haystack with needle" == true
contains (Pattern "needle") "haystack" == false

#codePointFromChar Source

codePointFromChar :: Char -> CodePoint

Creates a CodePoint from a given Char.

>>> codePointFromChar 'B'
CodePoint 0x42 -- represents 'B'

#codePointAt Source

codePointAt :: Int -> String -> Maybe CodePoint

Returns the first code point of the string after dropping the given number of code points from the beginning, if there is such a code point. Operates in constant space and in time linear to the given index.

>>> codePointAt 1 "๐€๐€๐€๐€"
Just (CodePoint 0x1D400) -- represents "๐€"
-- compare to Data.String:
>>> charAt 1 "๐€๐€๐€๐€"
Just '๏ฟฝ'

Re-exports from Data.String.Common

#trim Source

trim :: String -> String

Removes whitespace from the beginning and end of a string, including whitespace characters and line terminators.

trim "   Hello  \n World\n\t    " == "Hello  \n World"

#toUpper Source

toUpper :: String -> String

Returns the argument converted to uppercase.

toUpper "Hello" == "HELLO"

#toLower Source

toLower :: String -> String

Returns the argument converted to lowercase.

toLower "hElLo" == "hello"

#split Source

split :: Pattern -> String -> Array String

Returns the substrings of the second string separated along occurences of the first string.

split (Pattern " ") "hello world" == ["hello", "world"]

#replaceAll Source

replaceAll :: Pattern -> Replacement -> String -> String

Replaces all occurences of the pattern with the replacement string.

replaceAll (Pattern "<=") (Replacement "โ‰ค") "a <= b <= c" == "a โ‰ค b โ‰ค c"

#replace Source

replace :: Pattern -> Replacement -> String -> String

Replaces the first occurence of the pattern with the replacement string.

replace (Pattern "<=") (Replacement "โ‰ค") "a <= b <= c" == "a โ‰ค b <= c"

#null Source

null :: String -> Boolean

Returns true if the given string is empty.

null "" == true
null "Hi" == false

#localeCompare Source

localeCompare :: String -> String -> Ordering

Compare two strings in a locale-aware fashion. This is in contrast to the Ord instance on String which treats strings as arrays of code units:

"รค" `localeCompare` "b" == LT
"รค" `compare` "b" == GT

#joinWith Source

joinWith :: String -> Array String -> String

Joins the strings in the array together, inserting the first argument as separator between them.

joinWith ", " ["apple", "banana", "orange"] == "apple, banana, orange"

Re-exports from Data.String.Pattern

#Replacement Source

newtype Replacement

A newtype used in cases to specify a replacement for a pattern.

Constructors

Instances

#Pattern Source

newtype Pattern

A newtype used in cases where there is a string to be matched.

pursPattern = Pattern ".purs"
--can be used like this:
contains pursPattern "Test.purs"
   == true

Constructors

Instances