pub struct TextBuffer { /* private fields */ }Expand description
Re-export of TextBuffer — a rope-based text buffer for efficient editing.
Use TextBuffer::new to create a buffer from a string slice.
A rope-based text buffer for efficient editing operations.
TextBuffer wraps a ropey::Rope to provide fast insertions, deletions,
and character counting on texts of any size — from short snippets to files
with millions of lines.
§Invariants
- The internal rope is always well-formed (never
Noneor in an undefined state) after construction viaTextBuffer::new. - Character counts returned by
TextBuffer::len_charscount Unicode scalar values, so multi-byte characters like emoji are counted as a single character.
§Example
use dscode_core::TextBuffer;
let buffer = TextBuffer::new("Hello, world!");
assert_eq!(buffer.len_chars(), 13);
assert_eq!(buffer.get_text(), "Hello, world!");
assert!(!buffer.is_empty());Implementations§
Source§impl TextBuffer
impl TextBuffer
Sourcepub fn new(content: &str) -> Self
pub fn new(content: &str) -> Self
Create a new text buffer initialised with the given content.
The entire content string is copied into an internal rope data
structure. For an empty buffer, pass "".
§Arguments
content— The initial text content of the buffer. An empty string produces an empty buffer.
§Example
use dscode_core::TextBuffer;
let buffer = TextBuffer::new("some text");Examples found in repository?
7fn main() {
8 // Create a new buffer from a string
9 let buffer = TextBuffer::new("Hello, world!");
10 println!("Text: {}", buffer.get_text());
11 println!("Length: {} chars", buffer.len_chars());
12 println!("Is empty: {}", buffer.is_empty());
13
14 // Create an empty buffer
15 let empty = TextBuffer::new("");
16 println!("Empty buffer length: {}", empty.len_chars());
17 assert!(empty.is_empty());
18}Sourcepub fn get_text(&self) -> String
pub fn get_text(&self) -> String
Get the full text content of the buffer as a String.
This copies the entire rope contents into a new string. For very large buffers, consider whether you truly need the whole text at once.
§Example
use dscode_core::TextBuffer;
let buffer = TextBuffer::new("abc");
assert_eq!(buffer.get_text(), "abc");Examples found in repository?
7fn main() {
8 // Create a new buffer from a string
9 let buffer = TextBuffer::new("Hello, world!");
10 println!("Text: {}", buffer.get_text());
11 println!("Length: {} chars", buffer.len_chars());
12 println!("Is empty: {}", buffer.is_empty());
13
14 // Create an empty buffer
15 let empty = TextBuffer::new("");
16 println!("Empty buffer length: {}", empty.len_chars());
17 assert!(empty.is_empty());
18}Sourcepub fn len_chars(&self) -> usize
pub fn len_chars(&self) -> usize
Get the number of Unicode scalar values in the buffer.
This counts characters, not bytes. For example, "café" returns 4,
and "🦀" returns 1.
§Example
use dscode_core::TextBuffer;
let buffer = TextBuffer::new("café");
assert_eq!(buffer.len_chars(), 4);Examples found in repository?
7fn main() {
8 // Create a new buffer from a string
9 let buffer = TextBuffer::new("Hello, world!");
10 println!("Text: {}", buffer.get_text());
11 println!("Length: {} chars", buffer.len_chars());
12 println!("Is empty: {}", buffer.is_empty());
13
14 // Create an empty buffer
15 let empty = TextBuffer::new("");
16 println!("Empty buffer length: {}", empty.len_chars());
17 assert!(empty.is_empty());
18}Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check whether the buffer contains no characters.
Returns true when TextBuffer::len_chars is zero.
§Example
use dscode_core::TextBuffer;
let buffer = TextBuffer::new("");
assert!(buffer.is_empty());Examples found in repository?
7fn main() {
8 // Create a new buffer from a string
9 let buffer = TextBuffer::new("Hello, world!");
10 println!("Text: {}", buffer.get_text());
11 println!("Length: {} chars", buffer.len_chars());
12 println!("Is empty: {}", buffer.is_empty());
13
14 // Create an empty buffer
15 let empty = TextBuffer::new("");
16 println!("Empty buffer length: {}", empty.len_chars());
17 assert!(empty.is_empty());
18}