std::stringThe string module defines the String type which represents UTF8 encoded
strings.
Stringutf8from_asciito_asciitry_utf8as_bytesinto_bytesis_emptylengthappendappend_utf8insertsubstringindex_ofinternal_check_utf8internal_is_char_boundaryinternal_sub_stringinternal_index_ofbytessub_stringuse std::ascii;
use std::option;
use std::vector;
StringA String holds a sequence of bytes which is guaranteed to be in utf8
format.
public struct String has copy, drop, store
bytes: vector<u8>
An invalid UTF8 encoding.
const EInvalidUTF8: u64 = 1;
Index out of range.
const EInvalidIndex: u64 = 2;
utf8Creates 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
public fun utf8(bytes: vector<u8>): String {
assert!(internal_check_utf8(&bytes), EInvalidUTF8);
String { bytes }
}
from_asciiConvert an ASCII string to a UTF8 string
public fun from_ascii(s: std::ascii::String): std::string::String
public fun from_ascii(s: ascii::String): String {
String { bytes: s.into_bytes() }
}
to_asciiConvert 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
public fun to_ascii(s: String): ascii::String {
let String { bytes } = s;
bytes.to_ascii_string()
}
try_utf8Tries to create a new string from a sequence of bytes.
public fun try_utf8(bytes: vector<u8>): std::option::Option<std::string::String>
public fun try_utf8(bytes: vector<u8>): Option<String> {
if (internal_check_utf8(&bytes)) option::some(String { bytes }) else option::none()
}
as_bytesReturns a reference to the underlying byte vector.
public fun as_bytes(s: &std::string::String): &vector<u8>
into_bytesUnpack the string to get its underlying bytes.
public fun into_bytes(s: std::string::String): vector<u8>
public fun into_bytes(s: String): vector<u8> {
let String { bytes } = s;
bytes
}
is_emptyChecks whether this string is empty.
public fun is_empty(s: &std::string::String): bool
lengthReturns the length of this string, in bytes.
public fun length(s: &std::string::String): u64
appendAppends a string.
public fun append(s: &mut std::string::String, r: std::string::String)
append_utf8Appends bytes which must be in valid utf8 format.
public fun append_utf8(s: &mut std::string::String, bytes: vector<u8>)
insertInsert the other string at the byte index in given string. The index must be at a valid utf8 char boundary.
public fun insert(s: &mut std::string::String, at: u64, o: std::string::String)
public fun insert(s: &mut String, at: u64, o: String) {
let bytes = &s.bytes;
assert!(at <= bytes.length() && internal_is_char_boundary(bytes, at), EInvalidIndex);
let l = s.length();
let mut front = s.substring(0, at);
let end = s.substring(at, l);
front.append(o);
front.append(end);
*s = front;
}
substringReturns a sub-string using the given byte indices, where i is the first
byte position and j is the start of the first byte not included (or the
length of the string). The indices must be at valid utf8 char boundaries,
guaranteeing that the result is valid utf8.
public fun substring(s: &std::string::String, i: u64, j: u64): std::string::String
public fun substring(s: &String, i: u64, j: u64): String {
let bytes = &s.bytes;
let l = bytes.length();
assert!(
j <= l &&
i <= j &&
internal_is_char_boundary(bytes, i) &&
internal_is_char_boundary(bytes, j),
EInvalidIndex,
);
String { bytes: internal_sub_string(bytes, i, j) }
}
index_ofComputes the index of the first occurrence of a string. Returns s.length()
if no occurrence found.
public fun index_of(s: &std::string::String, r: &std::string::String): u64
public fun index_of(s: &String, r: &String): u64 {
internal_index_of(&s.bytes, &r.bytes)
}
internal_check_utf8fun internal_check_utf8(v: &vector<u8>): bool
native fun internal_check_utf8(v: &vector<u8>): bool;
internal_is_char_boundaryfun internal_is_char_boundary(v: &vector<u8>, i: u64): bool
native fun internal_is_char_boundary(v: &vector<u8>, i: u64): bool;
internal_sub_stringfun internal_sub_string(v: &vector<u8>, i: u64, j: u64): vector<u8>
native fun internal_sub_string(v: &vector<u8>, i: u64, j: u64): vector<u8>;
internal_index_offun internal_index_of(v: &vector<u8>, r: &vector<u8>): u64
native fun internal_index_of(v: &vector<u8>, r: &vector<u8>): u64;
bytespublic fun bytes(s: &std::string::String): &vector<u8>
sub_stringpublic fun sub_string(s: &std::string::String, i: u64, j: u64): std::string::String
public fun sub_string(s: &String, i: u64, j: u64): String {
s.substring(i, j)
}