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