--- lbbs/src/str_process.c 2025/07/02 04:17:33 1.20 +++ lbbs/src/str_process.c 2025/11/04 13:49:51 1.23 @@ -1,22 +1,15 @@ -/*************************************************************************** - 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 @@ -37,22 +30,33 @@ int str_length(const char *str, int skip if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence { - i += 2; - while (str[i] != '\0' && str[i] != 'm') + for (i = i + 2; isdigit(str[i]) || str[i] == ';' || str[i] == '?'; i++) + ; + + if (str[i] == 'm') // valid { - i++; + // skip + } + else if (isalpha(str[i])) + { + // unsupported ANSI CSI command + } + else + { + i--; } + continue; } // Process UTF-8 Chinese characters - if (c & 0b10000000) // head of multi-byte character + if (c & 0x80) // head of multi-byte character { - c = (c & 0b01110000) << 1; - while (c & 0b10000000) + c = (c & 0x70) << 1; + while (c & 0x80) { i++; - c = (c & 0b01111111) << 1; + c = (c & 0x7f) << 1; } ret += 2; @@ -92,18 +96,18 @@ int split_line(const char *buffer, int m continue; } - if (c & 0b10000000) // head of multi-byte character + if (c & 0x80) // head of multi-byte character { if (*p_display_len + 2 > max_display_len) { break; } - c = (c & 0b01110000) << 1; - while (c & 0b10000000) + c = (c & 0x70) << 1; + while (c & 0x80) { i++; - c = (c & 0b01111111) << 1; + c = (c & 0x7f) << 1; } (*p_display_len) += 2;