Equations
- s₁.decidableLT s₂ = s₁.data.decidableLT s₂.data
Equations
Instances For
Adds a character to the end of a string.
The internal implementation uses dynamic arrays and will perform destructive updates if the string is not shared.
Examples:
Instances For
Appends two strings. Usually accessed via the ++
operator.
The internal implementation will perform destructive updates if the string is not shared.
Examples:
"abc".append "def" = "abcdef"
"abc" ++ "def" = "abcdef"
"" ++ "" = ""
Instances For
Converts a string to a list of characters.
Even though the logical model of strings is as a structure that wraps a list of characters, this operation takes time and space linear in the length of the string. At runtime, strings are represented as dynamic arrays of bytes.
Examples:
Instances For
Returns true
if p
is a valid UTF-8 position in the string s
.
This means that p ≤ s.endPos
and p
lies on a UTF-8 character boundary. At runtime, this
operation takes constant time.
Examples:
String.Pos.isValid "abc" ⟨0⟩ = true
String.Pos.isValid "abc" ⟨1⟩ = true
String.Pos.isValid "abc" ⟨3⟩ = true
String.Pos.isValid "abc" ⟨4⟩ = false
String.Pos.isValid "𝒫(A)" ⟨0⟩ = true
String.Pos.isValid "𝒫(A)" ⟨1⟩ = false
String.Pos.isValid "𝒫(A)" ⟨2⟩ = false
String.Pos.isValid "𝒫(A)" ⟨3⟩ = false
String.Pos.isValid "𝒫(A)" ⟨4⟩ = true
Equations
- String.Pos.isValid s p = String.Pos.isValid.go p s.data 0
Instances For
Returns the character at position p
of a string. If p
is not a valid position, returns the
fallback value (default : Char)
, which is 'A'
, but does not panic.
This function is overridden with an efficient implementation in runtime code. See
String.utf8GetAux
for the reference implementation.
Examples:
"abc".get ⟨1⟩ = 'b'
"abc".get ⟨3⟩ = (default : Char)
because byte3
is at the end of the string."L∃∀N".get ⟨2⟩ = (default : Char)
because byte2
is in the middle of'∃'
.
Equations
- { data := s_1 }.get p = String.utf8GetAux s_1 0 p
Instances For
Returns the character at position p
of a string. If p
is not a valid position, returns none
.
This function is overridden with an efficient implementation in runtime code. See
String.utf8GetAux?
for the reference implementation.
Examples:
Equations
- { data := s }.get? x✝ = String.utf8GetAux? s 0 x✝
Instances For
Returns the character at position p
of a string. Panics if p
is not a valid position.
See String.get?
for a safer alternative.
This function is overridden with an efficient implementation in runtime code. See
String.utf8GetAux
for the reference implementation.
Examples
"abc".get! ⟨1⟩ = 'b'
Equations
- { data := s_1 }.get! p = String.utf8GetAux s_1 0 p
Instances For
Replaces the character at a specified position in a string with a new character. If the position is invalid, the string is returned unchanged.
If both the replacement character and the replaced character are 7-bit ASCII characters and the string is not shared, then it is updated in-place and not copied.
Examples:
"abc".set ⟨1⟩ 'B' = "aBc"
"abc".set ⟨3⟩ 'D' = "abc"
"L∃∀N".set ⟨4⟩ 'X' = "L∃XN"
"L∃∀N".set ⟨2⟩ 'X' = "L∃∀N"
because'∃'
is a multi-byte character, so the byte index2
is an invalid position.
Equations
- { data := s }.set x✝¹ x✝ = { data := String.utf8SetAux x✝ s 0 x✝¹ }
Instances For
Replaces the character at position p
in the string s
with the result of applying f
to that
character. If p
is an invalid position, the string is returned unchanged.
If both the replacement character and the replaced character are 7-bit ASCII characters and the string is not shared, then it is updated in-place and not copied.
Examples:
abc.modify ⟨1⟩ Char.toUpper = "aBc"
abc.modify ⟨3⟩ Char.toUpper = "abc"
Instances For
Returns the next position in a string after position p
. The result is unspecified if p
is not a
valid position or if p = s.endPos
.
A run-time bounds check is performed to determine whether p
is at the end of the string. If a
bounds check has already been performed, use String.next'
to avoid a repeated check.
Some examples where the result is unspecified:
"abc".next ⟨3⟩
, since3 = "abc".endPos
"L∃∀N".next ⟨2⟩
, since2
points into the middle of a multi-byte UTF-8 character
Examples:
Instances For
Returns the position in a string before a specified position, p
. If p = ⟨0⟩
, returns 0
. If p
is not a valid position, the result is unspecified.
For example, "L∃∀N".prev ⟨3⟩
is unspecified, since byte 3 occurs in the middle of the multi-byte
character '∃'
.
Examples:
"abc".get ("abc".endPos |> "abc".prev) = 'c'
"L∃∀N".get ("L∃∀N".endPos |> "L∃∀N".prev |> "L∃∀N".prev |> "L∃∀N".prev) = '∃'
Instances For
Returns true
if a specified byte position is greater than or equal to the position which points to
the end of a string. Otherwise, returns false
.
Examples:
(0 |> "abc".next |> "abc".next |> "abc".atEnd) = false
(0 |> "abc".next |> "abc".next |> "abc".next |> "abc".next |> "abc".atEnd) = true
(0 |> "L∃∀N".next |> "L∃∀N".next |> "L∃∀N".next |> "L∃∀N".atEnd) = false
(0 |> "L∃∀N".next |> "L∃∀N".next |> "L∃∀N".next |> "L∃∀N".next |> "L∃∀N".atEnd) = true
"abc".atEnd ⟨4⟩ = true
"L∃∀N".atEnd ⟨7⟩ = false
"L∃∀N".atEnd ⟨8⟩ = true
Instances For
Returns the character at position p
of a string. Returns (default : Char)
, which is 'A'
, if
p
is not a valid position.
Requires evidence, h
, that p
is within bounds instead of performing a run-time bounds check as
in String.get
.
A typical pattern combines get'
with a dependent if
-expression to avoid the overhead of an
additional bounds check. For example:
def getInBounds? (s : String) (p : String.Pos) : Option Char :=
if h : s.atEnd p then none else some (s.get' p h)
Even with evidence of ¬ s.atEnd p
, p
may be invalid if a byte index points into the middle of a
multi-byte UTF-8 character. For example, "L∃∀N".get' ⟨2⟩ (by decide) = (default : Char)
.
Examples:
"abc".get' 0 (by decide) = 'a'
let lean := "L∃∀N"; lean.get' (0 |> lean.next |> lean.next) (by decide) = '∀'
Equations
- { data := s_2 }.get' p h_2 = String.utf8GetAux s_2 0 p
Instances For
Returns the next position in a string after position p
. The result is unspecified if p
is not a
valid position.
Requires evidence, h
, that p
is within bounds. No run-time bounds check is performed, as in
String.next
.
A typical pattern combines String.next'
with a dependent if
-expression to avoid the overhead of
an additional bounds check. For example:
def next? (s: String) (p : String.Pos) : Option Char :=
if h : s.atEnd p then none else s.get (s.next' p h)
Example:
Instances For
Equations
Instances For
Finds the position of the first character in a string for which the Boolean predicate p
returns
true
. If there is no such character in the string, then the end position of the string is
returned.
Examples:
Instances For
Finds the position of the last character in a string for which the Boolean predicate p
returns
true
. If there is no such character in the string, then none
is returned.
Examples:
"coffee tea water".revFind (·.isWhitespace) = some ⟨10⟩
"tea".revFind (· == 'X') = none
"".revFind (· == 'X') = none
Equations
- s.revFind p = s.revFindAux p s.endPos
Instances For
Returns the first position where the two strings differ.
If one string is a prefix of the other, then the returned position is the end position of the shorter string. If the strings are identical, then their end position is returned.
Examples:
"tea".firstDiffPos "ten" = ⟨2⟩
"tea".firstDiffPos "tea" = ⟨3⟩
"tea".firstDiffPos "teas" = ⟨3⟩
"teas".firstDiffPos "tea" = ⟨3⟩
Equations
- a.firstDiffPos b = String.firstDiffPos.loop a b (a.endPos.min b.endPos) 0
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Creates a new string that consists of the region of the input string delimited by the two positions.
The result is ""
if the start position is greater than or equal to the end position or if the
start position is at the end of the string. If either position is invalid (that is, if either points
at the middle of a multi-byte UTF-8 character) then the result is unspecified.
Examples:
"red green blue".extract ⟨0⟩ ⟨3⟩ = "red"
"red green blue".extract ⟨3⟩ ⟨0⟩ = ""
"red green blue".extract ⟨0⟩ ⟨100⟩ = "red green blue"
"red green blue".extract ⟨4⟩ ⟨100⟩ = "green blue"
"L∃∀N".extract ⟨2⟩ ⟨100⟩ = "green blue"
Equations
Instances For
Splits a string at each character for which p
returns true
.
The characters that satisfy p
are not included in any of the resulting strings. If multiple
characters in a row satisfy p
, then the resulting list will contain empty strings.
Examples:
"coffee tea water".split (·.isWhitespace) = ["coffee", "tea", "water"]
"coffee tea water".split (·.isWhitespace) = ["coffee", "", "tea", "", "water"]
"fun x =>\n x + 1\n".split (· == '\n') = ["fun x =>", " x + 1", ""]
Instances For
Auxiliary for splitOn
. Preconditions:
It represents the state where we have currently parsed some split parts into r
(in reverse order),
b
is the beginning of the string / the end of the previous match of sep
, and the first j
bytes
of sep
match the bytes i-j .. i
of s
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Splits a string s
on occurrences of the separator string sep
. The default separator is " "
.
When sep
is empty, the result is [s]
. When sep
occurs in overlapping patterns, the first match
is taken. There will always be exactly n+1
elements in the returned list if there were n
non-overlapping matches of sep
in the string. The separators are not included in the returned
substrings.
Examples:
"here is some text ".splitOn = ["here", "is", "some", "text", ""]
"here is some text ".splitOn "some" = ["here is ", " text "]
"here is some text ".splitOn "" = ["here is some text "]
"ababacabac".splitOn "aba" = ["", "bac", "c"]
Instances For
Equations
- String.instInhabited = { default := "" }
Equations
- String.instAppend = { append := String.append }
Adds multiple repetitions of a character to the end of a string.
Returns s
, with n
repetitions of c
at the end. Internally, the implementation repeatedly calls
String.push
, so the string is modified in-place if there is a unique reference to it.
Examples:
Equations
- s.pushn c n = Nat.repeat (fun (s : String) => s.push c) n s
Instances For
Appends all the strings in a list of strings, in order.
Use String.intercalate
to place a separator string between the strings in a list.
Examples:
String.join ["gr", "ee", "n"] = "green"
String.join ["b", "", "l", "", "ue"] = "red"
String.join [] = ""
Equations
- String.join l = List.foldl (fun (r s : String) => r ++ s) "" l
Instances For
Returns a new string that contains only the character c
.
Because strings are encoded in UTF-8, the resulting string may take multiple bytes.
Examples:
String.singleton 'L' = "L"
String.singleton ' ' = " "
String.singleton '"' = "\""
String.singleton '𝒫' = "𝒫"
Equations
- String.singleton c = "".push c
Instances For
Appends the strings in a list of strings, placing the separator s
between each pair.
Examples:
", ".intercalate ["red", "green", "blue"] = "red, green, blue"
" and ".intercalate ["tea", "coffee"] = "tea and coffee"
" | ".intercalate ["M", "", "N"] = "M | | N"
Equations
- s.intercalate [] = ""
- s.intercalate (a :: as) = String.intercalate.go a s as
Instances For
Equations
- String.intercalate.go acc s (a :: as) = String.intercalate.go (acc ++ s ++ a) s as
- String.intercalate.go acc s [] = acc
Instances For
An iterator over the characters (Unicode code points) in a String
. Typically created by
String.iter
.
String iterators pair a string with a valid byte index. This allows efficient character-by-character processing of strings while avoiding the need to manually ensure that byte indices are used with the correct strings.
An iterator is valid if the position i
is valid for the string s
, meaning 0 ≤ i ≤ s.endPos
and i
lies on a UTF8 byte boundary. If i = s.endPos
, the iterator is at the end of the string.
Most operations on iterators return unspecified values if the iterator is not valid. The functions
in the String.Iterator
API rule out the creation of invalid iterators, with two exceptions:
Iterator.next iter
is invalid ifiter
is already at the end of the string (iter.atEnd
istrue
), andIterator.forward iter n
/Iterator.nextn iter n
is invalid ifn
is strictly greater than the number of remaining characters.
- s : String
The string being iterated over.
- i : Pos
The current UTF-8 byte position in the string
s
.This position is not guaranteed to be valid for the string. If the position is not valid, then the current character is
(default : Char)
, similar toString.get
on an invalid position.
Instances For
Equations
- String.instInhabitedIterator = { default := { s := default, i := default } }
Creates an iterator at the beginning of the string.
Equations
- s.mkIterator = { s := s, i := 0 }
Instances For
Creates an iterator at the beginning of the string.
Equations
Instances For
The size of a string iterator is the number of bytes remaining.
Recursive functions that iterate towards the end of a string will typically decrease this measure.
Equations
- String.instSizeOfIterator = { sizeOf := fun (i : String.Iterator) => i.s.utf8ByteSize - i.i.byteIdx }
The string being iterated over.
Equations
Instances For
The number of UTF-8 bytes remaining in the iterator.
Instances For
The current UTF-8 byte position in the string s
.
This position is not guaranteed to be valid for the string. If the position is not valid, then the
current character is (default : Char)
, similar to String.get
on an invalid position.
Equations
Instances For
Gets the character at the iterator's current position.
A run-time bounds check is performed. Use String.Iterator.curr'
to avoid redundant bounds checks.
If the position is invalid, returns (default : Char)
.
Instances For
Moves the iterator's position forward by one character, unconditionally.
It is only valid to call this function if the iterator is not at the end of the string (i.e.
if Iterator.atEnd
is false
); otherwise, the resulting iterator will be invalid.
Instances For
Gets the character at the iterator's current position.
The proof of it.hasNext
ensures that there is, in fact, a character at the current position. This
function is faster that String.Iterator.curr
due to avoiding a run-time bounds check.
Instances For
Moves the iterator's position forward by one character, unconditionally.
The proof of it.hasNext
ensures that there is, in fact, a position that's one character forwards.
This function is faster that String.Iterator.next
due to avoiding a run-time bounds check.
Instances For
Replaces the current character in the string.
Does nothing if the iterator is at the end of the string. If both the replacement character and the replaced character are 7-bit ASCII characters and the string is not shared, then it is updated in-place and not copied.
Instances For
Extracts the substring between the positions of two iterators. The first iterator's position is the start of the substring, and the second iterator's position is the end.
Returns the empty string if the iterators are for different strings, or if the position of the first iterator is past the position of the second iterator.
Equations
Instances For
Moves the iterator's position forward by the specified number of characters.
The resulting iterator is only valid if the number of characters to skip is less than or equal to the number of characters left in the iterator.
Instances For
The remaining characters in an iterator, as a string.
Equations
- { s := s, i := i }.remainingToString = s.extract i s.endPos
Instances For
Moves the iterator's position forward by the specified number of characters.
The resulting iterator is only valid if the number of characters to skip is less than or equal to the number of characters left in the iterator.
Instances For
Returns the character index that corresponds to the provided position (i.e. UTF-8 byte index) in a string.
If the position is at the end of the string, then the string's length in characters is returned. If the position is invalid due to pointing at the middle of a UTF-8 byte sequence, then the character index of the next character after the position is returned.
Examples:
"L∃∀N".offsetOfPos ⟨0⟩ = 0
"L∃∀N".offsetOfPos ⟨1⟩ = 1
"L∃∀N".offsetOfPos ⟨2⟩ = 2
"L∃∀N".offsetOfPos ⟨4⟩ = 2
"L∃∀N".offsetOfPos ⟨5⟩ = 3
"L∃∀N".offsetOfPos ⟨50⟩ = 4
Equations
- s.offsetOfPos pos = s.offsetOfPosAux pos 0 0
Instances For
Equations
- String.foldlAux f s stopPos i a = if h : i < stopPos then let_fun this := ⋯; String.foldlAux f s stopPos (s.next i) (f a (s.get i)) else a
Instances For
Folds a function over a string from the left, accumulating a value starting with init
. The
accumulated value is combined with each character in order, using f
.
Examples:
"coffee tea water".foldl (fun n c => if c.isWhitespace then n + 1 else n) 0 = 2
"coffee tea and water".foldl (fun n c => if c.isWhitespace then n + 1 else n) 0 = 3
"coffee tea water".foldl (·.push ·) "" = "coffee tea water"
Equations
- String.foldl f init s = String.foldlAux f s s.endPos 0 init
Instances For
Equations
- String.foldrAux f a s i begPos = if h : begPos < i then let_fun this := ⋯; let i := s.prev i; let a := f (s.get i) a; String.foldrAux f a s i begPos else a
Instances For
Folds a function over a string from the right, accumulating a value starting with init
. The
accumulated value is combined with each character in reverse order, using f
.
Examples:
"coffee tea water".foldr (fun c n => if c.isWhitespace then n + 1 else n) 0 = 2
"coffee tea and water".foldr (fun c n => if c.isWhitespace then n + 1 else n) 0 = 3
"coffee tea water".foldr (fun c s => c.push s) "" = "retaw dna aet eeffoc"
Equations
- String.foldr f init s = String.foldrAux f init s s.endPos 0
Instances For
Checks whether there is a character in a string for which the Boolean predicate p
returns true
.
Short-circuits at the first character for which p
returns true
.
Examples:
"brown".any (·.isLetter) = true
"brown".any (·.isWhitespace) = false
"brown and orange".any (·.isLetter) = true
"".any (fun _ => false) = false
Instances For
Applies the function f
to every character in a string, returning a string that contains the
resulting characters.
Examples:
"abc123".map Char.toUpper = "ABC123"
"".map Char.toUpper = ""
Equations
- String.map f s = String.mapAux f 0 s
Instances For
Checks whether the string can be interpreted as the decimal representation of a natural number.
A string can be interpreted as a decimal natural number if it is not empty and all the characters in it are digits.
Use String.toNat?
or String.toNat!
to convert such a string to a natural number.
Examples:
"".isNat = false
"0".isNat = true
"5".isNat = true
"05".isNat = true
"587".isNat = true
"-587".isNat = false
" 5".isNat = false
"2+3".isNat = false
"0xff".isNat = false
Instances For
Interprets a string as the decimal representation of a natural number, returning it. Returns none
if the string does not contain a decimal natural number.
A string can be interpreted as a decimal natural number if it is not empty and all the characters in it are digits.
Use String.isNat
to check whether String.toNat?
would return some
. String.toNat!
is an
alternative that panics instead of returning none
when the string is not a natural number.
Examples:
"".toNat? = none
"0".toNat? = some 0
"5".toNat? = some 5
"587".toNat? = some 587
"-587".toNat? = none
" 5".toNat? = none
"2+3".toNat? = none
"0xff".toNat? = none
Equations
Instances For
Checks whether substrings of two strings are equal. Substrings are indicated by their starting
positions and a size in UTF-8 bytes. Returns false
if the indicated substring does not exist in
either string.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Checks whether the first string (p
) is a prefix of the second (s
).
String.startsWith
is a version that takes the potential prefix after the string.
Examples:
"red".isPrefixOf "red green blue" = true
"green".isPrefixOf "red green blue" = false
"".isPrefixOf "red green blue" = true
Equations
- p.isPrefixOf s = p.substrEq 0 s 0 p.endPos.byteIdx
Instances For
Returns the position of the beginning of the line that contains the position pos
.
Lines are ended by '\n'
, and the returned position is either 0 : String.Pos
or immediately after
a '\n'
character.
Equations
- s.findLineStart pos = match s.revFindAux (fun (x : Char) => decide (x = '\n')) pos with | none => 0 | some n => { byteIdx := n.byteIdx + 1 }
Instances For
Returns an iterator into the underlying string, at the substring's starting position. The ending position is discarded, so the iterator alone cannot be used to determine whether its current position is within the original substring.
Equations
- { str := s, startPos := b, stopPos := e }.toIterator = { s := s, i := b }
Instances For
Returns the character at the given position in the substring.
The position is relative to the substring, rather than the underlying string, and no bounds checking
is performed with respect to the substring's end position. If the relative position is not a valid
position in the underlying string, the fallback value (default : Char)
, which is 'A'
, is
returned. Does not panic.
Instances For
Returns the next position in a substring after the given position. If the position is at the end of the substring, it is returned unmodified.
Both the input position and the returned position are interpreted relative to the substring's start position, not the underlying string.
Equations
Instances For
Returns the previous position in a substring, just prior to the given position. If the position is at the beginning of the substring, it is returned unmodified.
Both the input position and the returned position are interpreted relative to the substring's start position, not the underlying string.
Equations
Instances For
Returns the position that's the specified number of characters forward from the given position in a substring. If the end position of the substring is reached, it is returned.
Both the input position and the returned position are interpreted relative to the substring's start position, not the underlying string.
Instances For
Returns the position that's the specified number of characters prior to the given position in a substring. If the start position of the substring is reached, it is returned.
Both the input position and the returned position are interpreted relative to the substring's start position, not the underlying string.
Instances For
Returns the first character in the substring.
If the substring is empty, but the substring's start position is a valid position in the underlying
string, then the character at the start position is returned. If the substring's start position is
not a valid position in the string, the fallback value (default : Char)
, which is 'A'
, is
returned. Does not panic.
Instances For
Returns the substring-relative position of the first occurrence of c
in s
, or s.bsize
if c
doesn't occur.
Equations
Instances For
Removes the specified number of characters (Unicode code points) from the beginning of a substring by advancing its start position.
If the substring's end position is reached, the start position is not advanced past it.
Equations
Instances For
Removes the specified number of characters (Unicode code points) from the end of a substring by moving its end position towards its start position.
If the substring's start position is reached, the end position is not retracted past it.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Retains only the specified number of characters (Unicode code points) at the beginning of a substring, by moving its end position towards its start position.
If the substring's start position is reached, the end position is not retracted past it.
Equations
Instances For
Retains only the specified number of characters (Unicode code points) at the end of a substring, by moving its start position towards its end position.
If the substring's end position is reached, the start position is not advanced past it.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Checks whether a position in a substring is precisely equal to its ending position.
The position is understood relative to the substring's starting position, rather than the underlying string's starting position.
Instances For
Returns the region of the substring delimited by the provided start and stop positions, as a substring. The positions are interpreted with respect to the substring's start position, rather than the underlying string.
If the resulting substring is empty, then the resulting substring is a substring of the empty string
""
. Otherwise, the underlying string is that of the input substring with the beginning and end
positions adjusted.
Equations
Instances For
Splits a substring s
on occurrences of the separator string sep
. The default separator is " "
.
When sep
is empty, the result is [s]
. When sep
occurs in overlapping patterns, the first match
is taken. There will always be exactly n+1
elements in the returned list if there were n
non-overlapping matches of sep
in the string. The separators are not included in the returned
substrings, which are all substrings of s
's string.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Folds a function over a substring from the left, accumulating a value starting with init
. The
accumulated value is combined with each character in order, using f
.
Equations
- Substring.foldl f init { str := s_1, startPos := b, stopPos := e } = String.foldlAux f s_1 e b init
Instances For
Folds a function over a substring from the right, accumulating a value starting with init
. The
accumulated value is combined with each character in reverse order, using f
.
Equations
- Substring.foldr f init { str := s_1, startPos := b, stopPos := e } = String.foldrAux f init s_1 e b
Instances For
Checks whether the Boolean predicate p
returns true
for any character in a substring.
Short-circuits at the first character for which p
returns true
.
Instances For
Equations
Instances For
Retains only the longest prefix of a substring in which a Boolean predicate returns true
for all
characters by moving the substring's end position towards its start position.
Equations
- { str := s, startPos := b, stopPos := e }.takeWhile x✝ = { str := s, startPos := b, stopPos := Substring.takeWhileAux s e x✝ b }
Instances For
Removes the longest prefix of a substring in which a Boolean predicate returns true
for all
characters by moving the substring's start position. The start position is moved to the position of
the first character for which the predicate returns false
, or to the substring's end position if
the predicate always returns true
.
Equations
- { str := s, startPos := b, stopPos := e }.dropWhile x✝ = { str := s, startPos := Substring.takeWhileAux s e x✝ b, stopPos := e }
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Retains only the longest suffix of a substring in which a Boolean predicate returns true
for all
characters by moving the substring's start position towards its end position.
Equations
- { str := s, startPos := b, stopPos := e }.takeRightWhile x✝ = { str := s, startPos := Substring.takeRightWhileAux s b x✝ e, stopPos := e }
Instances For
Removes the longest suffix of a substring in which a Boolean predicate returns true
for all
characters by moving the substring's end position. The end position is moved just after the position
of the last character for which the predicate returns false
, or to the substring's start position
if the predicate always returns true
.
Equations
- { str := s, startPos := b, stopPos := e }.dropRightWhile x✝ = { str := s, startPos := b, stopPos := Substring.takeRightWhileAux s b x✝ e }
Instances For
Removes leading whitespace from a substring by moving its start position to the first non-whitespace character, or to its end position if there is no non-whitespace character.
“Whitespace” is defined as characters for which Char.isWhitespace
returns true
.
Equations
Instances For
Removes trailing whitespace from a substring by moving its end position to the last non-whitespace character, or to its start position if there is no non-whitespace character.
“Whitespace” is defined as characters for which Char.isWhitespace
returns true
.
Equations
Instances For
Removes leading and trailing whitespace from a substring by first moving its start position to the first non-whitespace character, and then moving its end position to the last non-whitespace character.
If the substring consists only of whitespace, then the resulting substring's start position is moved to its end position.
“Whitespace” is defined as characters for which Char.isWhitespace
returns true
.
Examples:
" red green blue ".toSubstring.trim.toString = "red green blue"
" red green blue ".toSubstring.trim.startPos = ⟨1⟩
" red green blue ".toSubstring.trim.stopPos = ⟨15⟩
" ".toSubstring.trim.startPos = ⟨5⟩
Equations
- One or more equations did not get rendered due to their size.
Instances For
Checks whether the substring can be interpreted as the decimal representation of a natural number.
A substring can be interpreted as a decimal natural number if it is not empty and all the characters in it are digits.
Use Substring.toNat?
to convert such a substring to a natural number.
Instances For
Checks whether the substring can be interpreted as the decimal representation of a natural number, returning the number if it can.
A substring can be interpreted as a decimal natural number if it is not empty and all the characters in it are digits.
Use Substring.isNat
to check whether the substring is such a substring.
Equations
Instances For
Checks whether two substrings represent equal strings. Usually accessed via the ==
operator.
Two substrings do not need to have the same underlying string or the same start and end positions; instead, they are equal if they contain the same sequence of characters.
Equations
Instances For
Returns the longest common prefix of two substrings.
The returned substring uses the same underlying string as s
.
Equations
- s.commonPrefix t = { str := s.str, startPos := s.startPos, stopPos := Substring.commonPrefix.loop s t s.startPos t.startPos }
Instances For
Returns the ending position of the common prefix, working up from spos, tpos
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Returns the longest common suffix of two substrings.
The returned substring uses the same underlying string as s
.
Equations
- s.commonSuffix t = { str := s.str, startPos := Substring.commonSuffix.loop s t s.stopPos t.stopPos, stopPos := s.stopPos }
Instances For
Returns the starting position of the common prefix, working down from spos, tpos
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
If pre
is a prefix of s
, returns the remainder. Returns none
otherwise.
The substring pre
is a prefix of s
if there exists a t : Substring
such that
s.toString = pre.toString ++ t.toString
. If so, the result is the substring of s
without the
prefix.
Equations
- s.dropPrefix? pre = if (s.commonPrefix pre).bsize = pre.bsize then some { str := s.str, startPos := (s.commonPrefix pre).stopPos, stopPos := s.stopPos } else none
Instances For
If suff
is a suffix of s
, returns the remainder. Returns none
otherwise.
The substring suff
is a suffix of s
if there exists a t : Substring
such that
s.toString = t.toString ++ suff.toString
. If so, the result the substring of s
without the
suffix.
Equations
- s.dropSuffix? suff = if (s.commonSuffix suff).bsize = suff.bsize then some { str := s.str, startPos := s.startPos, stopPos := (s.commonSuffix suff).startPos } else none
Instances For
Removes the specified number of characters (Unicode code points) from the start of the string.
If n
is greater than s.length
, returns ""
.
Examples:
"red green blue".drop 4 = "green blue"
"red green blue".drop 10 = "blue"
"red green blue".drop 50 = ""
Equations
- s.drop n = (s.toSubstring.drop n).toString
Instances For
Removes the specified number of characters (Unicode code points) from the end of the string.
If n
is greater than s.length
, returns ""
.
Examples:
"red green blue".dropRight 5 = "red green"
"red green blue".dropRight 11 = "red"
"red green blue".dropRight 50 = ""
Equations
- s.dropRight n = (s.toSubstring.dropRight n).toString
Instances For
Creates a new string that contains the first n
characters (Unicode code points) of s
.
If n
is greater than s.length
, returns s
.
Examples:
"red green blue".take 3 = "red"
"red green blue".take 1 = "r"
"red green blue".take 0 = ""
"red green blue".take 100 = "red green blue"
Equations
- s.take n = (s.toSubstring.take n).toString
Instances For
Creates a new string that contains the last n
characters (Unicode code points) of s
.
If n
is greater than s.length
, returns s
.
Examples:
"red green blue".takeRight 4 = "blue"
"red green blue".takeRight 1 = "e"
"red green blue".takeRight 0 = ""
"red green blue".takeRight 100 = "red green blue"
Equations
- s.takeRight n = (s.toSubstring.takeRight n).toString
Instances For
Creates a new string that contains the longest prefix of s
in which p
returns true
for all
characters.
Examples:
"red green blue".takeWhile (·.isLetter) = "red"
"red green blue".takeWhile (· == 'r') = "r"
"red green blue".takeWhile (· != 'n') = "red gree"
"red green blue".takeWhile (fun _ => true) = "red green blue"
Equations
- s.takeWhile p = (s.toSubstring.takeWhile p).toString
Instances For
Creates a new string by removing the longest prefix from s
in which p
returns true
for all
characters.
Examples:
"red green blue".dropWhile (·.isLetter) = " green blue"
"red green blue".dropWhile (· == 'r') = "ed green blue"
"red green blue".dropWhile (· != 'n') = "n blue"
"red green blue".dropWhile (fun _ => true) = ""
Equations
- s.dropWhile p = (s.toSubstring.dropWhile p).toString
Instances For
Creates a new string that contains the longest suffix of s
in which p
returns true
for all
characters.
Examples:
"red green blue".takeRightWhile (·.isLetter) = "blue"
"red green blue".takeRightWhile (· == 'e') = "e"
"red green blue".takeRightWhile (· != 'n') = " blue"
"red green blue".takeRightWhile (fun _ => true) = "red green blue"
Equations
- s.takeRightWhile p = (s.toSubstring.takeRightWhile p).toString
Instances For
Creates a new string by removing the longest suffix from s
in which p
returns true
for all
characters.
Examples:
"red green blue".dropRightWhile (·.isLetter) = "red green "
"red green blue".dropRightWhile (· == 'e') = "red green blu"
"red green blue".dropRightWhile (· != 'n') = "red green"
"red green blue".dropRightWhile (fun _ => true) = ""
Equations
- s.dropRightWhile p = (s.toSubstring.dropRightWhile p).toString
Instances For
Checks whether the first string (s
) begins with the second (pre
).
String.isPrefix
is a version that takes the potential prefix before the string.
Examples:
"red green blue".startsWith "red" = true
"red green blue".startsWith "green" = false
"red green blue".startsWith "" = true
"red".startsWith "red" = true
Equations
- s.startsWith pre = (s.toSubstring.take pre.length == pre.toSubstring)
Instances For
Checks whether the first string (s
) ends with the second (post
).
Examples:
"red green blue".endsWith "blue" = true
"red green blue".endsWith "green" = false
"red green blue".endsWith "" = true
"red".endsWith "red" = true
Equations
- s.endsWith post = (s.toSubstring.takeRight post.length == post.toSubstring)
Instances For
Removes trailing whitespace from a string.
“Whitespace” is defined as characters for which Char.isWhitespace
returns true
.
Examples:
"abc".trimRight = "abc"
" abc".trimRight = " abc"
"abc \t ".trimRight = "abc"
" abc ".trimRight = " abc"
"abc\ndef\n".trimRight = "abc\ndef"
Equations
Instances For
Removes leading whitespace from a string.
“Whitespace” is defined as characters for which Char.isWhitespace
returns true
.
Examples:
"abc".trimLeft = "abc"
" abc".trimLeft = " abc"
"abc \t ".trimLeft = "abc \t "
" abc ".trimLeft = "abc "
"abc\ndef\n".trimLeft = "abc\ndef\n"
Equations
Instances For
Removes leading and trailing whitespace from a string.
“Whitespace” is defined as characters for which Char.isWhitespace
returns true
.
Examples:
"abc".trim = "abc"
" abc".trim = "abc"
"abc \t ".trim = "abc"
" abc ".trim = "abc"
"abc\ndef\n".trim = "abc\ndef"
Equations
- s.trim = s.toSubstring.trim.toString
Instances For
Repeatedly increments a position in a string, as if by String.next
, while the predicate p
returns true
for the character at the position. Stops incrementing at the end of the string or
when p
returns false
for the current character.
Examples:
let s := " a "; s.get (s.nextWhile Char.isWhitespace 0) = 'a'
let s := "a "; s.get (s.nextWhile Char.isWhitespace 0) = 'a'
let s := "ba "; s.get (s.nextWhile Char.isWhitespace 0) = 'b'
Equations
- s.nextWhile p i = Substring.takeWhileAux s s.endPos p i
Instances For
Repeatedly increments a position in a string, as if by String.next
, while the predicate p
returns false
for the character at the position. Stops incrementing at the end of the string or
when p
returns true
for the current character.
Examples:
let s := " a "; s.get (s.nextUntil Char.isWhitespace 0) = ' '
let s := " a "; s.get (s.nextUntil Char.isLetter 0) = 'a'
let s := "a "; s.get (s.nextUntil Char.isWhitespace 0) = ' '
Instances For
Replaces each character in s
with the result of applying Char.toUpper
to it.
Char.toUpper
has no effect on characters outside of the range 'a'
–'z'
.
Examples:
Equations
Instances For
Replaces each character in s
with the result of applying Char.toLower
to it.
Char.toLower
has no effect on characters outside of the range 'A'
–'Z'
.
Examples:
Equations
Instances For
Replaces the first character in s
with the result of applying Char.toUpper
to it. Returns the
empty string if the string is empty.
Char.toUpper
has no effect on characters outside of the range 'a'
–'z'
.
Examples:
"orange".capitalize = "Orange"
"ORANGE".capitalize = "ORANGE"
"".capitalize = ""
Equations
- s.capitalize = s.set 0 (s.get 0).toUpper
Instances For
Replaces the first character in s
with the result of applying Char.toLower
to it. Returns the
empty string if the string is empty.
Char.toLower
has no effect on characters outside of the range 'A'
–'Z'
.
Examples:
"Orange".decapitalize = "orange"
"ORANGE".decapitalize = "oRANGE"
"".decapitalize = ""
Equations
- s.decapitalize = s.set 0 (s.get 0).toLower
Instances For
If pre
is a prefix of s
, returns the remainder. Returns none
otherwise.
The string pre
is a prefix of s
if there exists a t : String
such that s = pre ++ t
. If so,
the result is some t
.
Use String.stripPrefix
to return the string unchanged when pre
is not a prefix.
Examples:
"red green blue".dropPrefix? "red " = some "green blue"
"red green blue".dropPrefix? "reed " = none
"red green blue".dropPrefix? "" = some "red green blue"
Equations
- s.dropPrefix? pre = s.toSubstring.dropPrefix? pre.toSubstring
Instances For
If suff
is a suffix of s
, returns the remainder. Returns none
otherwise.
The string suff
is a suffix of s
if there exists a t : String
such that s = t ++ suff
. If so,
the result is some t
.
Use String.stripSuffix
to return the string unchanged when suff
is not a suffix.
Examples:
"red green blue".dropSuffix? " blue" = some "red green"
"red green blue".dropSuffix? " blu " = none
"red green blue".dropSuffix? "" = some "red green blue"
Equations
- s.dropSuffix? suff = s.toSubstring.dropSuffix? suff.toSubstring
Instances For
If pre
is a prefix of s
, returns the remainder. Returns s
unmodified otherwise.
The string pre
is a prefix of s
if there exists a t : String
such that s = pre ++ t
. If so,
the result is t
. Otherwise, it is s
.
Use String.dropPrefix?
to return none
when pre
is not a prefix.
Examples:
"red green blue".stripPrefix "red " = "green blue"
"red green blue".stripPrefix "reed " = "red green blue"
"red green blue".stripPrefix "" = "red green blue"
Equations
- s.stripPrefix pre = (Option.map Substring.toString (s.dropPrefix? pre)).getD s
Instances For
If suff
is a suffix of s
, returns the remainder. Returns s
unmodified otherwise.
The string suff
is a suffix of s
if there exists a t : String
such that s = t ++ suff
. If so,
the result is t
. Otherwise, it is s
.
Use String.dropSuffix?
to return none
when suff
is not a suffix.
Examples:
"red green blue".stripSuffix " blue" = "red green"
"red green blue".stripSuffix " blu " = "red green blue"
"red green blue".stripSuffix "" = "red green blue"
Equations
- s.stripSuffix suff = (Option.map Substring.toString (s.dropSuffix? suff)).getD s
Instances For
Constructs a singleton string that contains only the provided character.
Examples: