Module std::string
The
string module defines the String type which represents UTF8 encoded
strings.
- Struct String
- Constants
- Function utf8
- Function from_ascii
- Function to_ascii
- Function try_utf8
- Function as_bytes
- Function into_bytes
- Function is_empty
- Function length
- Function append
- Function append_utf8
- Function insert
- Function substring
- Function index_of
- Function internal_check_utf8
- Function internal_is_char_boundary
- Function internal_sub_string
- Function internal_index_of
- Function bytes
- Function sub_string
use std::ascii;
use std::option;
use std::vector;
Struct String
A
String holds a sequence of bytes which is guaranteed to be in utf8
format.
public struct String has copy, drop, store
Constants
An invalid UTF8 encoding.
const EInvalidUTF8: u64 = 1;
Index out of range.
const EInvalidIndex: u64 = 2;
Function utf8
Creates a new string from a sequence of bytes. Aborts if the bytes do not represent valid utf8.
public fun utf8(bytes: vector<u8>): std::string::String
Click to open
Implementation
public fun utf8(bytes: vector<u8>): String {
assert!(internal_check_utf8(&bytes), EInvalidUTF8);
String { bytes }
}
Function from_ascii
Convert an ASCII string to a UTF8 string
public fun from_ascii(s: std::ascii::String): std::string::String
Click to open
Implementation
public fun from_ascii(s: ascii::String): String {
String { bytes: s.into_bytes() }
}
Function to_ascii
Convert an UTF8 string to an ASCII string.
Aborts if s is not valid ASCII
public fun to_ascii(s: std::string::String): std::ascii::String
Click to open
Function try_utf8
Tries to create a new string from a sequence of bytes.
public fun try_utf8(bytes: vector<u8>): std::option::Option<std::string::String>
Click to open
Implementation
public fun try_utf8(bytes: vector<u8>): Option<String> {
if (internal_check_utf8(&bytes)) option::some(String { bytes }) else option::none()
}