--- lbbs/src/str_process.c 2025/06/14 02:58:11 1.14 +++ lbbs/src/str_process.c 2025/10/18 12:06:10 1.21 @@ -14,13 +14,59 @@ * * ***************************************************************************/ -#include "str_process.h" #include "common.h" #include "log.h" +#include "str_process.h" #include #include -int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len) +int str_length(const char *str, int skip_ctrl_seq) +{ + int i; + char c; + int ret = 0; + + for (i = 0; str[i] != '\0'; i++) + { + c = str[i]; + + if (c == '\r' || c == '\7') // skip + { + continue; + } + + if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence + { + i += 2; + while (str[i] != '\0' && str[i] != 'm') + { + i++; + } + continue; + } + + // Process UTF-8 Chinese characters + if (c & 0x80) // head of multi-byte character + { + c = (c & 0x70) << 1; + while (c & 0x80) + { + i++; + c = (c & 0x7f) << 1; + } + + ret += 2; + } + else + { + ret++; + } + } + + return ret; +} + +int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq) { int i; *p_eol = 0; @@ -36,7 +82,7 @@ int split_line(const char *buffer, int m continue; } - if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence + if (skip_ctrl_seq && c == '\033' && buffer[i + 1] == '[') // Skip control sequence { i += 2; while (buffer[i] != '\0' && buffer[i] != 'm') @@ -46,13 +92,20 @@ int split_line(const char *buffer, int m continue; } - if (c < 0 || c > 127) // GBK chinese character + if (c & 0x80) // head of multi-byte character { if (*p_display_len + 2 > max_display_len) { break; } - i++; + + c = (c & 0x70) << 1; + while (c & 0x80) + { + i++; + c = (c & 0x7f) << 1; + } + (*p_display_len) += 2; } else @@ -76,7 +129,8 @@ int split_line(const char *buffer, int m return i; } -long split_data_lines(const char *p_buf, int max_display_len, long *p_line_offsets, long line_offsets_count) +long split_data_lines(const char *p_buf, int max_display_len, long *p_line_offsets, long line_offsets_count, + int skip_ctrl_seq, int *p_line_widths) { int line_cnt = 0; int len; @@ -87,12 +141,17 @@ long split_data_lines(const char *p_buf, do { - len = split_line(p_buf, max_display_len, &end_of_line, &display_len); + len = split_line(p_buf, max_display_len, &end_of_line, &display_len, skip_ctrl_seq); + + if (p_line_widths) + { + p_line_widths[line_cnt] = display_len; + } // Exceed max_line_cnt if (line_cnt + 1 >= line_offsets_count) { - log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count); + // log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count); return line_cnt; } @@ -104,22 +163,19 @@ long split_data_lines(const char *p_buf, return line_cnt; } -int ctrl_seq_filter(char *buffer) +int str_filter(char *buffer, int skip_ctrl_seq) { int i; int j; - char c; for (i = 0, j = 0; buffer[i] != '\0'; i++) { - c = buffer[i]; - - if (c == '\r' || c == '\7') // skip + if (buffer[i] == '\r' || buffer[i] == '\7') // skip { continue; } - if (c == '\033' && buffer[i + 1] == '[') // Skip control sequence + if (skip_ctrl_seq && buffer[i] == '\033' && buffer[i + 1] == '[') // Skip control sequence { i += 2; while (buffer[i] != '\0' && buffer[i] != 'm')