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
- Struct
Char
- Constants
- Function
char
- Function
string
- Function
try_string
- Function
all_characters_printable
- Function
push_char
- Function
pop_char
- Function
length
- Function
as_bytes
- Function
into_bytes
- Function
byte
- Function
is_valid_char
- Function
is_printable_char
use 0x1::option;
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.
#[data_struct]
struct String has copy, drop, store
An ASCII character.
struct Char has copy, drop, store
An invalid ASCII character was encountered when creating an ASCII string.
const EINVALID_ASCII_CHARACTER: u64 = 65536;
Convert a byte
into a Char
that is checked to make sure it is valid ASCII.
public fun char(byte: u8): ascii::Char
Convert a vector of bytes bytes
into an String
. Aborts if
bytes
contains non-ASCII characters.
public fun string(bytes: vector<u8>): ascii::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
.
public fun try_string(bytes: vector<u8>): option::Option<ascii::String>
Returns true
if all characters in string
are printable characters
Returns false
otherwise. Not all String
s are printable strings.
public fun all_characters_printable(string: &ascii::String): bool
public fun push_char(string: &mut ascii::String, char: ascii::Char)
public fun pop_char(string: &mut ascii::String): ascii::Char
public fun length(string: &ascii::String): u64
Get the inner bytes of the string
as a reference
public fun as_bytes(string: &ascii::String): &vector<u8>
Unpack the string
to get its backing bytes
public fun into_bytes(string: ascii::String): vector<u8>
Unpack the char
into its underlying byte.
public fun byte(char: ascii::Char): u8
Returns true
if b
is a valid ASCII character. Returns false
otherwise.
public fun is_valid_char(b: u8): bool
Returns true
if byte
is an printable ASCII character. Returns false
otherwise.
public fun is_printable_char(byte: u8): bool