Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for fullwidth characters of the CJK charset (Chinese, Japanese, Korean). #10579

Merged
merged 2 commits into from Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions libr/cons/canvas.c
Expand Up @@ -266,25 +266,32 @@ beach: {
}

static int utf8len_fixed(const char *s, int n) {
int i = 0, j = 0;
int i = 0, j = 0, fullwidths = 0;
while (s[i] && n > 0) {
if ((s[i] & 0xc0) != 0x80) {
j++;
if (r_str_char_fullwidth (s + i, n)) {
fullwidths++;
}
}
n--;
i++;
}
return j;
return j + fullwidths;
}

static int bytes_utf8len(const char *s, int n, int left) {
int i = 0;
int i = 0, fullwidths = 0;
while (s[i] && n > -1 && i < left) {
if (r_str_char_fullwidth (s + i, left)) {
fullwidths++;
}
if ((s[i] & 0xc0) != 0x80) {
n--;
}
i++;
}
i -= fullwidths;
return n == -1 ? i - 1 : i;
}

Expand Down Expand Up @@ -430,6 +437,9 @@ R_API char *r_cons_canvas_to_string(RConsCanvas *c) {
olen += len;
}
attr_x++;
if (r_str_char_fullwidth (c->b[y] + x, c->bsize[c->y] - x)) {
attr_x++;
}
}
if (!c->b[y][x] || c->b[y][x] == '\n') {
o[olen++] = ' ';
Expand Down
2 changes: 2 additions & 0 deletions libr/include/r_util/r_str.h
Expand Up @@ -38,6 +38,8 @@ R_API bool r_str_range_in(const char *r, ut64 addr);
R_API int r_str_len_utf8(const char *s);
R_API int r_str_len_utf8char(const char *s, int left);
R_API void r_str_filter_zeroline(char *str, int len);
R_API int r_str_utf8_codepoint (char* s, int left);
R_API bool r_str_char_fullwidth (char* s, int left);
R_API int r_str_write(int fd, const char *b);
R_API void r_str_ncpy(char *dst, const char *src, int n);
R_API void r_str_sanitize(char *c);
Expand Down
51 changes: 47 additions & 4 deletions libr/util/str.c
Expand Up @@ -1511,13 +1511,13 @@ R_API int r_str_ansi_filter(char *str, char **out, int **cposs, int len) {

R_API char *r_str_ansi_crop(const char *str, ut32 x, ut32 y, ut32 x2, ut32 y2) {
char *r, *r_end, *ret;
const char *s;
const char *s, *s_start;
size_t r_len, str_len = 0, nr_of_lines = 0;
ut32 ch = 0, cw = 0;
if (x2 < 1 || y2 < 1 || !str) {
return strdup ("");
}
s = str;
s = s_start = str;
while (*s) {
str_len++;
if (*s == '\n') {
Expand Down Expand Up @@ -1560,6 +1560,14 @@ R_API char *r_str_ansi_crop(const char *str, ut32 x, ut32 y, ut32 x2, ut32 y2) {
}
continue;
}
if (r_str_char_fullwidth (str, str_len - (str - s_start))) {
cw++;
if (cw == x) {
*r++ = ' ';
str++;
continue;
}
}
if (*str == 0x1b && *(str + 1) == '[') {
const char *ptr = str;
if ((r_end - r) > 2) {
Expand Down Expand Up @@ -1592,6 +1600,38 @@ R_API char *r_str_ansi_crop(const char *str, ut32 x, ut32 y, ut32 x2, ut32 y2) {
return ret;
}

R_API int r_str_utf8_codepoint (char* s, int left) {
bool safe = left >= 0;
if ((*s & 0x80) != 0x80) {
return 0;
} else if ((*s & 0xe0) == 0xc0 && (safe ? left >= 1 : *(s + 1))) {
return ((*s & 0x1f) << 6) + (*(s + 1) & 0x3f);
} else if ((*s & 0xf0) == 0xe0 && (safe ? left >= 2 : (*(s + 1) && *(s + 2)))) {
return ((*s & 0xf) << 12) + ((*(s + 1) & 0x3f) << 6) + (*(s + 2) & 0x3f);
} else if ((*s & 0xf8) == 0xf0 && (safe ? left >= 3 : (*(s + 1) && *(s + 2) && *(s + 3)))) {
return ((*s & 0x7) << 18) + ((*(s + 1) & 0x3f) << 12) + ((*(s + 2) & 0x3f) << 6) + (*(s + 3) & 0x3f);
}
return 0;
}

R_API bool r_str_char_fullwidth (char* s, int left) {
int codepoint = r_str_utf8_codepoint (s, left);
return (codepoint >= 0x1100 &&
(codepoint <= 0x115f || /* Hangul Jamo init. consonants */
codepoint == 0x2329 || codepoint == 0x232a ||
(R_BETWEEN (0x2e80, codepoint, 0xa4cf)
&& codepoint != 0x303f) || /* CJK ... Yi */
R_BETWEEN (0xac00, codepoint, 0xd7a3) || /* Hangul Syllables */
R_BETWEEN (0xf900, codepoint, 0xfaff) || /* CJK Compatibility Ideographs */
R_BETWEEN (0xfe10, codepoint, 0xfe19) || /* Vertical forms */
R_BETWEEN (0xfe30, codepoint, 0xfe6f) || /* CJK Compatibility Forms */
R_BETWEEN (0xff00, codepoint, 0xff60) || /* Fullwidth Forms */
R_BETWEEN (0xffe0, codepoint, 0xffe6) ||
R_BETWEEN (0x20000, codepoint, 0x2fffd) ||
R_BETWEEN (0x30000, codepoint, 0x3fffd)));

}

R_API void r_str_filter_zeroline(char *str, int len) {
int i;
for (i = 0; i < len && str[i]; i++) {
Expand Down Expand Up @@ -1898,14 +1938,17 @@ R_API int r_str_len_utf8char(const char *s, int left) {
}

R_API int r_str_len_utf8(const char *s) {
int i = 0, j = 0;
int i = 0, j = 0, fullwidths = 0;
while (s[i]) {
if ((s[i] & 0xc0) != 0x80) {
j++;
if (r_str_char_fullwidth (s + i, 4)) {
fullwidths++;
}
}
i++;
}
return j;
return j + fullwidths;
}

R_API const char *r_str_casestr(const char *a, const char *b) {
Expand Down