Skip to main content

Module std::ascii

The ASCII module defines basic string and char newtypes in Move that verify that characters are valid ASCII, and that strings consist of only valid ASCII characters.

Struct String

The String struct holds a vector of bytes that all represent valid ASCII characters. Note that these ASCII characters may not all be printable. To determine if a String contains only "printable" characters you should use the all_characters_printable predicate defined in this module.

public struct String has copy, drop, store
Click to open
Fields
bytes: vector<u8>

Struct Char

An ASCII character.

public struct Char has copy, drop, store
Click to open
Fields
byte: u8

Constants

An invalid ASCII character was encountered when creating an ASCII string.

const EInvalidASCIICharacter: u64 = 65536;

An invalid index was encountered when creating a substring.

const EInvalidIndex: u64 = 65537;

Function char

Convert a byte into a Char that is checked to make sure it is valid ASCII.

public fun char(byte: u8): std::ascii::Char

Function string

Convert a vector of bytes bytes into an String. Aborts if bytes contains non-ASCII characters.

public fun string(bytes: vector<u8>): std::ascii::String

Function try_string

Convert a vector of bytes bytes into an String. Returns Some(<ascii_string>) if the bytes contains all valid ASCII characters. Otherwise returns None.

Function all_characters_printable

Returns true if all characters in string are printable characters.
Returns false otherwise. Not all Strings are printable strings.

Function push_char

Push a Char to the end of the string.

Function pop_char

Pop a Char from the end of the string.

Function length

Returns the length of the string in bytes.

Function append

Append the other string to the end of string.

Function insert

Insert the other string at the at index of string.

public fun insert(s: &mut std::ascii::String, at: u64, o: std::ascii::String)

Function substring

Copy the slice of the string from i to j into a new String.

Function as_bytes

Get the inner bytes of the string as a reference

Function into_bytes

Unpack the string to get its backing bytes

Function byte

Unpack the char into its underlying bytes.

public fun byte(char: std::ascii::Char): u8

Function is_valid_char

Returns true if b is a valid ASCII character.
Returns false otherwise.

public fun is_valid_char(b: u8): bool

Function is_printable_char

Returns true if byte is a printable ASCII character.
Returns false otherwise.

public fun is_printable_char(byte: u8): bool

Function is_empty

Returns true if string is empty.

Function to_uppercase

Convert a string to its uppercase equivalent.

Function to_lowercase

Convert a string to its lowercase equivalent.

Function index_of

Computes the index of the first occurrence of the substr in the string.
Returns the length of the string if the substr is not found.
Returns 0 if the substr is empty.

Function char_to_uppercase

Convert a char to its lowercase equivalent.

Function char_to_lowercase

Convert a char to its lowercase equivalent.