Skip to content

Commit 80c494d

Browse files
committed
Try too make new utf-8 code smaller again
1 parent 7af7e02 commit 80c494d

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

lib/mp-readline/readline.c

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -222,32 +222,26 @@ int readline_process_char(int c) {
222222
redraw_step_forward = compl_len;
223223
}
224224
#endif
225-
} else if (32 <= c && c <= 126) {
225+
} else if (32 <= c) {
226226
// printable character
227-
vstr_ins_char(rl.line, rl.cursor_pos, c);
228-
// set redraw parameters
229-
redraw_from_cursor = true;
230-
redraw_step_forward = 1;
231-
}else if (c >= 128) {
232-
// utf-8 character
233-
if (c >= 0xc0 && c < 0xf8) {
234-
// Lead code point
235-
vstr_ins_char(rl.line, rl.cursor_pos, c);
227+
char lcp = rl.line->buf[rl.cursor_pos];
228+
uint8_t cont_need = 0;
229+
if (!UTF8_IS_CONT(c)) {
230+
// ASCII or Lead code point
236231
rl.utf8_cont_chars = 0;
237-
}else if (UTF8_IS_CONT(c)) {
238-
char lcp = rl.line->buf[rl.cursor_pos];
239-
// Check for valid lead code point
240-
if (lcp >= 0xc0 && lcp < 0xf8) {
241-
rl.utf8_cont_chars += 1;
242-
vstr_ins_char(rl.line, rl.cursor_pos+rl.utf8_cont_chars, c);
243-
// set redraw parameters if we have the entire character
244-
uint8_t need = (0xe5 >> ((lcp >> 3) & 0x6)) & 3; // From unicode.c L195
245-
if (rl.utf8_cont_chars == need) {
246-
redraw_from_cursor = true;
247-
redraw_step_forward = rl.utf8_cont_chars+1;
248-
cont_chars = rl.utf8_cont_chars;
249-
}
250-
}
232+
lcp = c;
233+
}else {
234+
rl.utf8_cont_chars += 1;
235+
}
236+
if (lcp >= 0xc0 && lcp < 0xf8) {
237+
cont_need = (0xe5 >> ((lcp >> 3) & 0x6)) & 3; // From unicode.c L195
238+
}
239+
vstr_ins_char(rl.line, rl.cursor_pos+rl.utf8_cont_chars, c);
240+
// set redraw parameters if we have the entire character
241+
if (rl.utf8_cont_chars == cont_need) {
242+
redraw_from_cursor = true;
243+
redraw_step_forward = rl.utf8_cont_chars+1;
244+
cont_chars = rl.utf8_cont_chars;
251245
}
252246
}
253247
} else if (rl.escape_seq == ESEQ_ESC) {
@@ -274,7 +268,7 @@ int readline_process_char(int c) {
274268
#endif
275269
// up arrow
276270
if (rl.hist_cur + 1 < (int)READLINE_HIST_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) {
277-
// Check for continuation characters through the cursor_pos
271+
// Check for continuation characters
278272
cont_chars = count_cont_bytes(rl.line->buf+rl.orig_line_len, rl.line->buf+rl.cursor_pos);
279273
// increase hist num
280274
rl.hist_cur += 1;
@@ -292,7 +286,7 @@ int readline_process_char(int c) {
292286
#endif
293287
// down arrow
294288
if (rl.hist_cur >= 0) {
295-
// Check for continuation characters through the cursor_pos
289+
// Check for continuation characters
296290
cont_chars = count_cont_bytes(rl.line->buf+rl.orig_line_len, rl.line->buf+rl.cursor_pos);
297291
// decrease hist num
298292
rl.hist_cur -= 1;
@@ -390,7 +384,7 @@ int readline_process_char(int c) {
390384
// erase old chars
391385
mp_hal_erase_line_from_cursor(last_line_len - rl.cursor_pos);
392386
}
393-
// Check for continuation characters from the new cursor_pos to the EOL
387+
// Check for continuation characters
394388
cont_chars = count_cont_bytes(rl.line->buf+rl.cursor_pos+redraw_step_forward, rl.line->buf+rl.line->len);
395389
// draw new chars
396390
mp_hal_stdout_tx_strn(rl.line->buf + rl.cursor_pos, rl.line->len - rl.cursor_pos);

0 commit comments

Comments
 (0)