]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - utf8.cpp
Fix #158
[xonotic/gmqcc.git] / utf8.cpp
1 #include "gmqcc.h"
2
3 /*
4  * Based on the flexible and economical utf8 decoder:
5  * http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
6  *
7  * This is slightly more economical, the fastest way to decode utf8 is
8  * with a lookup table as in:
9  *
10  * first 1-byte lookup
11  * if that fails, 2-byte lookup
12  * if that fails, 3-byte lookup
13  * if that fails, 4-byte lookup
14  *
15  * The following table can be generated with some interval trickery.
16  * consider an interval [a, b):
17  *
18  *      a must be 0x80 or b must be 0xc0, lower 3 bits
19  *      are clear, thus:
20  *          interval(a,b) = ((uint32_t)((a==0x80?0x40-b:-a)<<23))
21  *
22  * The failstate can be represented as interval(0x80,0x80), it's
23  * odd to see but this is a full state machine.
24  *
25  * The table than maps the corresponding sections as a serise of
26  * intervals.
27  *
28  * In this table the transition values are pre-multiplied with 16 to
29  * save a shift instruction for every byte, we throw away fillers
30  * which makes the table smaller.
31  *
32  * The first section of the table handles bytes with leading C
33  * The second section of the table handles bytes with leading D
34  * The third section of the table handles bytes with leading E
35  * The last section of the table handles bytes with leading F
36  *
37  * The values themselfs in the table are arranged so that when you
38  * left shift them by 6 to shift continuation characters into place, the
39  * new top bits tell you:
40  *
41  *  1 - if you keep going
42  *  2 - the range of valid values for the next byte
43  */
44 static const uint32_t utf8_tab[] = {
45     0xC0000002, 0xC0000003, 0xC0000004, 0xC0000005, 0xC0000006,
46     0xC0000007, 0xC0000008, 0xC0000009, 0xC000000A, 0xC000000B,
47     0xC000000C, 0xC000000D, 0xC000000E, 0xC000000F, 0xC0000010,
48     0xC0000011, 0xC0000012, 0xC0000013, 0xC0000014, 0xC0000015,
49     0xC0000016, 0xC0000017, 0xC0000018, 0xC0000019, 0xC000001A,
50     0xC000001B, 0xC000001C, 0xC000001D, 0xC000001E, 0xC000001F,
51     0xB3000000, 0xC3000001, 0xC3000002, 0xC3000003, 0xC3000004,
52     0xC3000005, 0xC3000006, 0xC3000007, 0xC3000008, 0xC3000009,
53     0xC300000A, 0xC300000B, 0xC300000C, 0xD300000D, 0xC300000E,
54     0xC300000F, 0xBB0C0000, 0xC30C0001, 0xC30C0002, 0xC30C0003,
55     0xD30C0004
56 };
57
58 int utf8_from(char *s, utf8ch_t ch) {
59     if (!s)
60         return 0;
61
62     if ((unsigned)ch < 0x80) {
63         *s = ch;
64         return 1;
65     } else if ((unsigned)ch < 0x800) {
66         *s++ = 0xC0 | (ch >> 6);
67         *s   = 0x80 | (ch & 0x3F);
68         return 2;
69     } else if ((unsigned)ch < 0xD800 || (unsigned)ch - 0xE000 < 0x2000) {
70         *s++ = 0xE0 | (ch >> 12);
71         *s++ = 0x80 | ((ch >> 6) & 0x3F);
72         *s   = 0x80 | (ch & 0x3F);
73         return 3;
74     } else if ((unsigned)ch - 0x10000 < 0x100000) {
75         *s++ = 0xF0 | (ch >> 18);
76         *s++ = 0x80 | ((ch >> 12) & 0x3F);
77         *s++ = 0x80 | ((ch >> 6) & 0x3F);
78         *s   = 0x80 | (ch & 0x3F);
79         return 4;
80     }
81     return 0;
82 }
83
84 int utf8_to(utf8ch_t *i, const unsigned char *s, size_t n) {
85     unsigned c,j;
86
87     if (!s || !n)
88         return 0;
89
90     /* This is consistent with mbtowc behaviour. */
91     if (!i)
92         i = (utf8ch_t*)(void*)&i;
93
94     if (*s < 0x80)
95         return !!(*i = *s);
96     if (*s-0xC2U > 0x32)
97         return 0;
98
99     c = utf8_tab[*s++-0xC2U];
100
101     /*
102      * Avoid excessive checks against n.
103      *
104      * When shifting state `n-1` times does not clear the high bit,
105      * then the value of `n` won't satisfy the condition to read a
106      * character as it will be insufficent.
107      */
108     if (n < 4 && ((c<<(6*n-6)) & (1U << 31)))
109         return 0;
110
111     /*
112      * The upper 6 state bits are negitive integer offset to a bound-check
113      * next byte equivlant to: ((b-0x80)+(b+offset))&~0x3f
114      */
115     if ((((*s>>3)-0x10)|((*s>>3)+((int32_t)c>>26))) & ~7)
116         return 0;
117
118     for (j=2; j<3; j++) {
119         if (!((c = c<<6 | (*s++-0x80))&(1U<<31))) {
120             *i = c;
121             return j;
122         }
123         if (*s-0x80U >= 0x40)
124             return 0;
125     }
126
127     *i = c<<6 | (*s++-0x80);
128     return 4;
129 }