--- lbbs/src/str_process.c 2025/06/21 02:15:18 1.18 +++ lbbs/src/str_process.c 2025/11/04 13:49:51 1.23 @@ -1,25 +1,75 @@ -/*************************************************************************** - str_process.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * str_process + * - common string processing features with UTF-8 support + * + * Copyright (C) 2004-2025 by Leaflet + */ #include "common.h" #include "log.h" #include "str_process.h" +#include #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 + { + for (i = i + 2; isdigit(str[i]) || str[i] == ';' || str[i] == '?'; i++) + ; + + if (str[i] == 'm') // valid + { + // skip + } + else if (isalpha(str[i])) + { + // unsupported ANSI CSI command + } + else + { + 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 +96,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 +133,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, int skip_ctrl_seq) +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; @@ -89,6 +147,11 @@ long split_data_lines(const char *p_buf, { 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) {