Debian Codegraph @

texk/web2c/luatexdir/lua/lstrlibext.c

raw
1 /* lstrlibext.c
2
3 Copyright 2012 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
19
20 /* The relative ordering of the header files is important here,
21 otherwise some of the defines that are needed for lua_sdump
22 come out wrong.
23 */
24 #include "ptexlib.h"
25
26 #define LUA_CORE
27 #include "lua.h"
28 #ifdef LuajitTeX
29 #include "lauxlib.h"
30 #include "lualib.h"
31 #else
32 #include "luaconf.h"
33 #include "lapi.h"
34 #include "lundump.h"
35 #endif
36
37 static int bytepairs_aux (lua_State *L) {
38 size_t ls;
39 unsigned char i;
40 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
41 int ind = lua_tointeger(L, lua_upvalueindex(2));
42 if (ind<(int)ls) {
43 if (ind+1<(int)ls) {
44 lua_pushinteger(L, (ind+2)); /* iterator */
45 } else {
46 lua_pushinteger(L, (ind+1)); /* iterator */
47 }
48 lua_replace(L, lua_upvalueindex(2));
49 i = (unsigned char)*(s+ind);
50 lua_pushinteger(L, i); /* byte one */
51 if (ind+1<(int)ls) {
52 i = (unsigned char)*(s+ind+1);
53 lua_pushinteger(L, i); /* byte two */
54 } else {
55 lua_pushnil(L); /* odd string length */
56 }
57 return 2;
58 }
59 return 0; /* string ended */
60 }
61
62 static int str_bytepairs (lua_State *L) {
63 luaL_checkstring(L, 1);
64 lua_settop(L, 1);
65 lua_pushinteger(L, 0);
66 lua_pushcclosure(L, bytepairs_aux, 2);
67 return 1;
68 }
69
70 static int bytes_aux (lua_State *L) {
71 size_t ls;
72 unsigned char i;
73 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
74 int ind = lua_tointeger(L, lua_upvalueindex(2));
75 if (ind<(int)ls) {
76 lua_pushinteger(L, (ind+1)); /* iterator */
77 lua_replace(L, lua_upvalueindex(2));
78 i = (unsigned char)*(s+ind);
79 lua_pushinteger(L, i); /* byte */
80 return 1;
81 }
82 return 0; /* string ended */
83 }
84
85 static int str_bytes (lua_State *L) {
86 luaL_checkstring(L, 1);
87 lua_settop(L, 1);
88 lua_pushinteger(L, 0);
89 lua_pushcclosure(L, bytes_aux, 2);
90 return 1;
91 }
92
93 static int utf_failed(lua_State *L, int new_ind) {
94 static char fffd [3] = {0xEF,0xBF,0xBD};
95 lua_pushinteger(L, new_ind); /* iterator */
96 lua_replace(L, lua_upvalueindex(2));
97 lua_pushlstring(L, fffd, 3);
98 return 1;
99 }
100
101 static int utfcharacters_aux (lua_State *L) {
102 static const unsigned char mask[4] = {0x80,0xE0,0xF0,0xF8};
103 static const unsigned char mequ[4] = {0x00,0xC0,0xE0,0xF0};
104 size_t ls;
105 unsigned char c;
106 int j;
107 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
108 int ind = lua_tointeger(L, lua_upvalueindex(2));
109 if (ind>=(int)ls) return 0; /* end of string */
110 c = (unsigned) s[ind];
111 for (j=0;j<4;j++) {
112 if ((c&mask[j])==mequ[j]) {
113 int k;
114 if (ind+1+j>(int)ls) return utf_failed(L,ls); /* will not fit */
115 for (k=1; k<=j; k++) {
116 c = (unsigned) s[ind+k];
117 if ((c&0xC0)!=0x80) return utf_failed(L,ind+k); /* bad follow */
118 }
119 lua_pushinteger(L, ind+1+j); /* iterator */
120 lua_replace(L, lua_upvalueindex(2));
121 lua_pushlstring(L, s+ind, 1+j);
122 return 1;
123 }
124 }
125 return utf_failed(L,ind+1); /* we found a follow byte! */
126 }
127
128 static int str_utfcharacters (lua_State *L) {
129 luaL_checkstring(L, 1);
130 lua_settop(L, 1);
131 lua_pushinteger(L, 0);
132 lua_pushcclosure(L, utfcharacters_aux, 2);
133 return 1;
134 }
135
136 static int utfvalues_aux (lua_State *L) {
137 size_t ls;
138 unsigned char i = 0;
139 unsigned char j = 0;
140 unsigned char k = 0;
141 unsigned char l = 0;
142 unsigned int v = 0xFFFD;
143 int numbytes = 1;
144 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
145 int ind = lua_tointeger(L, lua_upvalueindex(2));
146
147 if (ind<(int)ls) {
148 i = *(s+ind);
149 if (i<0x80) {
150 v = i;
151 } else if (i>=0xF0) {
152 if ((ind+3)<(int)ls && ((unsigned)*(s+ind+1))>=0x80
153 && ((unsigned)*(s+ind+2))>=0x80 && ((unsigned)*(s+ind+3))>=0x80) {
154 numbytes = 4;
155 j = ((unsigned)*(s+ind+1))-128;
156 k = ((unsigned)*(s+ind+2))-128;
157 l = ((unsigned)*(s+ind+3))-128;
158 v = (((((i-0xF0)*64) + j)*64) + k)*64 + l;
159 }
160 } else if (i>=0xE0) {
161 if ((ind+2)<(int)ls && ((unsigned)*(s+ind+1))>=0x80 && ((unsigned)*(s+ind+2))>=0x80) {
162 numbytes = 3;
163 j = ((unsigned)*(s+ind+1))-128;
164 k = ((unsigned)*(s+ind+2))-128;
165 v = (((i-0xE0)*64) + j)*64 + k;
166 }
167
168 } else if (i>=0xC0) {
169 if ((ind+1)<(int)ls && ((unsigned)*(s+ind+1))>=0x80) {
170 numbytes = 2;
171 j = ((unsigned)*(s+ind+1))-128;
172 v = ((i-0xC0)*64) + j;
173 }
174 }
175 lua_pushinteger(L, (ind+numbytes)); /* iterator */
176 lua_replace(L, lua_upvalueindex(2));
177 lua_pushinteger(L, v);
178 return 1;
179 }
180 return 0; /* string ended */
181 }
182
183 static int str_utfvalues (lua_State *L) {
184 luaL_checkstring(L, 1);
185 lua_settop(L, 1);
186 lua_pushinteger(L, 0);
187 lua_pushcclosure(L, utfvalues_aux, 2);
188 return 1;
189 }
190
191 static int characterpairs_aux (lua_State *L) {
192 size_t ls;
193 char b[2];
194 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
195 int ind = lua_tointeger(L, lua_upvalueindex(2));
196 if (ind<(int)ls) {
197 if (ind+1<(int)ls) {
198 lua_pushinteger(L, (ind+2)); /* iterator */
199 } else {
200 lua_pushinteger(L, (ind+1)); /* iterator */
201 }
202 lua_replace(L, lua_upvalueindex(2));
203 b[0] = *(s+ind); b[1] = 0;
204 lua_pushlstring(L, b, 1);
205 if (ind+1<(int)ls) {
206 b[0] = *(s+ind+1);
207 lua_pushlstring(L, b, 1);
208 } else {
209 lua_pushlstring(L, b+1, 0);
210 }
211 return 2;
212 }
213 return 0; /* string ended */
214 }
215
216 static int str_characterpairs (lua_State *L) {
217 luaL_checkstring(L, 1);
218 lua_settop(L, 1);
219 lua_pushinteger(L, 0);
220 lua_pushcclosure(L, characterpairs_aux, 2);
221 return 1;
222 }
223
224 static int characters_aux (lua_State *L) {
225 size_t ls;
226 char b[2];
227 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
228 int ind = lua_tointeger(L, lua_upvalueindex(2));
229 if (ind<(int)ls) {
230 lua_pushinteger(L, (ind+1)); /* iterator */
231 lua_replace(L, lua_upvalueindex(2));
232 b[0] = *(s+ind); b[1] = 0;
233 lua_pushlstring(L, b, 1);
234 return 1;
235 }
236 return 0; /* string ended */
237 }
238
239 static int str_characters (lua_State *L) {
240 luaL_checkstring(L, 1);
241 lua_settop(L, 1);
242 lua_pushinteger(L, 0);
243 lua_pushcclosure(L, characters_aux, 2);
244 return 1;
245 }
246
247 static int str_split (lua_State *L) {
248 size_t l;
249 size_t i;
250 int n;
251 char *q, *p, *orig;
252 int mult = 0;
253 const char *s = luaL_checklstring(L, 1, &l);
254 const char *joiner = luaL_optstring(L, 2, " +");
255 lua_newtable(L);
256 if (l == 0) {
257 lua_pushvalue(L,1);
258 lua_rawseti(L,-2,1);
259 return 1;
260 }
261 orig = p = malloc(l+1);
262 if (p==NULL) {
263 fprintf(stderr, "fatal: memory exhausted (malloc of %u bytes).\n",(int)(l+1));
264 exit(EXIT_FAILURE);
265 }
266 strcpy(p,s);
267 n = 1;
268 q = p;
269
270 if (*joiner == 0) {
271 for (i=0;i<l;i++) {
272 lua_pushlstring(L,q,1); q++;
273 lua_rawseti(L,-2,n); n++;
274 }
275 free(orig);
276 return 1;
277 }
278 if (*(joiner+1) == '+') {
279 mult = 1;
280 while(*p==*joiner) {
281 p++;
282 l--;
283 }
284 q = p;
285 }
286 for (i=0;i<l;i++) {
287 if (*(p+i)==*joiner) {
288 *(p+i) = 0;
289 lua_pushlstring(L,q,((p+i)-q));
290 lua_rawseti(L,-2,n); n++;
291 if (mult) {
292 while(*(p+i+1)==*joiner) {
293 i++;
294 }
295 }
296 q = p+i+1;
297 }
298 }
299 if (mult && q==(p+l)) {
300 free(orig);
301 return 1;
302 }
303 if(q<=(p+l)) {
304 lua_pushlstring(L,q,strlen(q));
305 lua_rawseti(L,-2,n);
306 }
307 free(orig);
308 return 1;
309 }
310
311 #ifdef LuajitTeX
312 /* dump is built in */
313 #else
314 #if (LUA_VERSION_NUM == 502)
315 static int writer (lua_State *L, const void* b, size_t size, void* B) {
316 (void)L;
317 luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
318 return 0;
319 }
320
321 static int lua_sdump (lua_State *L, lua_Writer writer, void *data, int stripping) {
322 int status;
323 TValue *o;
324 lua_lock(L);
325 api_checknelems(L, 1);
326 o = L->top - 1;
327 if (isLfunction(o))
328 status = luaU_dump(L, getproto(o), writer, data, stripping);
329 else
330 status = 1;
331 lua_unlock(L);
332 return status;
333 }
334
335
336 static int str_dump (lua_State *L) {
337 luaL_Buffer b;
338 int stripping = 0;
339 luaL_checktype(L, 1, LUA_TFUNCTION);
340 if (lua_gettop(L)==2) {
341 stripping = lua_toboolean(L,2);
342 }
343 lua_settop(L, 1);
344 luaL_buffinit(L,&b);
345 if (lua_sdump(L, writer, &b, stripping) != 0)
346 return luaL_error(L, "unable to dump given function");
347 luaL_pushresult(&b);
348 return 1;
349 }
350 #endif
351 #endif /*ifdef LuajitTeX*/
352
353 static int str_bytetable (lua_State *L) {
354 size_t l;
355 int i;
356 const char *s = luaL_checklstring(L, 1, &l);
357 lua_createtable(L,l,0);
358 for (i=0;i<l;i++) {
359 lua_pushinteger(L,(unsigned char)*(s+i));
360 lua_rawseti(L,-2,i+1);
361 }
362 return 1;
363 }
364
365 /*
366 We provide a few helpers that we derived from the lua utf8 module
367 and slunicode. That way we're sort of covering a decent mix.
368
369 */
370
371 #define MAXUNICODE 0x10FFFF
372
373 /*
374 This is a combination of slunicode and utf8 converters but without mode
375 and a bit faster on the average than the utf8 one.
376 */
377
378 static void utf8_enco (luaL_Buffer *b, unsigned c)
379 {
380 if (c > MAXUNICODE) {
381 /* we silently ignore the bad character */
382 return;
383 }
384 if (0x80 > c) {
385 luaL_addchar(b, c);
386 return;
387 }
388 if (0x800 > c)
389 luaL_addchar(b, 0xC0|(c>>6));
390 else {
391 if (0x10000 > c)
392 luaL_addchar(b, 0xE0|(c>>12));
393 else {
394 luaL_addchar(b, 0xF0|(c>>18));
395 luaL_addchar(b, 0x80|(0x3F&(c>>12)));
396 }
397 luaL_addchar(b, 0x80|(0x3F&(c>>6)));
398 }
399 luaL_addchar(b, 0x80|(0x3F&c));
400 }
401
402 static int str_character (lua_State *L) {
403 int n = lua_gettop(L); /* number of arguments */
404 int i;
405 luaL_Buffer b;
406 luaL_buffinit(L,&b);
407 for (i = 1; i <= n; i++) {
408 utf8_enco (&b, (unsigned) lua_tointeger(L, i));
409 }
410 luaL_pushresult(&b);
411 return 1;
412 }
413
414 /*
415 The utf8 codepoint function takes two arguments, being positions in the
416 string, while slunicode byte takes two arguments representing the number of
417 utf characters. The variant below always returns all codepoints.
418
419 */
420
421 static int str_utfvalue (lua_State *L) {
422 size_t ls;
423 int ind = 0;
424 int num = 0;
425 const char *s = lua_tolstring(L, 1, &ls);
426 while (ind<(int)ls) {
427 unsigned char i = (unsigned)*(s+ind);
428 if (i<0x80) {
429 lua_pushinteger(L, i);
430 num += 1;
431 ind += 1;
432 } else if (i>=0xF0) {
433 if ((ind+3)<(int)ls && ((unsigned)*(s+ind+1))>=0x80 && ((unsigned)*(s+ind+2))>=0x80 && ((unsigned)*(s+ind+3))>=0x80) {
434 unsigned char j = ((unsigned)*(s+ind+1))-128;
435 unsigned char k = ((unsigned)*(s+ind+2))-128;
436 unsigned char l = ((unsigned)*(s+ind+3))-128;
437 lua_pushinteger(L, (((((i-0xF0)*64) + j)*64) + k)*64 + l);
438 num += 1;
439 }
440 ind += 4;
441 } else if (i>=0xE0) {
442 if ((ind+2)<(int)ls && ((unsigned)*(s+ind+1))>=0x80 && ((unsigned)*(s+ind+2))>=0x80) {
443 unsigned char j = ((unsigned)*(s+ind+1))-128;
444 unsigned char k = ((unsigned)*(s+ind+2))-128;
445 lua_pushinteger(L, (((i-0xE0)*64) + j)*64 + k);
446 num += 1;
447 }
448 ind += 3;
449 } else if (i>=0xC0) {
450 if ((ind+1)<(int)ls && ((unsigned)*(s+ind+1))>=0x80) {
451 unsigned char j = ((unsigned)*(s+ind+1))-128;
452 lua_pushinteger(L, ((i-0xC0)*64) + j);
453 num += 1;
454 }
455 ind += 2;
456 } else {
457 ind += 1;
458 }
459 }
460 return num;
461 }
462
463 /* This is a simplified version of utf8.len but without range. */
464
465 static int str_utflength (lua_State *L) {
466 size_t ls;
467 int ind = 0;
468 int num = 0;
469 const char *s = lua_tolstring(L, 1, &ls);
470 while (ind<(int)ls) {
471 unsigned char i = (unsigned)*(s+ind);
472 if (i<0x80) {
473 ind += 1;
474 } else if (i>=0xF0) {
475 ind += 4;
476 } else if (i>=0xE0) {
477 ind += 3;
478 } else if (i>=0xC0) {
479 ind += 2;
480 } else {
481 /* bad news, stupid recovery */
482 ind += 1;
483 }
484 num += 1;
485 }
486 lua_pushinteger(L, num);
487 return 1;
488 }
489
490 /* end of convenience inclusion */
491
492 static const luaL_Reg strlibext[] = {
493 {"utfvalues", str_utfvalues},
494 {"utfcharacters", str_utfcharacters},
495 {"characters", str_characters},
496 {"characterpairs", str_characterpairs},
497 {"bytes", str_bytes},
498 {"bytepairs", str_bytepairs},
499 {"bytetable", str_bytetable},
500 {"explode", str_split},
501 {"utfcharacter", str_character},
502 {"utfvalue", str_utfvalue},
503 {"utflength", str_utflength},
504 #ifdef LuajitTeX
505 /* luajit has dump built in */
506 #else
507 /* lua 5.3.4 doesn't need this patch */
508 #if (LUA_VERSION_NUM == 502)
509 {"dump", str_dump},
510 #endif
511
512 #endif /* #ifdef LuajitTeX */
513 {NULL, NULL}
514 };
515
516 void open_strlibext(lua_State * L)
517 {
518 const luaL_Reg *lib;
519 lua_getglobal(L, "string");
520 for (lib=strlibext;lib->name;lib++) {
521 lua_pushcfunction(L, lib->func);
522 lua_setfield(L, -2, lib->name);
523 }
524 lua_pop(L,1);
525 }