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
- Struct Char
- Constants
- Function char
- Function string
- Function try_string
- Function all_characters_printable
- Function push_char
- Function pop_char
- Function length
- Function append
- Function insert
- Function substring
- Function as_bytes
- Function into_bytes
- Function byte
- Function is_valid_char
- Function is_printable_char
- Function is_empty
- Function to_uppercase
- Function to_lowercase
- Function index_of
- Function char_to_uppercase
- Function char_to_lowercase
use std::option;
use std::vector;
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
Struct Char
An ASCII character.
public struct Char has copy, drop, store
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
Click to open
Implementation
public fun char(byte: u8): Char {
assert!(is_valid_char(byte), EInvalidASCIICharacter);
Char { byte }
}
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
Click to open
Implementation
public fun string(bytes: vector<u8>): String {
let x = try_string(bytes);
assert!(x.is_some(), EInvalidASCIICharacter);
x.destroy_some()
}
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
.
public fun try_string(bytes: vector<u8>): std::option::Option<std::ascii::String>
Click to open
Implementation
public fun try_string(bytes: vector<u8>): Option<String> {
let is_valid = bytes.all!(|byte| is_valid_char(*byte));
if (is_valid) option::some(String { bytes }) else option::none()
}
Function all_characters_printable
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: &std::ascii::String): bool
Click to open
Implementation
public fun all_characters_printable(string: &String): bool {
string.bytes.all!(|byte| is_printable_char(*byte))
}
Function push_char
Push a
Char
to the end of the string
.
public fun push_char(string: &mut std::ascii::String, char: std::ascii::Char)
Click to open
Function pop_char
Pop a
Char
from the end of the string
.
public fun pop_char(string: &mut std::ascii::String): std::ascii::Char
Click to open
Function length
Returns the length of the
string
in bytes.
public fun length(string: &std::ascii::String): u64
Function append
Append the other
string to the end of
string
.
public fun append(string: &mut std::ascii::String, other: std::ascii::String)
Click to open
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)
Click to open
Implementation
public fun insert(s: &mut String, at: u64, o: String) {
assert!(at <= s.length(), EInvalidIndex);
o.into_bytes().destroy!(|e| s.bytes.insert(e, at));
}
Function substring
Copy the slice of the
string
from i
to j
into a new String
.
public fun substring(string: &std::ascii::String, i: u64, j: u64): std::ascii::String
Click to open
Function as_bytes
Get the inner bytes of the
string
as a reference
public fun as_bytes(string: &std::ascii::String): &vector<u8>
Function into_bytes
Unpack the
string
to get its backing bytes
public fun into_bytes(string: std::ascii::String): vector<u8>
Click to open
Function byte
Unpack the
char
into its underlying bytes.
public fun byte(char: std::ascii::Char): u8