Fixed unicode bug where horizontally scrollable text would attempt to iterate beyond the length of the string; this happens whenever unicode values are present in the string
This commit is contained in:
+11
-9
@@ -230,8 +230,12 @@ impl HorizontallyScrollableText {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.text.chars().count()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn scroll_left(&self) {
|
pub fn scroll_left(&self) {
|
||||||
if *self.offset.borrow() < self.text.len() {
|
if *self.offset.borrow() < self.len() {
|
||||||
let new_offset = *self.offset.borrow() + 1;
|
let new_offset = *self.offset.borrow() + 1;
|
||||||
*self.offset.borrow_mut() = new_offset;
|
*self.offset.borrow_mut() = new_offset;
|
||||||
}
|
}
|
||||||
@@ -245,7 +249,7 @@ impl HorizontallyScrollableText {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn scroll_home(&self) {
|
pub fn scroll_home(&self) {
|
||||||
*self.offset.borrow_mut() = self.text.len();
|
*self.offset.borrow_mut() = self.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset_offset(&self) {
|
pub fn reset_offset(&self) {
|
||||||
@@ -253,8 +257,8 @@ impl HorizontallyScrollableText {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn scroll_left_or_reset(&self, width: usize, is_current_selection: bool, can_scroll: bool) {
|
pub fn scroll_left_or_reset(&self, width: usize, is_current_selection: bool, can_scroll: bool) {
|
||||||
if can_scroll && is_current_selection && self.text.len() >= width {
|
if can_scroll && is_current_selection && self.len() >= width {
|
||||||
if *self.offset.borrow() < self.text.len() {
|
if *self.offset.borrow() < self.len() {
|
||||||
self.scroll_left();
|
self.scroll_left();
|
||||||
} else {
|
} else {
|
||||||
self.reset_offset();
|
self.reset_offset();
|
||||||
@@ -265,17 +269,15 @@ impl HorizontallyScrollableText {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop(&mut self) {
|
pub fn pop(&mut self) {
|
||||||
if *self.offset.borrow() < self.text.len() {
|
if *self.offset.borrow() < self.len() {
|
||||||
self
|
self.text.remove(self.len() - *self.offset.borrow() - 1);
|
||||||
.text
|
|
||||||
.remove(self.text.len() - *self.offset.borrow() - 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push(&mut self, character: char) {
|
pub fn push(&mut self, character: char) {
|
||||||
self
|
self
|
||||||
.text
|
.text
|
||||||
.insert(self.text.len() - *self.offset.borrow(), character);
|
.insert(self.len() - *self.offset.borrow(), character);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -338,6 +338,15 @@ mod tests {
|
|||||||
assert_str_eq!(horizontally_scrollable_text.text, test_text);
|
assert_str_eq!(horizontally_scrollable_text.text, test_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_horizontally_scrollable_text_len() {
|
||||||
|
let test_text = "우리 생애 최고의 해 The.Best";
|
||||||
|
let horizontally_scrollable_text = HorizontallyScrollableText::new(test_text.to_owned());
|
||||||
|
|
||||||
|
assert_eq!(horizontally_scrollable_text.len(), 20);
|
||||||
|
assert_str_eq!(horizontally_scrollable_text.text, test_text);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_horizontally_scrollable_text_scroll_text_left() {
|
fn test_horizontally_scrollable_text_scroll_text_left() {
|
||||||
let horizontally_scrollable_text = HorizontallyScrollableText::from("Test string");
|
let horizontally_scrollable_text = HorizontallyScrollableText::from("Test string");
|
||||||
|
|||||||
Reference in New Issue
Block a user