| 20 |
#include <stdio.h> |
#include <stdio.h> |
| 21 |
#include <string.h> |
#include <string.h> |
| 22 |
|
|
| 23 |
|
int str_length(const char *str, int skip_ctrl_seq) |
| 24 |
|
{ |
| 25 |
|
int i; |
| 26 |
|
char c; |
| 27 |
|
int ret = 0; |
| 28 |
|
|
| 29 |
|
for (i = 0; str[i] != '\0'; i++) |
| 30 |
|
{ |
| 31 |
|
c = str[i]; |
| 32 |
|
|
| 33 |
|
if (c == '\r' || c == '\7') // skip |
| 34 |
|
{ |
| 35 |
|
continue; |
| 36 |
|
} |
| 37 |
|
|
| 38 |
|
if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence |
| 39 |
|
{ |
| 40 |
|
i += 2; |
| 41 |
|
while (str[i] != '\0' && str[i] != 'm') |
| 42 |
|
{ |
| 43 |
|
i++; |
| 44 |
|
} |
| 45 |
|
continue; |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
// Process UTF-8 Chinese characters |
| 49 |
|
if (c & 0b10000000) // head of multi-byte character |
| 50 |
|
{ |
| 51 |
|
c = (c & 0b01110000) << 1; |
| 52 |
|
while (c & 0b10000000) |
| 53 |
|
{ |
| 54 |
|
i++; |
| 55 |
|
c = (c & 0b01111111) << 1; |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
ret += 2; |
| 59 |
|
} |
| 60 |
|
else |
| 61 |
|
{ |
| 62 |
|
ret++; |
| 63 |
|
} |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
return ret; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq) |
int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq) |
| 70 |
{ |
{ |
| 71 |
int i; |
int i; |
| 92 |
continue; |
continue; |
| 93 |
} |
} |
| 94 |
|
|
| 95 |
if (c < 0 || c > 127) // GBK chinese character |
if (c & 0b10000000) // head of multi-byte character |
| 96 |
{ |
{ |
| 97 |
if (*p_display_len + 2 > max_display_len) |
if (*p_display_len + 2 > max_display_len) |
| 98 |
{ |
{ |
| 99 |
break; |
break; |
| 100 |
} |
} |
| 101 |
i++; |
|
| 102 |
|
c = (c & 0b01110000) << 1; |
| 103 |
|
while (c & 0b10000000) |
| 104 |
|
{ |
| 105 |
|
i++; |
| 106 |
|
c = (c & 0b01111111) << 1; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
(*p_display_len) += 2; |
(*p_display_len) += 2; |
| 110 |
} |
} |
| 111 |
else |
else |