| 6 |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
#ifdef HAVE_CONFIG_H |
| 10 |
|
#include "config.h" |
| 11 |
|
#endif |
| 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; |
| 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 |
|
#ifdef _DEBUG |
| 80 |
|
log_error("mbstowcs(%s) error\n", input_str); |
| 81 |
|
#endif |
| 82 |
|
wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback |
| 83 |
|
} |
| 84 |
|
else |
| 85 |
|
{ |
| 86 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
i += (str_len - 1); |
| 90 |
|
ret += wc_len; |
| 91 |
} |
} |
| 92 |
else |
else |
| 93 |
{ |
{ |
| 104 |
*p_eol = 0; |
*p_eol = 0; |
| 105 |
*p_display_len = 0; |
*p_display_len = 0; |
| 106 |
char c; |
char c; |
| 107 |
|
int str_len; |
| 108 |
|
char input_str[5]; |
| 109 |
|
wchar_t wcs[2]; |
| 110 |
|
int wc_len; |
| 111 |
|
|
| 112 |
for (i = 0; buffer[i] != '\0'; i++) |
for (i = 0; buffer[i] != '\0'; i++) |
| 113 |
{ |
{ |
| 130 |
|
|
| 131 |
if (c & 0x80) // head of multi-byte character |
if (c & 0x80) // head of multi-byte character |
| 132 |
{ |
{ |
| 133 |
if (*p_display_len + 2 > max_display_len) |
str_len = 0; |
| 134 |
|
c = (char)(c & 0xf0); |
| 135 |
|
while (c & 0x80) |
| 136 |
{ |
{ |
| 137 |
break; |
input_str[str_len] = buffer[i + str_len]; |
| 138 |
|
str_len++; |
| 139 |
|
c = (c & 0x7f) << 1; |
| 140 |
} |
} |
| 141 |
|
input_str[str_len] = '\0'; |
| 142 |
|
|
| 143 |
c = (c & 0x70) << 1; |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1) |
|
while (c & 0x80) |
|
| 144 |
{ |
{ |
| 145 |
i++; |
#ifdef _DEBUG |
| 146 |
c = (c & 0x7f) << 1; |
log_error("mbstowcs(%s) error\n", input_str); |
| 147 |
|
#endif |
| 148 |
|
wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback |
| 149 |
|
} |
| 150 |
|
else |
| 151 |
|
{ |
| 152 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
if (*p_display_len + wc_len > max_display_len) |
| 156 |
|
{ |
| 157 |
|
break; |
| 158 |
} |
} |
| 159 |
|
|
| 160 |
(*p_display_len) += 2; |
i += (str_len - 1); |
| 161 |
|
(*p_display_len) += wc_len; |
| 162 |
} |
} |
| 163 |
else |
else |
| 164 |
{ |
{ |