pub struct StrObj {
pub header: Header,
pub len: usize,
pub hash: AtomicU64,
pub chars: AtomicU64,
/* private fields */
}Expand description
A string: immutable UTF-8 bytes with a cached hash and character count.
Strings of at least StrObj::INDEX_MIN bytes get their character count when they are
allocated. When such a string is not ASCII, its allocation also reserves a sparse index after
the bytes: one u32 byte offset for every StrObj::STRIDEth character, filled in the first
time a character position is looked up. That makes s[i] and slicing O(1) for ASCII strings
(character count equals byte length) and O(STRIDE) for the rest.
Fields§
§header: Header§len: usize§hash: AtomicU64Cached hash; 0 means not yet computed.
chars: AtomicU64Cached char count plus one (0 means not yet computed), and StrObj::INDEX_READY once
the char index has been filled in.
Implementations§
Source§impl StrObj
impl StrObj
pub const BASE: usize
Sourcepub const INDEX_MIN: usize = 256
pub const INDEX_MIN: usize = 256
Strings at least this long have an eagerly computed char count, and an index if not ASCII.
Sourcepub fn alloc_size(&self) -> usize
pub fn alloc_size(&self) -> usize
Total bytes the object occupies (header, bytes and index padding included).
pub fn as_str(&self) -> &str
pub fn char_count(&self) -> usize
Sourcepub fn is_ascii(&self) -> bool
pub fn is_ascii(&self) -> bool
Whether every character is a single byte. O(1) once the char count is known.
Sourcepub fn byte_offset(&self, i: usize) -> usize
pub fn byte_offset(&self, i: usize) -> usize
The byte offset of character i (i may equal the char count, giving the length).
O(1) for ASCII strings, O(StrObj::STRIDE) for indexed ones, O(n) for short ones.
Sourcepub fn char_offset(&self, b: usize) -> usize
pub fn char_offset(&self, b: usize) -> usize
The character position of byte offset b, which must be a char boundary.
Sourcepub fn char_slice(&self, start: usize, end: usize) -> &str
pub fn char_slice(&self, start: usize, end: usize) -> &str
The characters [start, end) (clamped to the string).