=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/tmux/utf8.c,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- src/usr.bin/tmux/utf8.c 2015/11/14 11:13:44 1.22 +++ src/usr.bin/tmux/utf8.c 2015/11/14 11:45:43 1.23 @@ -1,4 +1,4 @@ -/* $OpenBSD: utf8.c,v 1.22 2015/11/14 11:13:44 nicm Exp $ */ +/* $OpenBSD: utf8.c,v 1.23 2015/11/14 11:45:43 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -381,10 +381,8 @@ * 11000010-11011111 C2-DF start of 2-byte sequence * 11100000-11101111 E0-EF start of 3-byte sequence * 11110000-11110100 F0-F4 start of 4-byte sequence - * - * Returns 1 if more UTF-8 to come, 0 if not UTF-8. */ -int +enum utf8_state utf8_open(struct utf8_data *ud, u_char ch) { memset(ud, 0, sizeof *ud); @@ -395,18 +393,13 @@ else if (ch >= 0xf0 && ch <= 0xf4) ud->size = 4; else - return (0); + return (UTF8_ERROR); utf8_append(ud, ch); - return (1); + return (UTF8_MORE); } -/* - * Append character to UTF-8, closing if finished. - * - * Returns 1 if more UTF-8 data to come, 0 if finished and valid, -1 if - * finished and invalid. - */ -int +/* Append character to UTF-8, closing if finished. */ +enum utf8_state utf8_append(struct utf8_data *ud, u_char ch) { if (ud->have >= ud->size) @@ -419,12 +412,12 @@ ud->data[ud->have++] = ch; if (ud->have != ud->size) - return (1); + return (UTF8_MORE); if (ud->width == 0xff) - return (-1); + return (UTF8_ERROR); ud->width = utf8_width(utf8_combine(ud)); - return (0); + return (UTF8_DONE); } /* Build UTF-8 width tree. */ @@ -501,7 +494,7 @@ } /* Split 32-bit Unicode into UTF-8. */ -int +enum utf8_state utf8_split(u_int uc, struct utf8_data *ud) { if (uc < 0x7f) { @@ -523,9 +516,9 @@ ud->data[2] = 0x80 | ((uc >> 6) & 0x3f); ud->data[3] = 0x80 | (uc & 0x3f); } else - return (-1); + return (UTF8_ERROR); ud->width = utf8_width(uc); - return (0); + return (UTF8_DONE); } /* Split a two-byte UTF-8 character. */ @@ -551,26 +544,24 @@ { struct utf8_data ud; const char *start, *end; - int more; + enum utf8_state more; size_t i; start = dst; end = src + len; while (src < end) { - if (utf8_open(&ud, *src)) { - more = 1; - while (++src < end && more == 1) + if ((more = utf8_open(&ud, *src)) == UTF8_MORE) { + while (++src < end && more == UTF8_MORE) more = utf8_append(&ud, *src); - if (more == 0) { + if (more == UTF8_DONE) { /* UTF-8 character finished. */ for (i = 0; i < ud.size; i++) *dst++ = ud.data[i]; continue; - } else if (ud.have > 0) { - /* Not a complete, valid UTF-8 character. */ - src -= ud.have; } + /* Not a complete, valid UTF-8 character. */ + src -= ud.have; } if (src < end - 1) dst = vis(dst, src[0], flag, src[1]); @@ -593,7 +584,7 @@ { char *dst; size_t n; - int more; + enum utf8_state more; struct utf8_data ud; u_int i; @@ -602,11 +593,10 @@ n = 0; while (*src != '\0') { dst = xreallocarray(dst, n + 1, sizeof *dst); - if (utf8_open(&ud, *src)) { - more = 1; - while (*++src != '\0' && more == 1) + if ((more = utf8_open(&ud, *src)) == UTF8_MORE) { + while (*++src != '\0' && more == UTF8_MORE) more = utf8_append(&ud, *src); - if (more != 1) { + if (more == UTF8_DONE) { dst = xreallocarray(dst, n + ud.width, sizeof *dst); for (i = 0; i < ud.width; i++) @@ -617,6 +607,8 @@ } if (*src > 0x1f && *src < 0x7f) dst[n++] = *src; + else + dst[n++] = '_'; src++; } @@ -634,27 +626,24 @@ { struct utf8_data *dst; size_t n; - int more; + enum utf8_state more; dst = NULL; n = 0; while (*src != '\0') { dst = xreallocarray(dst, n + 1, sizeof *dst); - if (utf8_open(&dst[n], *src)) { - more = 1; - while (*++src != '\0' && more == 1) + if ((more = utf8_open(&dst[n], *src)) == UTF8_MORE) { + while (*++src != '\0' && more == UTF8_MORE) more = utf8_append(&dst[n], *src); - if (more != 1) { + if (more == UTF8_DONE) { n++; continue; } src -= dst[n].have; } - if (*src > 0x1f && *src < 0x7f) { - utf8_set(&dst[n], *src); - n++; - } + utf8_set(&dst[n], *src); + n++; src++; } @@ -690,21 +679,20 @@ { struct utf8_data tmp; u_int width; - int more; + enum utf8_state more; width = 0; while (*s != '\0') { - if (utf8_open(&tmp, *s)) { - more = 1; - while (*++s != '\0' && more == 1) + if ((more = utf8_open(&tmp, *s)) == UTF8_MORE) { + while (*++s != '\0' && more == UTF8_MORE) more = utf8_append(&tmp, *s); - if (more != 1) { + if (more == UTF8_DONE) { width += tmp.width; continue; } s -= tmp.have; } - if (*s > 0x1f && *s < 0x7f) + if (*s > 0x1f && *s != 0x7f) width++; s++; }