Documentation

Init.Data.String.Extra

Interprets a string as the decimal representation of a natural number, returning it. Panics 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 a value. String.toNat? is a safer alternative that returns none instead of panicking when the string is not a natural number.

Examples:

Equations
  • One or more equations did not get rendered due to their size.

Decodes the UTF-8 character sequence that starts at a given index in a byte array, or none if index i is out of bounds or is not the start of a valid UTF-8 character.

Equations
  • One or more equations did not get rendered due to their size.
@[extern lean_string_validate_utf8]

Checks whether an array of bytes is a valid UTF-8 encoding of a string.

Equations
@[extern lean_string_from_utf8_unchecked]

Decodes an array of bytes that encode a string as UTF-8 into the corresponding string. Invalid UTF-8 characters in the byte array result in (default : Char), or 'A', in the string.

Equations
@[irreducible]
Equations
@[inline]

Decodes an array of bytes that encode a string as UTF-8 into the corresponding string, or returns none if the array is not a valid UTF-8 encoding of a string.

Equations
@[inline]

Decodes an array of bytes that encode a string as UTF-8 into the corresponding string, or panics if the array is not a valid UTF-8 encoding of a string.

Equations

Returns the sequence of bytes in a character's UTF-8 encoding.

Equations
  • One or more equations did not get rendered due to their size.
@[extern lean_string_to_utf8]

Encodes a string in UTF-8 as an array of bytes.

Equations
@[extern lean_string_get_byte_fast]
def String.getUtf8Byte (s : String) (n : Nat) (h : n < s.utf8ByteSize) :

Accesses the indicated byte in the UTF-8 encoding of a string.

At runtime, this function is implemented by efficient, constant-time code.

Equations
@[irreducible, specialize #[]]

Moves the iterator forward until the Boolean predicate p returns true for the iterator's current character or until the end of the string is reached. Does nothing if the current character already satisfies p.

Equations
@[irreducible, specialize #[]]
def String.Iterator.foldUntil {α : Type u_1} (it : Iterator) (init : α) (f : αCharOption α) :

Iterates over a string, updating a state at each character using the provided function f, until f returns none. Begins with the state init. Returns the state and character for which f returns none.

Equations

Consistently de-indents the lines in a string, removing the same amount of leading whitespace from each line such that the least-indented line has no leading whitespace.

The number of leading whitespace characters to remove from each line is determined by counting the number of leading space (' ') and tab ('\t') characters on lines after the first line that also contain non-whitespace characters. No distinction is made between tab and space characters; both count equally.

The least number of leading whitespace characters found is then removed from the beginning of each line. The first line's leading whitespace is not counted when determining how far to de-indent the string, but leading whitespace is removed from it.

Examples:

Equations

Replaces each \r\n with \n to normalize line endings, but does not validate that there are no isolated \r characters.

This is an optimized version of String.replace text "\r\n" "\n".

Equations
@[irreducible]
def String.crlfToLf.go (text acc : String) (accStop pos : Pos) :
Equations
  • One or more equations did not get rendered due to their size.