mirror of
https://github.com/tmux/tmux.git
synced 2025-09-02 05:21:10 +00:00
Merge branch 'obsd-master'
This commit is contained in:
341
utf8.c
341
utf8.c
@ -26,153 +26,157 @@
|
||||
|
||||
#include "tmux.h"
|
||||
|
||||
static int utf8_width(wchar_t);
|
||||
|
||||
struct utf8_big_item {
|
||||
u_int index;
|
||||
RB_ENTRY(utf8_big_item) entry;
|
||||
struct utf8_item {
|
||||
u_int offset;
|
||||
RB_ENTRY(utf8_item) entry;
|
||||
|
||||
char data[UTF8_SIZE];
|
||||
u_char size;
|
||||
};
|
||||
RB_HEAD(utf8_big_tree, utf8_big_item);
|
||||
RB_HEAD(utf8_tree, utf8_item);
|
||||
|
||||
static int
|
||||
utf8_big_cmp(struct utf8_big_item *bi1, struct utf8_big_item *bi2)
|
||||
utf8_cmp(struct utf8_item *ui1, struct utf8_item *ui2)
|
||||
{
|
||||
if (bi1->size < bi2->size)
|
||||
if (ui1->size < ui2->size)
|
||||
return (-1);
|
||||
if (bi1->size > bi2->size)
|
||||
if (ui1->size > ui2->size)
|
||||
return (1);
|
||||
return (memcmp(bi1->data, bi2->data, bi1->size));
|
||||
return (memcmp(ui1->data, ui2->data, ui1->size));
|
||||
}
|
||||
RB_GENERATE_STATIC(utf8_big_tree, utf8_big_item, entry, utf8_big_cmp);
|
||||
static struct utf8_big_tree utf8_big_tree = RB_INITIALIZER(utf8_big_tree);
|
||||
RB_GENERATE_STATIC(utf8_tree, utf8_item, entry, utf8_cmp);
|
||||
static struct utf8_tree utf8_tree = RB_INITIALIZER(utf8_tree);
|
||||
|
||||
static struct utf8_big_item *utf8_big_list;
|
||||
static u_int utf8_big_list_size;
|
||||
static u_int utf8_big_list_used;
|
||||
static struct utf8_item *utf8_list;
|
||||
static u_int utf8_list_size;
|
||||
static u_int utf8_list_used;
|
||||
|
||||
union utf8_big_map {
|
||||
u_int value;
|
||||
union utf8_map {
|
||||
utf8_char uc;
|
||||
struct {
|
||||
u_char flags;
|
||||
#define UTF8_BIG_SIZE 0x1f
|
||||
#define UTF8_BIG_WIDTH2 0x20
|
||||
#define UTF8_FLAG_SIZE 0x1f
|
||||
#define UTF8_FLAG_WIDTH2 0x20
|
||||
|
||||
u_char data[3];
|
||||
};
|
||||
} __packed;
|
||||
|
||||
static const union utf8_big_map utf8_big_space1 = {
|
||||
static const union utf8_map utf8_space1 = {
|
||||
.flags = 1,
|
||||
.data = " "
|
||||
};
|
||||
static const union utf8_big_map utf8_big_space2 = {
|
||||
.flags = UTF8_BIG_WIDTH2|2,
|
||||
static const union utf8_map utf8_space2 = {
|
||||
.flags = UTF8_FLAG_WIDTH2|2,
|
||||
.data = " "
|
||||
};
|
||||
|
||||
/* Get a big item by index. */
|
||||
static struct utf8_big_item *
|
||||
utf8_get_big_item(const char *data, size_t size)
|
||||
/* Get a UTF-8 item by offset. */
|
||||
static struct utf8_item *
|
||||
utf8_get_item(const char *data, size_t size)
|
||||
{
|
||||
struct utf8_big_item bi;
|
||||
struct utf8_item ui;
|
||||
|
||||
memcpy(bi.data, data, size);
|
||||
bi.size = size;
|
||||
memcpy(ui.data, data, size);
|
||||
ui.size = size;
|
||||
|
||||
return (RB_FIND(utf8_big_tree, &utf8_big_tree, &bi));
|
||||
return (RB_FIND(utf8_tree, &utf8_tree, &ui));
|
||||
}
|
||||
|
||||
/* Add a big item. */
|
||||
/* Expand UTF-8 list. */
|
||||
static int
|
||||
utf8_put_big_item(const char *data, size_t size, u_int *index)
|
||||
utf8_expand_list(void)
|
||||
{
|
||||
struct utf8_big_item *bi;
|
||||
|
||||
bi = utf8_get_big_item(data, size);
|
||||
if (bi != NULL) {
|
||||
*index = bi->index;
|
||||
log_debug("%s: have %.*s at %u", __func__, (int)size, data,
|
||||
*index);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (utf8_big_list_used == utf8_big_list_size) {
|
||||
if (utf8_big_list_size == 0xffffff)
|
||||
return (-1);
|
||||
if (utf8_big_list_size == 0)
|
||||
utf8_big_list_size = 256;
|
||||
else if (utf8_big_list_size > 0x7fffff)
|
||||
utf8_big_list_size = 0xffffff;
|
||||
else
|
||||
utf8_big_list_size *= 2;
|
||||
utf8_big_list = xreallocarray(utf8_big_list, utf8_big_list_size,
|
||||
sizeof *utf8_big_list);
|
||||
}
|
||||
*index = utf8_big_list_used++;
|
||||
|
||||
bi = &utf8_big_list[*index];
|
||||
bi->index = *index;
|
||||
memcpy(bi->data, data, size);
|
||||
bi->size = size;
|
||||
RB_INSERT(utf8_big_tree, &utf8_big_tree, bi);
|
||||
|
||||
log_debug("%s: added %.*s at %u", __func__, (int)size, data, *index);
|
||||
if (utf8_list_size == 0xffffff)
|
||||
return (-1);
|
||||
if (utf8_list_size == 0)
|
||||
utf8_list_size = 256;
|
||||
else if (utf8_list_size > 0x7fffff)
|
||||
utf8_list_size = 0xffffff;
|
||||
else
|
||||
utf8_list_size *= 2;
|
||||
utf8_list = xreallocarray(utf8_list, utf8_list_size, sizeof *utf8_list);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Get UTF-8 as index into buffer. */
|
||||
u_int
|
||||
utf8_map_big(const struct utf8_data *ud)
|
||||
/* Add a UTF-8 item. */
|
||||
static int
|
||||
utf8_put_item(const char *data, size_t size, u_int *offset)
|
||||
{
|
||||
union utf8_big_map m = { .value = 0 };
|
||||
u_int o;
|
||||
const char *data = ud->data;
|
||||
size_t size = ud->size;
|
||||
struct utf8_item *ui;
|
||||
|
||||
if (ud->width != 1 && ud->width != 2)
|
||||
return (utf8_big_space1.value);
|
||||
|
||||
if (size > UTF8_BIG_SIZE)
|
||||
goto fail;
|
||||
if (size == 1)
|
||||
return (utf8_set_big(data[0], 1));
|
||||
|
||||
m.flags = size;
|
||||
if (ud->width == 2)
|
||||
m.flags |= UTF8_BIG_WIDTH2;
|
||||
|
||||
if (size <= 3) {
|
||||
memcpy(&m.data, data, size);
|
||||
return (m.value);
|
||||
ui = utf8_get_item(data, size);
|
||||
if (ui != NULL) {
|
||||
*offset = ui->offset;
|
||||
log_debug("%s: have %.*s at %u", __func__, (int)size, data,
|
||||
*offset);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (utf8_put_big_item(data, size, &o) != 0)
|
||||
if (utf8_list_used == utf8_list_size && utf8_expand_list() != 0)
|
||||
return (-1);
|
||||
*offset = utf8_list_used++;
|
||||
|
||||
ui = &utf8_list[*offset];
|
||||
ui->offset = *offset;
|
||||
memcpy(ui->data, data, size);
|
||||
ui->size = size;
|
||||
RB_INSERT(utf8_tree, &utf8_tree, ui);
|
||||
|
||||
log_debug("%s: added %.*s at %u", __func__, (int)size, data, *offset);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Get UTF-8 character from data. */
|
||||
enum utf8_state
|
||||
utf8_from_data(const struct utf8_data *ud, utf8_char *uc)
|
||||
{
|
||||
union utf8_map m = { .uc = 0 };
|
||||
u_int offset;
|
||||
|
||||
if (ud->width != 1 && ud->width != 2)
|
||||
return (utf8_space1.uc);
|
||||
|
||||
if (ud->size > UTF8_FLAG_SIZE)
|
||||
goto fail;
|
||||
m.data[0] = (o & 0xff);
|
||||
m.data[1] = (o >> 8) & 0xff;
|
||||
m.data[2] = (o >> 16);
|
||||
return (m.value);
|
||||
if (ud->size == 1)
|
||||
return (utf8_build_one(ud->data[0], 1));
|
||||
|
||||
m.flags = ud->size;
|
||||
if (ud->width == 2)
|
||||
m.flags |= UTF8_FLAG_WIDTH2;
|
||||
|
||||
if (ud->size <= 3)
|
||||
memcpy(m.data, ud->data, ud->size);
|
||||
else {
|
||||
if (utf8_put_item(ud->data, ud->size, &offset) != 0)
|
||||
goto fail;
|
||||
m.data[0] = (offset & 0xff);
|
||||
m.data[1] = (offset >> 8) & 0xff;
|
||||
m.data[2] = (offset >> 16);
|
||||
}
|
||||
*uc = m.uc;
|
||||
return (UTF8_DONE);
|
||||
|
||||
fail:
|
||||
if (ud->width == 1)
|
||||
return (utf8_big_space1.value);
|
||||
return (utf8_big_space2.value);
|
||||
*uc = utf8_space1.uc;
|
||||
else
|
||||
*uc = utf8_space2.uc;
|
||||
return (UTF8_ERROR);
|
||||
}
|
||||
|
||||
/* Get UTF-8 from index into buffer. */
|
||||
/* Get UTF-8 data from character. */
|
||||
void
|
||||
utf8_get_big(u_int v, struct utf8_data *ud)
|
||||
utf8_to_data(utf8_char uc, struct utf8_data *ud)
|
||||
{
|
||||
union utf8_big_map m = { .value = v };
|
||||
struct utf8_big_item *bi;
|
||||
u_int o;
|
||||
union utf8_map m = { .uc = uc };
|
||||
struct utf8_item *ui;
|
||||
u_int offset;
|
||||
|
||||
memset(ud, 0, sizeof *ud);
|
||||
ud->size = ud->have = (m.flags & UTF8_BIG_SIZE);
|
||||
if (m.flags & UTF8_BIG_WIDTH2)
|
||||
ud->size = ud->have = (m.flags & UTF8_FLAG_SIZE);
|
||||
if (m.flags & UTF8_FLAG_WIDTH2)
|
||||
ud->width = 2;
|
||||
else
|
||||
ud->width = 1;
|
||||
@ -182,24 +186,24 @@ utf8_get_big(u_int v, struct utf8_data *ud)
|
||||
return;
|
||||
}
|
||||
|
||||
o = ((u_int)m.data[2] << 16)|((u_int)m.data[1] << 8)|m.data[0];
|
||||
if (o >= utf8_big_list_used)
|
||||
offset = ((u_int)m.data[2] << 16)|((u_int)m.data[1] << 8)|m.data[0];
|
||||
if (offset >= utf8_list_used)
|
||||
memset(ud->data, ' ', ud->size);
|
||||
else {
|
||||
bi = &utf8_big_list[o];
|
||||
memcpy(ud->data, bi->data, ud->size);
|
||||
ui = &utf8_list[offset];
|
||||
memcpy(ud->data, ui->data, ud->size);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get big value for UTF-8 single character. */
|
||||
/* Get UTF-8 character from a single ASCII character. */
|
||||
u_int
|
||||
utf8_set_big(char c, u_int width)
|
||||
utf8_build_one(char c, u_int width)
|
||||
{
|
||||
union utf8_big_map m = { .flags = 1, .data[0] = c };
|
||||
union utf8_map m = { .flags = 1, .data[0] = c };
|
||||
|
||||
if (width == 2)
|
||||
m.flags |= UTF8_BIG_WIDTH2;
|
||||
return (m.value);
|
||||
m.flags |= UTF8_FLAG_WIDTH2;
|
||||
return (m.uc);
|
||||
}
|
||||
|
||||
/* Set a single character. */
|
||||
@ -224,6 +228,30 @@ utf8_copy(struct utf8_data *to, const struct utf8_data *from)
|
||||
to->data[i] = '\0';
|
||||
}
|
||||
|
||||
/* Get width of Unicode character. */
|
||||
static enum utf8_state
|
||||
utf8_width(struct utf8_data *ud, int *width)
|
||||
{
|
||||
wchar_t wc;
|
||||
|
||||
switch (mbtowc(&wc, ud->data, ud->size)) {
|
||||
case -1:
|
||||
log_debug("UTF-8 %.*s, mbtowc() %d", (int)ud->size, ud->data,
|
||||
errno);
|
||||
mbtowc(NULL, NULL, MB_CUR_MAX);
|
||||
return (UTF8_ERROR);
|
||||
case 0:
|
||||
return (UTF8_ERROR);
|
||||
}
|
||||
*width = wcwidth(wc);
|
||||
if (*width < 0 || *width > 0xff) {
|
||||
log_debug("UTF-8 %.*s, wcwidth() %d", (int)ud->size, ud->data,
|
||||
*width);
|
||||
return (UTF8_ERROR);
|
||||
}
|
||||
return (UTF8_DONE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Open UTF-8 sequence.
|
||||
*
|
||||
@ -251,7 +279,6 @@ utf8_open(struct utf8_data *ud, u_char ch)
|
||||
enum utf8_state
|
||||
utf8_append(struct utf8_data *ud, u_char ch)
|
||||
{
|
||||
wchar_t wc;
|
||||
int width;
|
||||
|
||||
if (ud->have >= ud->size)
|
||||
@ -268,10 +295,7 @@ utf8_append(struct utf8_data *ud, u_char ch)
|
||||
|
||||
if (ud->width == 0xff)
|
||||
return (UTF8_ERROR);
|
||||
|
||||
if (utf8_combine(ud, &wc) != UTF8_DONE)
|
||||
return (UTF8_ERROR);
|
||||
if ((width = utf8_width(wc)) < 0)
|
||||
if (utf8_width(ud, &width) != UTF8_DONE)
|
||||
return (UTF8_ERROR);
|
||||
ud->width = width;
|
||||
|
||||
@ -310,50 +334,6 @@ utf8_width(wchar_t wc)
|
||||
return (width);
|
||||
}
|
||||
|
||||
/* Combine UTF-8 into Unicode. */
|
||||
enum utf8_state
|
||||
utf8_combine(const struct utf8_data *ud, wchar_t *wc)
|
||||
{
|
||||
#ifdef HAVE_UTF8PROC
|
||||
switch (utf8proc_mbtowc(wc, ud->data, ud->size)) {
|
||||
#else
|
||||
switch (mbtowc(wc, ud->data, ud->size)) {
|
||||
#endif
|
||||
case -1:
|
||||
log_debug("UTF-8 %.*s, mbtowc() %d", (int)ud->size, ud->data,
|
||||
errno);
|
||||
mbtowc(NULL, NULL, MB_CUR_MAX);
|
||||
return (UTF8_ERROR);
|
||||
case 0:
|
||||
return (UTF8_ERROR);
|
||||
default:
|
||||
return (UTF8_DONE);
|
||||
}
|
||||
}
|
||||
|
||||
/* Split Unicode into UTF-8. */
|
||||
enum utf8_state
|
||||
utf8_split(wchar_t wc, struct utf8_data *ud)
|
||||
{
|
||||
char s[MB_LEN_MAX];
|
||||
int slen;
|
||||
|
||||
#ifdef HAVE_UTF8PROC
|
||||
slen = utf8proc_wctomb(s, wc);
|
||||
#else
|
||||
slen = wctomb(s, wc);
|
||||
#endif
|
||||
if (slen <= 0 || slen > (int)sizeof ud->data)
|
||||
return (UTF8_ERROR);
|
||||
|
||||
memcpy(ud->data, s, slen);
|
||||
ud->size = slen;
|
||||
|
||||
ud->width = utf8_width(wc);
|
||||
return (UTF8_DONE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Encode len characters from src into dst, which is guaranteed to have four
|
||||
* bytes available for each character from src (for \abc or UTF-8) plus space
|
||||
* for \0.
|
||||
@ -362,13 +342,10 @@ int
|
||||
utf8_strvis(char *dst, const char *src, size_t len, int flag)
|
||||
{
|
||||
struct utf8_data ud;
|
||||
const char *start, *end;
|
||||
const char *start = dst, *end = src + len;
|
||||
enum utf8_state more;
|
||||
size_t i;
|
||||
|
||||
start = dst;
|
||||
end = src + len;
|
||||
|
||||
while (src < end) {
|
||||
if ((more = utf8_open(&ud, *src)) == UTF8_MORE) {
|
||||
while (++src < end && more == UTF8_MORE)
|
||||
@ -394,7 +371,6 @@ utf8_strvis(char *dst, const char *src, size_t len, int flag)
|
||||
dst = vis(dst, src[0], flag, '\0');
|
||||
src++;
|
||||
}
|
||||
|
||||
*dst = '\0';
|
||||
return (dst - start);
|
||||
}
|
||||
@ -417,9 +393,9 @@ utf8_stravis(char **dst, const char *src, int flag)
|
||||
int
|
||||
utf8_isvalid(const char *s)
|
||||
{
|
||||
struct utf8_data ud;
|
||||
const char *end;
|
||||
enum utf8_state more;
|
||||
struct utf8_data ud;
|
||||
const char *end;
|
||||
enum utf8_state more;
|
||||
|
||||
end = s + strlen(s);
|
||||
while (s < end) {
|
||||
@ -445,15 +421,12 @@ utf8_isvalid(const char *s)
|
||||
char *
|
||||
utf8_sanitize(const char *src)
|
||||
{
|
||||
char *dst;
|
||||
size_t n;
|
||||
enum utf8_state more;
|
||||
struct utf8_data ud;
|
||||
u_int i;
|
||||
char *dst = NULL;
|
||||
size_t n = 0;
|
||||
enum utf8_state more;
|
||||
struct utf8_data ud;
|
||||
u_int i;
|
||||
|
||||
dst = NULL;
|
||||
|
||||
n = 0;
|
||||
while (*src != '\0') {
|
||||
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
||||
if ((more = utf8_open(&ud, *src)) == UTF8_MORE) {
|
||||
@ -474,7 +447,6 @@ utf8_sanitize(const char *src)
|
||||
dst[n++] = '_';
|
||||
src++;
|
||||
}
|
||||
|
||||
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
||||
dst[n] = '\0';
|
||||
return (dst);
|
||||
@ -496,9 +468,8 @@ u_int
|
||||
utf8_strwidth(const struct utf8_data *s, ssize_t n)
|
||||
{
|
||||
ssize_t i;
|
||||
u_int width;
|
||||
u_int width = 0;
|
||||
|
||||
width = 0;
|
||||
for (i = 0; s[i].size != 0; i++) {
|
||||
if (n != -1 && n == i)
|
||||
break;
|
||||
@ -514,13 +485,10 @@ utf8_strwidth(const struct utf8_data *s, ssize_t n)
|
||||
struct utf8_data *
|
||||
utf8_fromcstr(const char *src)
|
||||
{
|
||||
struct utf8_data *dst;
|
||||
size_t n;
|
||||
struct utf8_data *dst = NULL;
|
||||
size_t n = 0;
|
||||
enum utf8_state more;
|
||||
|
||||
dst = NULL;
|
||||
|
||||
n = 0;
|
||||
while (*src != '\0') {
|
||||
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
||||
if ((more = utf8_open(&dst[n], *src)) == UTF8_MORE) {
|
||||
@ -536,7 +504,6 @@ utf8_fromcstr(const char *src)
|
||||
n++;
|
||||
src++;
|
||||
}
|
||||
|
||||
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
||||
dst[n].size = 0;
|
||||
return (dst);
|
||||
@ -546,18 +513,14 @@ utf8_fromcstr(const char *src)
|
||||
char *
|
||||
utf8_tocstr(struct utf8_data *src)
|
||||
{
|
||||
char *dst;
|
||||
size_t n;
|
||||
char *dst = NULL;
|
||||
size_t n = 0;
|
||||
|
||||
dst = NULL;
|
||||
|
||||
n = 0;
|
||||
for(; src->size != 0; src++) {
|
||||
dst = xreallocarray(dst, n + src->size, 1);
|
||||
memcpy(dst + n, src->data, src->size);
|
||||
n += src->size;
|
||||
}
|
||||
|
||||
dst = xreallocarray(dst, n + 1, 1);
|
||||
dst[n] = '\0';
|
||||
return (dst);
|
||||
@ -595,7 +558,7 @@ utf8_padcstr(const char *s, u_int width)
|
||||
{
|
||||
size_t slen;
|
||||
char *out;
|
||||
u_int n, i;
|
||||
u_int n, i;
|
||||
|
||||
n = utf8_cstrwidth(s);
|
||||
if (n >= width)
|
||||
@ -616,7 +579,7 @@ utf8_rpadcstr(const char *s, u_int width)
|
||||
{
|
||||
size_t slen;
|
||||
char *out;
|
||||
u_int n, i;
|
||||
u_int n, i;
|
||||
|
||||
n = utf8_cstrwidth(s);
|
||||
if (n >= width)
|
||||
|
Reference in New Issue
Block a user