Skip to content

Commit

Permalink
fix infinite loop in next_word on multibyte chars
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigodd committed Sep 2, 2022
1 parent 9fa9bdc commit 803f4d1
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/components/textinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl TextInputComponent {
let mut index = self.cursor_position.saturating_add(1);
while index < self.msg.len() {
if !self.msg.is_char_boundary(index) {
index += 1;
continue;
}

Expand Down Expand Up @@ -716,6 +717,43 @@ mod tests {
assert_eq!(comp.previous_word_position(), None);
}

#[test]
fn test_next_word_multibyte() {
let mut comp = TextInputComponent::new(
SharedTheme::default(),
SharedKeyConfig::default(),
"",
"",
false,
);

// "01245 89A EFG"
let text = dbg!("a à \u{2764}ab\u{1F92F} a");

comp.set_text(String::from(text));

comp.cursor_position = 0;
assert_eq!(comp.next_word_position(), Some(2));
comp.cursor_position = 2;
assert_eq!(comp.next_word_position(), Some(8));
comp.cursor_position = 8;
assert_eq!(comp.next_word_position(), Some(15));
comp.cursor_position = 15;
assert_eq!(comp.next_word_position(), Some(16));
comp.cursor_position = 16;
assert_eq!(comp.next_word_position(), None);

assert_eq!(comp.previous_word_position(), Some(15));
comp.cursor_position = 15;
assert_eq!(comp.previous_word_position(), Some(8));
comp.cursor_position = 8;
assert_eq!(comp.previous_word_position(), Some(2));
comp.cursor_position = 2;
assert_eq!(comp.previous_word_position(), Some(0));
comp.cursor_position = 0;
assert_eq!(comp.previous_word_position(), None);
}

fn get_text<'a>(t: &'a Span) -> Option<&'a str> {
Some(&t.content)
}
Expand Down

0 comments on commit 803f4d1

Please sign in to comment.