1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//! UTF-8 aware text editing utilities.
//!
//! Provides cursor movement and text manipulation functions that correctly
//! handle multi-byte UTF-8 characters (CJK, emoji, etc.).
/// Find the byte position of the previous character boundary.
/// Returns 0 if already at the start.
pub fn prev_char_boundary(buffer: &str, cursor: usize) -> usize {
if cursor == 0 {
return 0;
}
let cursor = cursor.min(buffer.len());
// Move back one byte at a time until we find a char boundary
let mut pos = cursor - 1;
while pos > 0 && !buffer.is_char_boundary(pos) {
pos -= 1;
}
pos
}
/// Find the byte position of the next character boundary.
/// Returns buffer.len() if already at the end.
pub fn next_char_boundary(buffer: &str, cursor: usize) -> usize {
if cursor >= buffer.len() {
return buffer.len();
}
// Move forward one byte at a time until we find a char boundary
let mut pos = cursor + 1;
while pos < buffer.len() && !buffer.is_char_boundary(pos) {
pos += 1;
}
pos
}
/// Delete the character before the cursor position, returning the new cursor position.
/// Handles multi-byte UTF-8 characters correctly.
pub fn delete_char_before(buffer: &mut String, cursor: usize) -> usize {
if cursor == 0 {
return 0;
}
let prev = prev_char_boundary(buffer, cursor);
buffer.replace_range(prev..cursor, "");
prev
}
/// Delete word before cursor, returning the new cursor position.
/// Handles multi-byte UTF-8 characters correctly.
pub fn delete_word_before(buffer: &mut String, cursor: usize) -> usize {
if cursor == 0 {
return 0;
}
let mut pos = cursor;
// Skip whitespace backwards
while pos > 0 {
let prev = prev_char_boundary(buffer, pos);
if let Some(ch) = buffer[prev..pos].chars().next()
&& !ch.is_whitespace()
{
break;
}
pos = prev;
}
// Skip non-whitespace backwards (the word itself)
while pos > 0 {
let prev = prev_char_boundary(buffer, pos);
if let Some(ch) = buffer[prev..pos].chars().next()
&& ch.is_whitespace()
{
break;
}
pos = prev;
}
// Delete from pos to cursor
buffer.replace_range(pos..cursor, "");
pos
}
#[cfg(test)]
mod tests {
use super::*;
// -- prev_char_boundary tests --
#[test]
fn should_return_zero_when_at_start() {
// given
let s = "hello";
// when
let result = prev_char_boundary(s, 0);
// then
assert_eq!(result, 0);
}
#[test]
fn should_find_prev_boundary_for_ascii() {
// given
let s = "hello";
// when/then
assert_eq!(prev_char_boundary(s, 5), 4);
assert_eq!(prev_char_boundary(s, 3), 2);
assert_eq!(prev_char_boundary(s, 1), 0);
}
#[test]
fn should_find_prev_boundary_for_multibyte_char() {
// given - '좋' is 3 bytes, '아' is 3 bytes
let s = "좋아";
assert_eq!(s.len(), 6);
// when/then
assert_eq!(prev_char_boundary(s, 6), 3); // End -> start of '아'
assert_eq!(prev_char_boundary(s, 3), 0); // Start of '아' -> start of '좋'
}
#[test]
fn should_find_prev_boundary_for_emoji() {
// given - '🦀' is 4 bytes
let s = "🦀";
assert_eq!(s.len(), 4);
// when/then
assert_eq!(prev_char_boundary(s, 4), 0);
}
#[test]
fn should_find_prev_boundary_for_mixed_content() {
// given
let s = "a좋b"; // 1 + 3 + 1 = 5 bytes
// when/then
assert_eq!(prev_char_boundary(s, 5), 4); // After 'b' -> start of 'b'
assert_eq!(prev_char_boundary(s, 4), 1); // After '좋' -> start of '좋'
assert_eq!(prev_char_boundary(s, 1), 0); // After 'a' -> start
}
// -- next_char_boundary tests --
#[test]
fn should_return_len_when_at_end() {
// given
let s = "hello";
// when
let result = next_char_boundary(s, 5);
// then
assert_eq!(result, 5);
}
#[test]
fn should_find_next_boundary_for_ascii() {
// given
let s = "hello";
// when/then
assert_eq!(next_char_boundary(s, 0), 1);
assert_eq!(next_char_boundary(s, 2), 3);
assert_eq!(next_char_boundary(s, 4), 5);
}
#[test]
fn should_find_next_boundary_for_multibyte_char() {
// given
let s = "좋아";
// when/then
assert_eq!(next_char_boundary(s, 0), 3); // Start -> after '좋'
assert_eq!(next_char_boundary(s, 3), 6); // After '좋' -> after '아'
}
#[test]
fn should_find_next_boundary_for_emoji() {
// given
let s = "🦀";
// when/then
assert_eq!(next_char_boundary(s, 0), 4);
}
// -- delete_char_before tests --
#[test]
fn should_delete_ascii_char() {
// given
let mut s = String::from("hello");
// when
let cursor = delete_char_before(&mut s, 5);
// then
assert_eq!(s, "hell");
assert_eq!(cursor, 4);
}
#[test]
fn should_delete_multibyte_char() {
// given
let mut s = String::from("좋아");
// when
let cursor = delete_char_before(&mut s, 6);
// then
assert_eq!(s, "좋");
assert_eq!(cursor, 3);
}
#[test]
fn should_delete_multibyte_char_from_middle() {
// given
let mut s = String::from("좋아요");
// when
let cursor = delete_char_before(&mut s, 6); // After '좋아'
// then
assert_eq!(s, "좋요");
assert_eq!(cursor, 3);
}
#[test]
fn should_not_delete_when_at_start() {
// given
let mut s = String::from("hello");
// when
let cursor = delete_char_before(&mut s, 0);
// then
assert_eq!(s, "hello");
assert_eq!(cursor, 0);
}
#[test]
fn should_delete_emoji() {
// given
let mut s = String::from("hi🦀");
// when
let cursor = delete_char_before(&mut s, 6); // After emoji
// then
assert_eq!(s, "hi");
assert_eq!(cursor, 2);
}
// -- delete_word_before tests --
#[test]
fn should_delete_ascii_word() {
// given
let mut s = String::from("hello world");
// when
let cursor = delete_word_before(&mut s, 11);
// then
assert_eq!(s, "hello ");
assert_eq!(cursor, 6);
}
#[test]
fn should_delete_multibyte_word() {
// given
let mut s = String::from("안녕 아가브라"); // 6 + 1 + 12 = 19 bytes
// when
let cursor = delete_word_before(&mut s, 19);
// then
assert_eq!(s, "안녕 ");
assert_eq!(cursor, 7);
}
#[test]
fn should_skip_trailing_whitespace_when_deleting_word() {
// given
let mut s = String::from("hello ");
// when
let cursor = delete_word_before(&mut s, 8);
// then
assert_eq!(s, "");
assert_eq!(cursor, 0);
}
#[test]
fn should_not_delete_word_when_at_start() {
// given
let mut s = String::from("hello");
// when
let cursor = delete_word_before(&mut s, 0);
// then
assert_eq!(s, "hello");
assert_eq!(cursor, 0);
}
// -- Integration tests --
#[test]
fn should_navigate_multibyte_string_correctly() {
// given
let s = "좋아요"; // 9 bytes, 3 chars
// when - navigate right through all chars
let mut cursor = 0;
cursor = next_char_boundary(s, cursor);
assert_eq!(cursor, 3);
cursor = next_char_boundary(s, cursor);
assert_eq!(cursor, 6);
cursor = next_char_boundary(s, cursor);
assert_eq!(cursor, 9);
// when - navigate left through all chars
cursor = prev_char_boundary(s, cursor);
assert_eq!(cursor, 6);
cursor = prev_char_boundary(s, cursor);
assert_eq!(cursor, 3);
cursor = prev_char_boundary(s, cursor);
assert_eq!(cursor, 0);
}
#[test]
fn should_handle_insert_delete_roundtrip() {
// given
let mut s = String::new();
let mut cursor = 0;
// when - insert multibyte chars
for c in "좋아".chars() {
s.insert(cursor, c);
cursor += c.len_utf8();
}
// then
assert_eq!(s, "좋아");
assert_eq!(cursor, 6);
// when - delete both chars
cursor = delete_char_before(&mut s, cursor);
assert_eq!(s, "좋");
cursor = delete_char_before(&mut s, cursor);
assert_eq!(s, "");
assert_eq!(cursor, 0);
}
}