All these return values from utf8_* are confusing, use an enum.

This commit is contained in:
nicm
2015-11-14 11:45:43 +00:00
parent f401791a56
commit 205d15e82d
7 changed files with 70 additions and 77 deletions

80
utf8.c
View File

@ -381,10 +381,8 @@ utf8_copy(struct utf8_data *to, const struct utf8_data *from)
* 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 @@ utf8_open(struct utf8_data *ud, u_char ch)
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 @@ utf8_append(struct utf8_data *ud, u_char ch)
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 @@ utf8_combine(const struct utf8_data *ud)
}
/* 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 @@ utf8_split(u_int uc, struct utf8_data *ud)
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 @@ utf8_strvis(char *dst, const char *src, size_t len, int flag)
{
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 @@ utf8_sanitize(const char *src)
{
char *dst;
size_t n;
int more;
enum utf8_state more;
struct utf8_data ud;
u_int i;
@ -602,11 +593,10 @@ utf8_sanitize(const char *src)
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 @@ utf8_sanitize(const char *src)
}
if (*src > 0x1f && *src < 0x7f)
dst[n++] = *src;
else
dst[n++] = '_';
src++;
}
@ -634,27 +626,24 @@ utf8_fromcstr(const char *src)
{
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 @@ utf8_cstrwidth(const char *s)
{
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++;
}