@@ -63,39 +63,35 @@ impl TextInputComponent {
63
63
64
64
/// Move the cursor left one char.
65
65
fn decr_cursor ( & mut self ) {
66
- let mut new_pos: usize = 0 ;
67
- for ( bytes, _) in self . msg . char_indices ( ) {
68
- if bytes >= self . cursor_position {
69
- break ;
70
- }
71
- new_pos = bytes;
66
+ let mut index = self . cursor_position . saturating_sub ( 1 ) ;
67
+ while index > 0 && !self . msg . is_char_boundary ( index) {
68
+ index -= 1 ;
72
69
}
73
- self . cursor_position = new_pos ;
70
+ self . cursor_position = index ;
74
71
}
75
72
76
73
/// Get the position of the next char, or, if the cursor points
77
74
/// to the last char, the `msg.len()`.
78
75
/// Returns None when the cursor is already at `msg.len()`.
79
76
fn next_char_position ( & self ) -> Option < usize > {
80
- let mut char_indices =
81
- self . msg [ self . cursor_position ..] . char_indices ( ) ;
82
- if char_indices. next ( ) . is_some ( ) {
83
- if let Some ( ( bytes, _) ) = char_indices. next ( ) {
84
- Some ( self . cursor_position + bytes)
85
- } else {
86
- Some ( self . msg . len ( ) )
87
- }
88
- } else {
89
- None
77
+ if self . cursor_position >= self . msg . len ( ) {
78
+ return None ;
90
79
}
80
+ let mut index = self . cursor_position . saturating_add ( 1 ) ;
81
+ while index < self . msg . len ( )
82
+ && !self . msg . is_char_boundary ( index)
83
+ {
84
+ index += 1 ;
85
+ }
86
+ Some ( index)
91
87
}
92
88
93
- ///
89
+ /// Set the `msg`.
94
90
pub fn set_text ( & mut self , msg : String ) {
95
91
self . msg = msg;
96
92
}
97
93
98
- ///
94
+ /// Set the `title`.
99
95
pub fn set_title ( & mut self , t : String ) {
100
96
self . title = t;
101
97
}
0 commit comments