-
Notifications
You must be signed in to change notification settings - Fork 10
/
ldbcs.h
278 lines (247 loc) · 7.22 KB
/
ldbcs.h
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#ifndef ldbcs_h
#define ldbcs_h
#include <lua.h>
#include <lauxlib.h>
#include <stddef.h>
#define UTF_MAX 8
#define DBCS_MAX 2
static unsigned from_utf8(unsigned uni_code) {
const unsigned short *page = from_uni[(uni_code >> 8) & 0xFF];
return page == NULL ? DBCS_DEFAULT_CODE : page[uni_code & 0xFF];
}
static unsigned to_utf8(unsigned cp_code) {
const unsigned short *page = to_uni[(cp_code >> 8) & 0xFF];
return page == NULL ? UNI_INVALID_CODE : page[cp_code & 0xFF];
}
static size_t utf8_encode(char *s, unsigned ch) {
if (ch < 0x80) {
s[0] = (char)ch;
return 1;
}
if (ch <= 0x7FF) {
s[1] = (char) ((ch | 0x80) & 0xBF);
s[0] = (char) ((ch >> 6) | 0xC0);
return 2;
}
if (ch <= 0xFFFF) {
three:
s[2] = (char) ((ch | 0x80) & 0xBF);
s[1] = (char) (((ch >> 6) | 0x80) & 0xBF);
s[0] = (char) ((ch >> 12) | 0xE0);
return 3;
}
if (ch <= 0x1FFFFF) {
s[3] = (char) ((ch | 0x80) & 0xBF);
s[2] = (char) (((ch >> 6) | 0x80) & 0xBF);
s[1] = (char) (((ch >> 12) | 0x80) & 0xBF);
s[0] = (char) ((ch >> 18) | 0xF0);
return 4;
}
if (ch <= 0x3FFFFFF) {
s[4] = (char) ((ch | 0x80) & 0xBF);
s[3] = (char) (((ch >> 6) | 0x80) & 0xBF);
s[2] = (char) (((ch >> 12) | 0x80) & 0xBF);
s[1] = (char) (((ch >> 18) | 0x80) & 0xBF);
s[0] = (char) ((ch >> 24) | 0xF8);
return 5;
}
if (ch <= 0x7FFFFFFF) {
s[5] = (char) ((ch | 0x80) & 0xBF);
s[4] = (char) (((ch >> 6) | 0x80) & 0xBF);
s[3] = (char) (((ch >> 12) | 0x80) & 0xBF);
s[2] = (char) (((ch >> 18) | 0x80) & 0xBF);
s[1] = (char) (((ch >> 24) | 0x80) & 0xBF);
s[0] = (char) ((ch >> 30) | 0xFC);
return 6;
}
/* fallback */
ch = 0xFFFD;
goto three;
}
static size_t utf8_decode(const char *s, const char *e, unsigned *pch) {
unsigned ch;
if (s >= e) {
*pch = 0;
return 0;
}
ch = (unsigned char)s[0];
if (ch < 0xC0) goto fallback;
if (ch < 0xE0) {
if (s+1 >= e || (s[1] & 0xC0) != 0x80)
goto fallback;
*pch = ((ch & 0x1F) << 6) | (s[1] & 0x3F);
return 2;
}
if (ch < 0xF0) {
if (s+2 >= e || (s[1] & 0xC0) != 0x80
|| (s[2] & 0xC0) != 0x80)
goto fallback;
*pch = ((ch & 0x0F) << 12) | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F);
return 3;
}
{
int count = 0; /* to count number of continuation bytes */
unsigned res = 0;
while ((ch & 0x40) != 0) { /* still have continuation bytes? */
int cc = (unsigned char)s[++count];
if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
goto fallback; /* invalid byte sequence, fallback */
res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
ch <<= 1; /* to test next bit */
}
if (count > 5)
goto fallback; /* invalid byte sequence */
res |= ((ch & 0x7F) << (count * 5)); /* add first byte */
*pch = res;
return count+1;
}
fallback:
*pch = ch;
return 1;
}
static void add_utf8char(luaL_Buffer *b, unsigned ch) {
char buff[UTF_MAX];
size_t n = utf8_encode(buff, ch);
luaL_addlstring(b, buff, n);
}
static size_t dbcs_decode(const char *s, const char *e, unsigned *pch) {
unsigned ch;
if (s >= e) {
*pch = 0;
return 0;
}
ch = s[0] & 0xFF;
if (to_uni_00[ch] != UNI_INVALID_CODE) {
*pch = ch;
return 1;
}
*pch = (ch << 8) | (s[1] & 0xFF);
return 2;
}
static void add_dbcschar(luaL_Buffer *b, unsigned ch) {
if (ch < 0x7F)
luaL_addchar(b, ch);
else {
luaL_addchar(b, (ch >> 8) & 0xFF);
luaL_addchar(b, ch & 0xFF);
}
}
static size_t dbcs_length(const char *s, const char *e) {
size_t dbcslen = 0;
while (s < e) {
if ((unsigned char)(*s++) > 0x7F)
++s;
++dbcslen;
}
return dbcslen;
}
/* dbcs module interface */
static const char *check_dbcs(lua_State *L, int idx, const char **pe) {
size_t len;
const char *s = luaL_checklstring(L, idx, &len);
if (pe != NULL) *pe = s + len;
return s;
}
static int posrelat(int pos, size_t len) {
if (pos >= 0) return (size_t)pos;
else if (0u - (size_t)pos > len) return 0;
else return len - ((size_t)-pos) + 1;
}
static int string_from_utf8(lua_State *L) {
const char *e, *s = check_dbcs(L, 1, &e);
luaL_Buffer b;
luaL_buffinit(L, &b);
while (s < e) {
unsigned ch;
s += utf8_decode(s, e, &ch);
add_dbcschar(&b, from_utf8(ch));
}
luaL_pushresult(&b);
return 1;
}
static int string_to_utf8(lua_State *L) {
const char *e, *s = check_dbcs(L, 1, &e);
luaL_Buffer b;
luaL_buffinit(L, &b);
while (s < e) {
unsigned ch;
s += dbcs_decode(s, e, &ch);
add_utf8char(&b, to_utf8(ch));
}
luaL_pushresult(&b);
return 1;
}
static int Ldbcs_len(lua_State *L) {
const char *e, *s = check_dbcs(L, 1, &e);
lua_pushinteger(L, dbcs_length(s, e));
return 1;
}
static int Ldbcs_byte(lua_State *L) {
const char *e, *s = check_dbcs(L, 1, &e);
size_t len = dbcs_length(s, e);
int posi = posrelat((int)luaL_optinteger(L, 2, 1), len);
int pose = posrelat((int)luaL_optinteger(L, 3, posi), len);
const char *start = s;
int i, n;
if (posi < 1) posi = 1;
if (pose > (int)len) pose = len;
if (pose > pose) return 0;
n = (int)(pose - posi + 1);
if (posi + n <= pose) /* (size_t -> int) overflow? */
return luaL_error(L, "string slice too long");
luaL_checkstack(L, n, "string slice too long");
for (i = 0; i < posi; ++i) {
unsigned ch;
start += dbcs_decode(start, e, &ch);
}
for (i = 0; i < n; ++i) {
unsigned ch;
start += dbcs_decode(start, e, &ch);
lua_pushinteger(L, ch);
}
return n;
}
static int Ldbcs_char(lua_State *L) {
luaL_Buffer b;
int i, top = lua_gettop(L);
luaL_buffinit(L, &b);
for (i = 1; i <= top; ++i)
add_dbcschar(&b, (unsigned)luaL_checkinteger(L, i));
luaL_pushresult(&b);
return 1;
}
static int Ldbcs_fromutf8(lua_State *L) {
int i, top;
switch (lua_type(L, 1)) {
case LUA_TSTRING:
return string_from_utf8(L);
case LUA_TNUMBER:
top = lua_gettop(L);
for (i = 1; i <= top; ++i) {
unsigned code = (unsigned)luaL_checkinteger(L, i);
lua_pushinteger(L, (lua_Integer)from_utf8(code));
lua_replace(L, i);
}
return top;
}
return luaL_error(L, "string/number expected, got %s",
luaL_typename(L, 1));
}
static int Ldbcs_toutf8(lua_State *L) {
int i, top;
switch (lua_type(L, 1)) {
case LUA_TSTRING:
return string_to_utf8(L);
case LUA_TNUMBER:
top = lua_gettop(L);
for (i = 1; i <= top; ++i) {
unsigned code = (unsigned)luaL_checkinteger(L, i);
lua_pushinteger(L, to_utf8(code));
lua_replace(L, i);
}
return top;
}
return luaL_error(L, "string/number expected, got %s",
luaL_typename(L, 1));
}
#endif /* ldbcs_h */