--- lbbs/src/str_process.c 2025/07/02 03:08:10 1.19 +++ lbbs/src/str_process.c 2025/10/18 12:06:10 1.21 @@ -20,6 +20,52 @@ #include #include +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; @@ -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