| 17 |
#include "common.h" |
#include "common.h" |
| 18 |
#include "log.h" |
#include "log.h" |
| 19 |
#include "str_process.h" |
#include "str_process.h" |
| 20 |
|
#include <ctype.h> |
| 21 |
#include <stdio.h> |
#include <stdio.h> |
| 22 |
#include <string.h> |
#include <string.h> |
| 23 |
|
|
| 24 |
|
int str_length(const char *str, int skip_ctrl_seq) |
| 25 |
|
{ |
| 26 |
|
int i; |
| 27 |
|
char c; |
| 28 |
|
int ret = 0; |
| 29 |
|
|
| 30 |
|
for (i = 0; str[i] != '\0'; i++) |
| 31 |
|
{ |
| 32 |
|
c = str[i]; |
| 33 |
|
|
| 34 |
|
if (c == '\r' || c == '\7') // skip |
| 35 |
|
{ |
| 36 |
|
continue; |
| 37 |
|
} |
| 38 |
|
|
| 39 |
|
if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence |
| 40 |
|
{ |
| 41 |
|
for (i = i + 2; isdigit(str[i]) || str[i] == ';' || str[i] == '?'; i++) |
| 42 |
|
; |
| 43 |
|
|
| 44 |
|
if (str[i] == 'm') // valid |
| 45 |
|
{ |
| 46 |
|
// skip |
| 47 |
|
} |
| 48 |
|
else if (isalpha(str[i])) |
| 49 |
|
{ |
| 50 |
|
// unsupported ANSI CSI command |
| 51 |
|
} |
| 52 |
|
else |
| 53 |
|
{ |
| 54 |
|
i--; |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
continue; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
// Process UTF-8 Chinese characters |
| 61 |
|
if (c & 0x80) // head of multi-byte character |
| 62 |
|
{ |
| 63 |
|
c = (c & 0x70) << 1; |
| 64 |
|
while (c & 0x80) |
| 65 |
|
{ |
| 66 |
|
i++; |
| 67 |
|
c = (c & 0x7f) << 1; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
ret += 2; |
| 71 |
|
} |
| 72 |
|
else |
| 73 |
|
{ |
| 74 |
|
ret++; |
| 75 |
|
} |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
return ret; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
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) |
| 82 |
{ |
{ |
| 83 |
int i; |
int i; |
| 104 |
continue; |
continue; |
| 105 |
} |
} |
| 106 |
|
|
| 107 |
if (c < 0 || c > 127) // GBK chinese character |
if (c & 0x80) // head of multi-byte character |
| 108 |
{ |
{ |
| 109 |
if (*p_display_len + 2 > max_display_len) |
if (*p_display_len + 2 > max_display_len) |
| 110 |
{ |
{ |
| 111 |
break; |
break; |
| 112 |
} |
} |
| 113 |
i++; |
|
| 114 |
|
c = (c & 0x70) << 1; |
| 115 |
|
while (c & 0x80) |
| 116 |
|
{ |
| 117 |
|
i++; |
| 118 |
|
c = (c & 0x7f) << 1; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
(*p_display_len) += 2; |
(*p_display_len) += 2; |
| 122 |
} |
} |
| 123 |
else |
else |