--- lbbs/src/editor.c 2025/11/08 08:21:31 1.55 +++ lbbs/src/editor.c 2025/11/08 12:32:16 1.56 @@ -647,9 +647,10 @@ int editor_display(EDITOR_DATA *p_editor EDITOR_CTX ctx; int ch = 0; char input_str[5]; + int str_len = 0; wchar_t wcs[2]; + int wc_len; char c; - int str_len = 0; int input_ok; const int screen_begin_row = 1; const int screen_row_total = SCREEN_ROWS - screen_begin_row; @@ -1025,13 +1026,36 @@ int editor_display(EDITOR_DATA *p_editor offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], (int)col_pos - 1, &eol, &display_len, 0); col_pos = display_len; - if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 + if (offset_in > 0) { - split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], - (int)col_pos - 1, &eol, &display_len, 0); - if (display_len == col_pos - 2) + str_len = 1; + offset_in--; + if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0 || + p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] > 127) // UTF8 { - col_pos--; + while (offset_in > 0 && + (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xc0) != 0xc0) + { + str_len++; + offset_in--; + } + + if (str_len > 4) + { + log_error("Invalid UTF-8 data detected: str_len > 4\n"); + } + + if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) == + (size_t)-1) + { + log_error("mbstowcs() error\n"); + } + wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); + + if (wc_len == 2) + { + col_pos--; + } } } if (col_pos >= 1) @@ -1062,15 +1086,41 @@ int editor_display(EDITOR_DATA *p_editor offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], (int)col_pos - 1, &eol, &display_len, 0); col_pos = display_len + 2; - if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] && - p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8 + if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]) { - split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, - 1, &eol, &display_len, 0); - if (display_len == 0) + str_len = 0; + if ((p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0x80) == + 0x80) // head of multi-byte character + { + c = (char)(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xf0); + while (c & 0x80) + { + str_len++; + c = (c & 0x7f) << 1; + } + + if (str_len > 4) + { + log_error("Invalid UTF-8 data detected: str_len > 4\n"); + } + + if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) == + (size_t)-1) + { + log_error("mbstowcs() error\n"); + } + wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); + + if (wc_len == 2) + { + col_pos++; + } + } + else { - col_pos++; + str_len = 1; } + offset_in += str_len; } if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos]) {