Skip to content

Commit 398be76

Browse files
committed
Count utf8 chars, not every byte for line
1 parent 93829e5 commit 398be76

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/mp-readline/readline.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ STATIC char *str_dup_maybe(const char *str) {
5858
return s2;
5959
}
6060

61-
STATIC int count_cont_bytes(char *start, char *end) {
61+
STATIC size_t count_cont_bytes(char *start, char *end) {
6262
int count = 0;
6363
for (char *pos = start; pos < end; pos++) {
64-
count += UTF8_IS_CONT(*pos);
64+
if(UTF8_IS_CONT(*pos)) {
65+
count++;
66+
}
6567
}
6668
return count;
6769
}
@@ -108,7 +110,7 @@ typedef struct _readline_t {
108110
STATIC readline_t rl;
109111

110112
int readline_process_char(int c) {
111-
size_t last_line_len = rl.line->len;
113+
size_t last_line_len = utf8_charlen((byte *)rl.line->buf, rl.line->len);
112114
int cont_chars = 0;
113115
int redraw_step_back = 0;
114116
bool redraw_from_cursor = false;
@@ -380,7 +382,7 @@ int readline_process_char(int c) {
380382
rl.cursor_pos -= redraw_step_back;
381383
}
382384
if (redraw_from_cursor) {
383-
if (rl.line->len < last_line_len) {
385+
if (utf8_charlen((byte *)rl.line->buf, rl.line->len) < last_line_len) {
384386
// erase old chars
385387
mp_hal_erase_line_from_cursor(last_line_len - rl.cursor_pos);
386388
}

0 commit comments

Comments
 (0)