Struct endbasic_std::console::LineBuffer
source · pub struct LineBuffer { /* private fields */ }
Expand description
Abstraction over a string to handle manipulation operations at char boundaries.
This exists because Rust strings are indexed by bytes and manipulating string bytes is complicated.
TODO(zenria): The current implementation of the buffer is using String::chars
. Should be
converted to using graphemes instead.
Implementations§
source§impl LineBuffer
impl LineBuffer
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty LineBuffer
with an allocated capacity
.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the logical LineBuffer
length (in UTF-8 code point count and not in bytes).
sourcepub fn end(&self, start_pos: usize) -> String
pub fn end(&self, start_pos: usize) -> String
Returns the end of the string starting at start_pos
, or an empty string if start_pos
is
after the string’s end.
sourcepub fn start(&self, end_pos: usize) -> String
pub fn start(&self, end_pos: usize) -> String
Returns the characters from the start of the string to end_pos
, excluding end_pos
.
Calling this with end_pos
set to 0 will return an empty string, and a 1-char string if
end_pos
is 1 (if the string contains at least 1 character).
sourcepub fn range(&self, start_pos: usize, end_pos: usize) -> String
pub fn range(&self, start_pos: usize, end_pos: usize) -> String
Extracts a range of characters from this buffer.
sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Gets a view on the underlying bytes held by this buffer.
Warning: direct bytes manipulation may lead to undefined behavior.
sourcepub fn remove(&mut self, pos: usize)
pub fn remove(&mut self, pos: usize)
Removes a character from this buffer.
If the given position if greater than the length of the buffer, this function does nothing.
sourcepub fn insert(&mut self, pos: usize, ch: char)
pub fn insert(&mut self, pos: usize, ch: char)
Inserts a char at the given position.
If the position is greater than the buffer length, the character will be appended at the end ot it.
sourcepub fn into_inner(self) -> String
pub fn into_inner(self) -> String
Returns the underlying string.
sourcepub fn insert_str(&mut self, pos: usize, s: &str)
pub fn insert_str(&mut self, pos: usize, s: &str)
Inserts the given string s
into the buffer at pos
.
If pos
is greater than the length of the buffer, s
will be appended at the end of it.
sourcepub fn push_str(&mut self, s: &LineBuffer)
pub fn push_str(&mut self, s: &LineBuffer)
Appends the given string s
to the buffer.
sourcepub fn split_off(&mut self, at: usize) -> LineBuffer
pub fn split_off(&mut self, at: usize) -> LineBuffer
Splits the buffer in two parts at the position at
.
Returns the remaining part of the buffer (same behavior as String::split_off
).