| 11 |
#include "str_process.h" |
#include "str_process.h" |
| 12 |
#include <ctype.h> |
#include <ctype.h> |
| 13 |
#include <stdio.h> |
#include <stdio.h> |
| 14 |
|
#include <stdlib.h> |
| 15 |
#include <string.h> |
#include <string.h> |
| 16 |
|
#include <wchar.h> |
| 17 |
|
|
| 18 |
|
int UTF8_fixed_width = 1; |
| 19 |
|
|
| 20 |
int str_length(const char *str, int skip_ctrl_seq) |
int str_length(const char *str, int skip_ctrl_seq) |
| 21 |
{ |
{ |
| 22 |
|
int str_len; |
| 23 |
|
char input_str[5]; |
| 24 |
|
wchar_t wcs[2]; |
| 25 |
|
int wc_len; |
| 26 |
int i; |
int i; |
| 27 |
char c; |
char c; |
| 28 |
int ret = 0; |
int ret = 0; |
| 60 |
// Process UTF-8 Chinese characters |
// Process UTF-8 Chinese characters |
| 61 |
if (c & 0x80) // head of multi-byte character |
if (c & 0x80) // head of multi-byte character |
| 62 |
{ |
{ |
| 63 |
c = (c & 0x70) << 1; |
str_len = 0; |
| 64 |
|
c = (char)(c & 0xf0); |
| 65 |
while (c & 0x80) |
while (c & 0x80) |
| 66 |
{ |
{ |
| 67 |
i++; |
input_str[str_len] = str[i + str_len]; |
| 68 |
|
str_len++; |
| 69 |
c = (c & 0x7f) << 1; |
c = (c & 0x7f) << 1; |
| 70 |
} |
} |
| 71 |
|
input_str[str_len] = '\0'; |
| 72 |
|
|
| 73 |
ret += 2; |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1) |
| 74 |
|
{ |
| 75 |
|
log_error("mbstowcs(%s) error\n", input_str); |
| 76 |
|
} |
| 77 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 78 |
|
|
| 79 |
|
i += (str_len - 1); |
| 80 |
|
ret += wc_len; |
| 81 |
} |
} |
| 82 |
else |
else |
| 83 |
{ |
{ |
| 94 |
*p_eol = 0; |
*p_eol = 0; |
| 95 |
*p_display_len = 0; |
*p_display_len = 0; |
| 96 |
char c; |
char c; |
| 97 |
|
int str_len; |
| 98 |
|
char input_str[5]; |
| 99 |
|
wchar_t wcs[2]; |
| 100 |
|
int wc_len; |
| 101 |
|
|
| 102 |
for (i = 0; buffer[i] != '\0'; i++) |
for (i = 0; buffer[i] != '\0'; i++) |
| 103 |
{ |
{ |
| 120 |
|
|
| 121 |
if (c & 0x80) // head of multi-byte character |
if (c & 0x80) // head of multi-byte character |
| 122 |
{ |
{ |
| 123 |
if (*p_display_len + 2 > max_display_len) |
str_len = 0; |
| 124 |
|
c = (char)(c & 0xf0); |
| 125 |
|
while (c & 0x80) |
| 126 |
{ |
{ |
| 127 |
break; |
input_str[str_len] = buffer[i + str_len]; |
| 128 |
|
str_len++; |
| 129 |
|
c = (c & 0x7f) << 1; |
| 130 |
} |
} |
| 131 |
|
input_str[str_len] = '\0'; |
| 132 |
|
|
| 133 |
c = (c & 0x70) << 1; |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1) |
|
while (c & 0x80) |
|
| 134 |
{ |
{ |
| 135 |
i++; |
log_error("mbstowcs(%s) error\n", input_str); |
| 136 |
c = (c & 0x7f) << 1; |
} |
| 137 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 138 |
|
if (*p_display_len + wc_len > max_display_len) |
| 139 |
|
{ |
| 140 |
|
break; |
| 141 |
} |
} |
| 142 |
|
|
| 143 |
(*p_display_len) += 2; |
i += (str_len - 1); |
| 144 |
|
(*p_display_len) += wc_len; |
| 145 |
} |
} |
| 146 |
else |
else |
| 147 |
{ |
{ |
| 193 |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len; |
| 194 |
line_cnt++; |
line_cnt++; |
| 195 |
p_buf += len; |
p_buf += len; |
| 196 |
} while (p_buf[0] != '\0'); |
} while (p_buf[0] != '\0' || end_of_line); |
| 197 |
|
|
| 198 |
return line_cnt; |
return line_cnt; |
| 199 |
} |
} |