Skip to content

Commit

Permalink
refactor: convert single-use trait CharIndexed into free functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Dec 27, 2024
1 parent 53d89a0 commit 77797bd
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions src/debugger/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl Terminal {
// character
_ => {
self.update_next();
self.buffer.insert_char_index(self.visible_cursor, ch);
insert_char_index(&mut self.buffer, self.visible_cursor, ch);
self.visible_cursor += 1;
}
},
Expand All @@ -329,13 +329,13 @@ impl Terminal {
&& self.visible_cursor <= self.get_current().chars().count()
{
self.visible_cursor -= 1;
self.buffer.remove_char_index(self.visible_cursor);
remove_char_index(&mut self.buffer, self.visible_cursor);
}
}
Key::Del => {
self.update_next();
if self.visible_cursor < self.get_current().chars().count() {
self.buffer.remove_char_index(self.visible_cursor);
remove_char_index(&mut self.buffer, self.visible_cursor);
}
}

Expand Down Expand Up @@ -432,38 +432,29 @@ impl SourceRead for Terminal {
}
}

/// Extension trait for character-indexed string operations.
trait CharIndexed {
/// Insert a character at a character index.
fn insert_char_index(&mut self, char_index: usize, ch: char);
/// Remove a character at a character index.
fn remove_char_index(&mut self, char_index: usize) -> char;

/// Returns the byte index from a character index, and the total character count.
fn count_chars_bytes(string: &str, char_index: usize) -> (usize, usize) {
let mut byte_index = string.len();
let mut char_count = 0;
for (i, (j, _)) in string.char_indices().enumerate() {
if i == char_index {
byte_index = j;
}
char_count += 1;
}
(byte_index, char_count)
}
/// Insert a character at a character index.
fn insert_char_index(string: &mut String, char_index: usize, ch: char) {
let (byte_index, char_count) = count_chars_bytes(string, char_index);
assert!(char_index <= char_count, "out-of-bounds char index");
string.insert(byte_index, ch)
}

impl CharIndexed for String {
fn insert_char_index(&mut self, char_index: usize, ch: char) {
let (byte_index, char_count) = Self::count_chars_bytes(self, char_index);
assert!(char_index <= char_count, "out-of-bounds char index");
self.insert(byte_index, ch)
}
fn remove_char_index(&mut self, char_index: usize) -> char {
let (byte_index, char_count) = Self::count_chars_bytes(self, char_index);
assert!(char_index < char_count, "out-of-bounds char index");
self.remove(byte_index)
/// Remove a character at a character index.
fn remove_char_index(string: &mut String, char_index: usize) -> char {
let (byte_index, char_count) = count_chars_bytes(string, char_index);
assert!(char_index < char_count, "out-of-bounds char index");
string.remove(byte_index)
}
/// Returns the byte index from a character index, and the total character count.
fn count_chars_bytes(string: &str, char_index: usize) -> (usize, usize) {
let mut byte_index = string.len();
let mut char_count = 0;
for (i, (j, _)) in string.char_indices().enumerate() {
if i == char_index {
byte_index = j;
}
char_count += 1;
}
(byte_index, char_count)
}

impl TerminalHistory {
Expand Down

0 comments on commit 77797bd

Please sign in to comment.