/src/testdir/build/lua-master/source/lutf8lib.c
Line | Count | Source |
1 | | /* |
2 | | ** $Id: lutf8lib.c $ |
3 | | ** Standard library for UTF-8 manipulation |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lutf8lib_c |
8 | | #define LUA_LIB |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <limits.h> |
14 | | #include <stdlib.h> |
15 | | #include <string.h> |
16 | | |
17 | | #include "lua.h" |
18 | | |
19 | | #include "lauxlib.h" |
20 | | #include "lualib.h" |
21 | | #include "llimits.h" |
22 | | |
23 | | |
24 | 196k | #define MAXUNICODE 0x10FFFFu |
25 | | |
26 | 13.0k | #define MAXUTF 0x7FFFFFFFu |
27 | | |
28 | | |
29 | 94 | #define MSGInvalid "invalid UTF-8 code" |
30 | | |
31 | | |
32 | 17.7k | #define iscont(c) (((c) & 0xC0) == 0x80) |
33 | 531 | #define iscontp(p) iscont(*(p)) |
34 | | |
35 | | |
36 | | /* from strlib */ |
37 | | /* translate a relative string position: negative means back from end */ |
38 | 32.8k | static lua_Integer u_posrelat (lua_Integer pos, size_t len) { |
39 | 32.8k | if (pos >= 0) return pos; |
40 | 10.8k | else if (0u - (size_t)pos > len) return 0; |
41 | 10.5k | else return (lua_Integer)len + pos + 1; |
42 | 32.8k | } |
43 | | |
44 | | |
45 | | /* |
46 | | ** Decode one UTF-8 sequence, returning NULL if byte sequence is |
47 | | ** invalid. The array 'limits' stores the minimum value for each |
48 | | ** sequence length, to check for overlong representations. Its first |
49 | | ** entry forces an error for non-ASCII bytes with no continuation |
50 | | ** bytes (count == 0). |
51 | | */ |
52 | 104k | static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { |
53 | 104k | static const l_uint32 limits[] = |
54 | 104k | {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; |
55 | 104k | unsigned int c = (unsigned char)s[0]; |
56 | 104k | l_uint32 res = 0; /* final result */ |
57 | 104k | if (c < 0x80) /* ASCII? */ |
58 | 94.4k | res = c; |
59 | 9.88k | else { |
60 | 9.88k | int count = 0; /* to count number of continuation bytes */ |
61 | 22.8k | for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ |
62 | 16.2k | unsigned int cc = (unsigned char)s[++count]; /* read next byte */ |
63 | 16.2k | if (!iscont(cc)) /* not a continuation byte? */ |
64 | 3.26k | return NULL; /* invalid byte sequence */ |
65 | 13.0k | res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ |
66 | 13.0k | } |
67 | 6.61k | res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */ |
68 | 6.61k | if (count > 5 || res > MAXUTF || res < limits[count]) |
69 | 1.93k | return NULL; /* invalid byte sequence */ |
70 | 4.68k | s += count; /* skip continuation bytes read */ |
71 | 4.68k | } |
72 | 99.0k | if (strict) { |
73 | | /* check for invalid code points; too large or surrogates */ |
74 | 98.2k | if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) |
75 | 2.39k | return NULL; |
76 | 98.2k | } |
77 | 96.6k | if (val) *val = res; |
78 | 96.6k | return s + 1; /* +1 to include first byte */ |
79 | 99.0k | } |
80 | | |
81 | | |
82 | | /* |
83 | | ** utf8len(s [, i [, j [, lax]]]) --> number of characters that |
84 | | ** start in the range [i,j], or nil + current position if 's' is not |
85 | | ** well formed in that interval |
86 | | */ |
87 | 16.3k | static int utflen (lua_State *L) { |
88 | 16.3k | lua_Integer n = 0; /* counter for the number of characters */ |
89 | 16.3k | size_t len; /* string length in bytes */ |
90 | 16.3k | const char *s = luaL_checklstring(L, 1, &len); |
91 | 16.3k | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); |
92 | 16.3k | lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); |
93 | 16.3k | int lax = lua_toboolean(L, 4); |
94 | 16.3k | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, |
95 | 16.3k | "initial position out of bounds"); |
96 | 16.3k | luaL_argcheck(L, --posj < (lua_Integer)len, 3, |
97 | 16.3k | "final position out of bounds"); |
98 | 112k | while (posi <= posj) { |
99 | 103k | const char *s1 = utf8_decode(s + posi, NULL, !lax); |
100 | 103k | if (s1 == NULL) { /* conversion error? */ |
101 | 7.54k | luaL_pushfail(L); /* return fail ... */ |
102 | 7.54k | lua_pushinteger(L, posi + 1); /* ... and current position */ |
103 | 7.54k | return 2; |
104 | 7.54k | } |
105 | 96.0k | posi = ct_diff2S(s1 - s); |
106 | 96.0k | n++; |
107 | 96.0k | } |
108 | 8.79k | lua_pushinteger(L, n); |
109 | 8.79k | return 1; |
110 | 16.3k | } |
111 | | |
112 | | |
113 | | /* |
114 | | ** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all |
115 | | ** characters that start in the range [i,j] |
116 | | */ |
117 | 98 | static int codepoint (lua_State *L) { |
118 | 98 | size_t len; |
119 | 98 | const char *s = luaL_checklstring(L, 1, &len); |
120 | 98 | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); |
121 | 98 | lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); |
122 | 98 | int lax = lua_toboolean(L, 4); |
123 | 98 | int n; |
124 | 98 | const char *se; |
125 | 98 | luaL_argcheck(L, posi >= 1, 2, "out of bounds"); |
126 | 98 | luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds"); |
127 | 98 | if (posi > pose) return 0; /* empty interval; return no values */ |
128 | 98 | if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ |
129 | 0 | return luaL_error(L, "string slice too long"); |
130 | 98 | n = (int)(pose - posi) + 1; /* upper bound for number of returns */ |
131 | 98 | luaL_checkstack(L, n, "string slice too long"); |
132 | 98 | n = 0; /* count the number of returns */ |
133 | 98 | se = s + pose; /* string end */ |
134 | 193 | for (s += posi - 1; s < se;) { |
135 | 97 | l_uint32 code; |
136 | 97 | s = utf8_decode(s, &code, !lax); |
137 | 97 | if (s == NULL) |
138 | 2 | return luaL_error(L, MSGInvalid); |
139 | 95 | lua_pushinteger(L, l_castU2S(code)); |
140 | 95 | n++; |
141 | 95 | } |
142 | 96 | return n; |
143 | 98 | } |
144 | | |
145 | | |
146 | 1.91k | static void pushutfchar (lua_State *L, int arg) { |
147 | 1.91k | lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg); |
148 | 1.91k | luaL_argcheck(L, code <= MAXUTF, arg, "value out of range"); |
149 | 1.91k | lua_pushfstring(L, "%U", (long)code); |
150 | 1.91k | } |
151 | | |
152 | | |
153 | | /* |
154 | | ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... |
155 | | */ |
156 | 4.75k | static int utfchar (lua_State *L) { |
157 | 4.75k | int n = lua_gettop(L); /* number of arguments */ |
158 | 4.75k | if (n == 1) /* optimize common case of single char */ |
159 | 1.26k | pushutfchar(L, 1); |
160 | 3.48k | else { |
161 | 3.48k | int i; |
162 | 3.48k | luaL_Buffer b; |
163 | 3.48k | luaL_buffinit(L, &b); |
164 | 4.13k | for (i = 1; i <= n; i++) { |
165 | 644 | pushutfchar(L, i); |
166 | 644 | luaL_addvalue(&b); |
167 | 644 | } |
168 | 3.48k | luaL_pushresult(&b); |
169 | 3.48k | } |
170 | 4.75k | return 1; |
171 | 4.75k | } |
172 | | |
173 | | |
174 | | /* |
175 | | ** offset(s, n, [i]) -> indices where n-th character counting from |
176 | | ** position 'i' starts and ends; 0 means character at 'i'. |
177 | | */ |
178 | 11 | static int byteoffset (lua_State *L) { |
179 | 11 | size_t len; |
180 | 11 | const char *s = luaL_checklstring(L, 1, &len); |
181 | 11 | lua_Integer n = luaL_checkinteger(L, 2); |
182 | 11 | lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1; |
183 | 11 | posi = u_posrelat(luaL_optinteger(L, 3, posi), len); |
184 | 11 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, |
185 | 11 | "position out of bounds"); |
186 | 11 | if (n == 0) { |
187 | | /* find beginning of current byte sequence */ |
188 | 0 | while (posi > 0 && iscontp(s + posi)) posi--; |
189 | 0 | } |
190 | 11 | else { |
191 | 11 | if (iscontp(s + posi)) |
192 | 0 | return luaL_error(L, "initial position is a continuation byte"); |
193 | 11 | if (n < 0) { |
194 | 0 | while (n < 0 && posi > 0) { /* move back */ |
195 | 0 | do { /* find beginning of previous character */ |
196 | 0 | posi--; |
197 | 0 | } while (posi > 0 && iscontp(s + posi)); |
198 | 0 | n++; |
199 | 0 | } |
200 | 0 | } |
201 | 11 | else { |
202 | 11 | n--; /* do not move for 1st character */ |
203 | 11 | while (n > 0 && posi < (lua_Integer)len) { |
204 | 0 | do { /* find beginning of next character */ |
205 | 0 | posi++; |
206 | 0 | } while (iscontp(s + posi)); /* (cannot pass final '\0') */ |
207 | 0 | n--; |
208 | 0 | } |
209 | 11 | } |
210 | 11 | } |
211 | 11 | if (n != 0) { /* did not find given character? */ |
212 | 0 | luaL_pushfail(L); |
213 | 0 | return 1; |
214 | 0 | } |
215 | 11 | lua_pushinteger(L, posi + 1); /* initial position */ |
216 | 11 | if ((s[posi] & 0x80) != 0) { /* multi-byte character? */ |
217 | 0 | if (iscont(s[posi])) |
218 | 0 | return luaL_error(L, "initial position is a continuation byte"); |
219 | 0 | while (iscontp(s + posi + 1)) |
220 | 0 | posi++; /* skip to last continuation byte */ |
221 | 0 | } |
222 | | /* else one-byte character: final position is the initial one */ |
223 | 11 | lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */ |
224 | 11 | return 2; |
225 | 11 | } |
226 | | |
227 | | |
228 | 1.71k | static int iter_aux (lua_State *L, int strict) { |
229 | 1.71k | size_t len; |
230 | 1.71k | const char *s = luaL_checklstring(L, 1, &len); |
231 | 1.71k | lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); |
232 | 1.71k | if (n < len) { |
233 | 624 | while (iscontp(s + n)) n++; /* go to next character */ |
234 | 624 | } |
235 | 1.71k | if (n >= len) /* (also handles original 'n' being negative) */ |
236 | 1.12k | return 0; /* no more codepoints */ |
237 | 585 | else { |
238 | 585 | l_uint32 code; |
239 | 585 | const char *next = utf8_decode(s + n, &code, strict); |
240 | 585 | if (next == NULL || iscontp(next)) |
241 | 92 | return luaL_error(L, MSGInvalid); |
242 | 493 | lua_pushinteger(L, l_castU2S(n + 1)); |
243 | 493 | lua_pushinteger(L, l_castU2S(code)); |
244 | 493 | return 2; |
245 | 585 | } |
246 | 1.71k | } |
247 | | |
248 | | |
249 | 1.71k | static int iter_auxstrict (lua_State *L) { |
250 | 1.71k | return iter_aux(L, 1); |
251 | 1.71k | } |
252 | | |
253 | 0 | static int iter_auxlax (lua_State *L) { |
254 | 0 | return iter_aux(L, 0); |
255 | 0 | } |
256 | | |
257 | | |
258 | 3.07k | static int iter_codes (lua_State *L) { |
259 | 3.07k | int lax = lua_toboolean(L, 2); |
260 | 3.07k | const char *s = luaL_checkstring(L, 1); |
261 | 3.07k | luaL_argcheck(L, !iscontp(s), 1, MSGInvalid); |
262 | 3.07k | lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); |
263 | 3.07k | lua_pushvalue(L, 1); |
264 | 3.07k | lua_pushinteger(L, 0); |
265 | 3.07k | return 3; |
266 | 3.07k | } |
267 | | |
268 | | |
269 | | /* pattern to match a single UTF-8 character */ |
270 | 52.3k | #define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*" |
271 | | |
272 | | |
273 | | static const luaL_Reg funcs[] = { |
274 | | {"offset", byteoffset}, |
275 | | {"codepoint", codepoint}, |
276 | | {"char", utfchar}, |
277 | | {"len", utflen}, |
278 | | {"codes", iter_codes}, |
279 | | /* placeholders */ |
280 | | {"charpattern", NULL}, |
281 | | {NULL, NULL} |
282 | | }; |
283 | | |
284 | | |
285 | 26.1k | LUAMOD_API int luaopen_utf8 (lua_State *L) { |
286 | 26.1k | luaL_newlib(L, funcs); |
287 | 26.1k | lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); |
288 | 26.1k | lua_setfield(L, -2, "charpattern"); |
289 | 26.1k | return 1; |
290 | 26.1k | } |
291 | | |