| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
str_process.c - description |
/* |
| 3 |
------------------- |
* str_process |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - common string processing features with UTF-8 support |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "common.h" |
#include "common.h" |
| 14 |
#include "log.h" |
#include "log.h" |
| 15 |
#include "str_process.h" |
#include "str_process.h" |
| 16 |
#include <ctype.h> |
#include <ctype.h> |
| 17 |
#include <stdio.h> |
#include <stdio.h> |
| 18 |
|
#include <stdlib.h> |
| 19 |
#include <string.h> |
#include <string.h> |
| 20 |
|
#include <wchar.h> |
| 21 |
|
|
| 22 |
|
int UTF8_fixed_width = 1; |
| 23 |
|
|
| 24 |
int str_length(const char *str, int skip_ctrl_seq) |
int str_length(const char *str, int skip_ctrl_seq) |
| 25 |
{ |
{ |
| 26 |
|
int str_len; |
| 27 |
|
char input_str[5]; |
| 28 |
|
wchar_t wcs[2]; |
| 29 |
|
int wc_len; |
| 30 |
int i; |
int i; |
| 31 |
char c; |
char c; |
| 32 |
int ret = 0; |
int ret = 0; |
| 42 |
|
|
| 43 |
if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence |
if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence |
| 44 |
{ |
{ |
| 45 |
for (i = i + 2; isdigit(str[i]) || str[i] == ';' || str[i] == '?'; i++) |
for (i = i + 2; isdigit((int)str[i]) || str[i] == ';' || str[i] == '?'; i++) |
| 46 |
; |
; |
| 47 |
|
|
| 48 |
if (str[i] == 'm') // valid |
if (str[i] == 'm') // valid |
| 49 |
{ |
{ |
| 50 |
// skip |
// skip |
| 51 |
} |
} |
| 52 |
else if (isalpha(str[i])) |
else if (isalpha((int)str[i])) |
| 53 |
{ |
{ |
| 54 |
// unsupported ANSI CSI command |
// unsupported ANSI CSI command |
| 55 |
} |
} |
| 64 |
// Process UTF-8 Chinese characters |
// Process UTF-8 Chinese characters |
| 65 |
if (c & 0x80) // head of multi-byte character |
if (c & 0x80) // head of multi-byte character |
| 66 |
{ |
{ |
| 67 |
c = (c & 0x70) << 1; |
str_len = 0; |
| 68 |
|
c = (char)(c & 0xf0); |
| 69 |
while (c & 0x80) |
while (c & 0x80) |
| 70 |
{ |
{ |
| 71 |
i++; |
input_str[str_len] = str[i + str_len]; |
| 72 |
|
str_len++; |
| 73 |
c = (c & 0x7f) << 1; |
c = (c & 0x7f) << 1; |
| 74 |
} |
} |
| 75 |
|
input_str[str_len] = '\0'; |
| 76 |
|
|
| 77 |
ret += 2; |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1) |
| 78 |
|
{ |
| 79 |
|
log_debug("mbstowcs(%s) error", input_str); |
| 80 |
|
wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback |
| 81 |
|
} |
| 82 |
|
else |
| 83 |
|
{ |
| 84 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
i += (str_len - 1); |
| 88 |
|
ret += wc_len; |
| 89 |
} |
} |
| 90 |
else |
else |
| 91 |
{ |
{ |
| 102 |
*p_eol = 0; |
*p_eol = 0; |
| 103 |
*p_display_len = 0; |
*p_display_len = 0; |
| 104 |
char c; |
char c; |
| 105 |
|
int str_len; |
| 106 |
|
char input_str[5]; |
| 107 |
|
wchar_t wcs[2]; |
| 108 |
|
int wc_len; |
| 109 |
|
|
| 110 |
for (i = 0; buffer[i] != '\0'; i++) |
for (i = 0; buffer[i] != '\0'; i++) |
| 111 |
{ |
{ |
| 128 |
|
|
| 129 |
if (c & 0x80) // head of multi-byte character |
if (c & 0x80) // head of multi-byte character |
| 130 |
{ |
{ |
| 131 |
if (*p_display_len + 2 > max_display_len) |
str_len = 0; |
| 132 |
|
c = (char)(c & 0xf0); |
| 133 |
|
while (c & 0x80) |
| 134 |
{ |
{ |
| 135 |
break; |
input_str[str_len] = buffer[i + str_len]; |
| 136 |
|
str_len++; |
| 137 |
|
c = (c & 0x7f) << 1; |
| 138 |
} |
} |
| 139 |
|
input_str[str_len] = '\0'; |
| 140 |
|
|
| 141 |
c = (c & 0x70) << 1; |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1) |
|
while (c & 0x80) |
|
| 142 |
{ |
{ |
| 143 |
i++; |
log_debug("mbstowcs(%s) error", input_str); |
| 144 |
c = (c & 0x7f) << 1; |
wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback |
| 145 |
|
} |
| 146 |
|
else |
| 147 |
|
{ |
| 148 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
if (*p_display_len + wc_len > max_display_len) |
| 152 |
|
{ |
| 153 |
|
break; |
| 154 |
} |
} |
| 155 |
|
|
| 156 |
(*p_display_len) += 2; |
i += (str_len - 1); |
| 157 |
|
(*p_display_len) += wc_len; |
| 158 |
} |
} |
| 159 |
else |
else |
| 160 |
{ |
{ |
| 199 |
// Exceed max_line_cnt |
// Exceed max_line_cnt |
| 200 |
if (line_cnt + 1 >= line_offsets_count) |
if (line_cnt + 1 >= line_offsets_count) |
| 201 |
{ |
{ |
| 202 |
// log_error("Line count %d reaches limit %d\n", line_cnt + 1, line_offsets_count); |
log_debug("Line count %d reaches limit %d", line_cnt + 1, line_offsets_count); |
| 203 |
return line_cnt; |
return line_cnt; |
| 204 |
} |
} |
| 205 |
|
|
| 206 |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
| 207 |
line_cnt++; |
line_cnt++; |
| 208 |
p_buf += len; |
p_buf += len; |
| 209 |
} while (p_buf[0] != '\0'); |
} while (p_buf[0] != '\0' || end_of_line); |
| 210 |
|
|
| 211 |
return line_cnt; |
return line_cnt; |
| 212 |
} |
} |