diff options
Diffstat (limited to 'include/term/unicode/utf8.h')
| -rw-r--r-- | include/term/unicode/utf8.h | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/include/term/unicode/utf8.h b/include/term/unicode/utf8.h index 1c3cc3f..ad44465 100644 --- a/include/term/unicode/utf8.h +++ b/include/term/unicode/utf8.h @@ -1,19 +1,19 @@ -#ifndef UTF_8_H -#define UTF_8_H +#ifndef TERM_UNICODE_UTF8_H +#define TERM_UNICODE_UTF8_H #include <stdint.h> #include <stddef.h> #include <stdlib.h> #include <string.h> -#define UTF8_ACCEPT 0 -#define UTF8_REJECT 1 +#define TERM_UTF8_ACCEPT 0 +#define TERM_UTF8_REJECT 1 -#define UTF8_3BYTE_MIN 0x800u -#define UTF8_SURROGATE_LO 0xd800u -#define UTF8_SURROGATE_HI 0xdfffu +#define TERM_UTF8_3BYTE_MIN 0x800u +#define TERM_UTF8_SURROGATE_LO 0xd800u +#define TERM_UTF8_SURROGATE_HI 0xdfffu -static const uint8_t utf8d[] = { +static const uint8_t term_utf8d[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, @@ -30,19 +30,19 @@ static const uint8_t utf8d[] = { 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, }; -static inline uint32_t utf8_decode(uint32_t *state, uint32_t *codep, uint32_t byte) { +static inline uint32_t term_utf8_decode(uint32_t *state, uint32_t *codep, uint32_t byte) { byte &= 0xff; - uint32_t type = utf8d[byte]; + uint32_t type = term_utf8d[byte]; - *codep = (*state != UTF8_ACCEPT) ? + *codep = (*state != TERM_UTF8_ACCEPT) ? (byte & 0x3fu) | (*codep << 6) : (0xff >> type) & (byte); - *state = utf8d[256 + *state*16 + type]; + *state = term_utf8d[256 + *state*16 + type]; return *state; } -static const uint8_t utf8_acc_state[256] = { +static const uint8_t term_utf8_acc_state[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -60,7 +60,7 @@ static const uint8_t utf8_acc_state[256] = { 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 6, 7, 7, 7, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }; -static const uint8_t utf8_acc_codep[256] = { +static const uint8_t term_utf8_acc_codep[256] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, @@ -86,7 +86,7 @@ static const uint8_t utf8_acc_codep[256] = { * ends an unfinished sequence the same way. An unfinished sequence at the * end of buf gives *cp = 0 and keeps the state. */ -static inline int utf8_decode_ground(uint32_t *state, uint32_t *codep, +static inline int term_utf8_decode_ground(uint32_t *state, uint32_t *codep, const char *buf, size_t len, uint32_t *cp) { uint32_t st = *state; uint32_t cpv = *codep; @@ -97,25 +97,25 @@ static inline int utf8_decode_ground(uint32_t *state, uint32_t *codep, do { unsigned char c = (unsigned char)buf[n]; if (c < 0x20) { - if (st != UTF8_ACCEPT) *cp = 0xFFFD; - st = UTF8_ACCEPT; + if (st != TERM_UTF8_ACCEPT) *cp = 0xFFFD; + st = TERM_UTF8_ACCEPT; break; } uint32_t before = st; n++; - if (st == UTF8_ACCEPT) { - cpv = utf8_acc_codep[c]; - st = utf8_acc_state[c]; + if (st == TERM_UTF8_ACCEPT) { + cpv = term_utf8_acc_codep[c]; + st = term_utf8_acc_state[c]; } else { - uint32_t type = utf8d[c]; + uint32_t type = term_utf8d[c]; cpv = (c & 0x3fu) | (cpv << 6); - st = utf8d[256 + st*16 + type]; + st = term_utf8d[256 + st*16 + type]; } - if (st == UTF8_ACCEPT) { *cp = cpv; break; } - if (st == UTF8_REJECT) { - st = UTF8_ACCEPT; + if (st == TERM_UTF8_ACCEPT) { *cp = cpv; break; } + if (st == TERM_UTF8_REJECT) { + st = TERM_UTF8_ACCEPT; *cp = 0xFFFD; - if (before != UTF8_ACCEPT) n--; + if (before != TERM_UTF8_ACCEPT) n--; break; } } while ((size_t)n < len); @@ -125,7 +125,7 @@ static inline int utf8_decode_ground(uint32_t *state, uint32_t *codep, } static inline int -utf8_decode_one(const char *buf, size_t len, uint32_t *codep) +term_utf8_decode_one(const char *buf, size_t len, uint32_t *codep) { const unsigned char *s = (const unsigned char *)buf; unsigned c0, c1, c2, c3; @@ -159,8 +159,8 @@ utf8_decode_one(const char *buf, size_t len, uint32_t *codep) if ((c1 & 0xc0) != 0x80 || (c2 & 0xc0) != 0x80) return 0; cp = ((c0 & 0x0f) << 12) | ((c1 & 0x3f) << 6) | (c2 & 0x3f); - if (cp < UTF8_3BYTE_MIN || - (cp >= UTF8_SURROGATE_LO && cp <= UTF8_SURROGATE_HI)) + if (cp < TERM_UTF8_3BYTE_MIN || + (cp >= TERM_UTF8_SURROGATE_LO && cp <= TERM_UTF8_SURROGATE_HI)) return 0; *codep = cp; return 3; @@ -183,14 +183,14 @@ utf8_decode_one(const char *buf, size_t len, uint32_t *codep) } static inline void -utf8_sanitize(char *dst, size_t cap, const char *src) +term_utf8_sanitize(char *dst, size_t cap, const char *src) { if (!cap) return; size_t i = 0, len = strlen(src), off = 0; while (off < len && i + 1 < cap) { uint32_t cp; - int n = utf8_decode_one(src + off, len - off, &cp); + int n = term_utf8_decode_one(src + off, len - off, &cp); if (n <= 0 || i + (size_t)n >= cap) { off++; continue; @@ -204,17 +204,17 @@ utf8_sanitize(char *dst, size_t cap, const char *src) /* A sanitized copy of src. Sanitizing only drops bytes, so the copy is * never longer than src. */ -static inline char *utf8_sanitize_dup(const char *src) +static inline char *term_utf8_sanitize_dup(const char *src) { size_t cap = strlen(src) + 1; - char *dst = malloc(cap); + char *dst = (char *)malloc(cap); if (dst) - utf8_sanitize(dst, cap, src); + term_utf8_sanitize(dst, cap, src); return dst; } -#define UTF8_MAX_BYTES 4 -static inline int utf8_encode(uint32_t cp, char *out) +#define TERM_UTF8_MAX_BYTES 4 +static inline int term_utf8_encode(uint32_t cp, char *out) { if (cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff)) cp = 0xfffd; |
