lobject.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. ** $Id: lobject.c $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lobject_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <locale.h>
  10. #include <math.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "lua.h"
  16. #include "lctype.h"
  17. #include "ldebug.h"
  18. #include "ldo.h"
  19. #include "lmem.h"
  20. #include "lobject.h"
  21. #include "lstate.h"
  22. #include "lstring.h"
  23. #include "lvm.h"
  24. /*
  25. ** converts an integer to a "floating point byte", represented as
  26. ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
  27. ** eeeee != 0 and (xxx) otherwise.
  28. */
  29. int luaO_int2fb (unsigned int x) {
  30. int e = 0; /* exponent */
  31. if (x < 8) return x;
  32. while (x >= (8 << 4)) { /* coarse steps */
  33. x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
  34. e += 4;
  35. }
  36. while (x >= (8 << 1)) { /* fine steps */
  37. x = (x + 1) >> 1; /* x = ceil(x / 2) */
  38. e++;
  39. }
  40. return ((e+1) << 3) | (cast_int(x) - 8);
  41. }
  42. /* converts back */
  43. int luaO_fb2int (int x) {
  44. return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1);
  45. }
  46. /*
  47. ** Computes ceil(log2(x))
  48. */
  49. int luaO_ceillog2 (unsigned int x) {
  50. static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
  51. 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  52. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  53. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  54. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  55. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  56. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  57. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  58. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
  59. };
  60. int l = 0;
  61. x--;
  62. while (x >= 256) { l += 8; x >>= 8; }
  63. return l + log_2[x];
  64. }
  65. static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
  66. lua_Integer v2) {
  67. switch (op) {
  68. case LUA_OPADD: return intop(+, v1, v2);
  69. case LUA_OPSUB:return intop(-, v1, v2);
  70. case LUA_OPMUL:return intop(*, v1, v2);
  71. case LUA_OPMOD: return luaV_mod(L, v1, v2);
  72. case LUA_OPIDIV: return luaV_idiv(L, v1, v2);
  73. case LUA_OPBAND: return intop(&, v1, v2);
  74. case LUA_OPBOR: return intop(|, v1, v2);
  75. case LUA_OPBXOR: return intop(^, v1, v2);
  76. case LUA_OPSHL: return luaV_shiftl(v1, v2);
  77. case LUA_OPSHR: return luaV_shiftl(v1, -v2);
  78. case LUA_OPUNM: return intop(-, 0, v1);
  79. case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
  80. default: lua_assert(0); return 0;
  81. }
  82. }
  83. static lua_Number numarith (lua_State *L, int op, lua_Number v1,
  84. lua_Number v2) {
  85. switch (op) {
  86. case LUA_OPADD: return luai_numadd(L, v1, v2);
  87. case LUA_OPSUB: return luai_numsub(L, v1, v2);
  88. case LUA_OPMUL: return luai_nummul(L, v1, v2);
  89. case LUA_OPDIV: return luai_numdiv(L, v1, v2);
  90. case LUA_OPPOW: return luai_numpow(L, v1, v2);
  91. case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
  92. case LUA_OPUNM: return luai_numunm(L, v1);
  93. case LUA_OPMOD: return luaV_modf(L, v1, v2);
  94. default: lua_assert(0); return 0;
  95. }
  96. }
  97. int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2,
  98. TValue *res) {
  99. switch (op) {
  100. case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
  101. case LUA_OPSHL: case LUA_OPSHR:
  102. case LUA_OPBNOT: { /* operate only on integers */
  103. lua_Integer i1; lua_Integer i2;
  104. if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) {
  105. setivalue(res, intarith(L, op, i1, i2));
  106. return 1;
  107. }
  108. else return 0; /* fail */
  109. }
  110. case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
  111. lua_Number n1; lua_Number n2;
  112. if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
  113. setfltvalue(res, numarith(L, op, n1, n2));
  114. return 1;
  115. }
  116. else return 0; /* fail */
  117. }
  118. default: { /* other operations */
  119. lua_Number n1; lua_Number n2;
  120. if (ttisinteger(p1) && ttisinteger(p2)) {
  121. setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
  122. return 1;
  123. }
  124. else if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
  125. setfltvalue(res, numarith(L, op, n1, n2));
  126. return 1;
  127. }
  128. else return 0; /* fail */
  129. }
  130. }
  131. }
  132. void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
  133. StkId res) {
  134. if (!luaO_rawarith(L, op, p1, p2, s2v(res))) {
  135. /* could not perform raw operation; try metamethod */
  136. luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
  137. }
  138. }
  139. int luaO_hexavalue (int c) {
  140. if (lisdigit(c)) return c - '0';
  141. else return (ltolower(c) - 'a') + 10;
  142. }
  143. static int isneg (const char **s) {
  144. if (**s == '-') { (*s)++; return 1; }
  145. else if (**s == '+') (*s)++;
  146. return 0;
  147. }
  148. /*
  149. ** {==================================================================
  150. ** Lua's implementation for 'lua_strx2number'
  151. ** ===================================================================
  152. */
  153. #if !defined(lua_strx2number)
  154. /* maximum number of significant digits to read (to avoid overflows
  155. even with single floats) */
  156. #define MAXSIGDIG 30
  157. /*
  158. ** convert a hexadecimal numeric string to a number, following
  159. ** C99 specification for 'strtod'
  160. */
  161. static lua_Number lua_strx2number (const char *s, char **endptr) {
  162. int dot = lua_getlocaledecpoint();
  163. lua_Number r = 0.0; /* result (accumulator) */
  164. int sigdig = 0; /* number of significant digits */
  165. int nosigdig = 0; /* number of non-significant digits */
  166. int e = 0; /* exponent correction */
  167. int neg; /* 1 if number is negative */
  168. int hasdot = 0; /* true after seen a dot */
  169. *endptr = cast_charp(s); /* nothing is valid yet */
  170. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  171. neg = isneg(&s); /* check sign */
  172. if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
  173. return 0.0; /* invalid format (no '0x') */
  174. for (s += 2; ; s++) { /* skip '0x' and read numeral */
  175. if (*s == dot) {
  176. if (hasdot) break; /* second dot? stop loop */
  177. else hasdot = 1;
  178. }
  179. else if (lisxdigit(cast_uchar(*s))) {
  180. if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
  181. nosigdig++;
  182. else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
  183. r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
  184. else e++; /* too many digits; ignore, but still count for exponent */
  185. if (hasdot) e--; /* decimal digit? correct exponent */
  186. }
  187. else break; /* neither a dot nor a digit */
  188. }
  189. if (nosigdig + sigdig == 0) /* no digits? */
  190. return 0.0; /* invalid format */
  191. *endptr = cast_charp(s); /* valid up to here */
  192. e *= 4; /* each digit multiplies/divides value by 2^4 */
  193. if (*s == 'p' || *s == 'P') { /* exponent part? */
  194. int exp1 = 0; /* exponent value */
  195. int neg1; /* exponent sign */
  196. s++; /* skip 'p' */
  197. neg1 = isneg(&s); /* sign */
  198. if (!lisdigit(cast_uchar(*s)))
  199. return 0.0; /* invalid; must have at least one digit */
  200. while (lisdigit(cast_uchar(*s))) /* read exponent */
  201. exp1 = exp1 * 10 + *(s++) - '0';
  202. if (neg1) exp1 = -exp1;
  203. e += exp1;
  204. *endptr = cast_charp(s); /* valid up to here */
  205. }
  206. if (neg) r = -r;
  207. return l_mathop(ldexp)(r, e);
  208. }
  209. #endif
  210. /* }====================================================== */
  211. /* maximum length of a numeral */
  212. #if !defined (L_MAXLENNUM)
  213. #define L_MAXLENNUM 200
  214. #endif
  215. static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
  216. char *endptr;
  217. *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
  218. : lua_str2number(s, &endptr);
  219. if (endptr == s) return NULL; /* nothing recognized? */
  220. while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
  221. return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */
  222. }
  223. /*
  224. ** Convert string 's' to a Lua number (put in 'result'). Return NULL
  225. ** on fail or the address of the ending '\0' on success.
  226. ** 'pmode' points to (and 'mode' contains) special things in the string:
  227. ** - 'x'/'X' means a hexadecimal numeral
  228. ** - 'n'/'N' means 'inf' or 'nan' (which should be rejected)
  229. ** - '.' just optimizes the search for the common case (nothing special)
  230. ** This function accepts both the current locale or a dot as the radix
  231. ** mark. If the conversion fails, it may mean number has a dot but
  232. ** locale accepts something else. In that case, the code copies 's'
  233. ** to a buffer (because 's' is read-only), changes the dot to the
  234. ** current locale radix mark, and tries to convert again.
  235. */
  236. static const char *l_str2d (const char *s, lua_Number *result) {
  237. const char *endptr;
  238. const char *pmode = strpbrk(s, ".xXnN");
  239. int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
  240. if (mode == 'n') /* reject 'inf' and 'nan' */
  241. return NULL;
  242. endptr = l_str2dloc(s, result, mode); /* try to convert */
  243. if (endptr == NULL) { /* failed? may be a different locale */
  244. char buff[L_MAXLENNUM + 1];
  245. const char *pdot = strchr(s, '.');
  246. if (strlen(s) > L_MAXLENNUM || pdot == NULL)
  247. return NULL; /* string too long or no dot; fail */
  248. strcpy(buff, s); /* copy string to buffer */
  249. buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
  250. endptr = l_str2dloc(buff, result, mode); /* try again */
  251. if (endptr != NULL)
  252. endptr = s + (endptr - buff); /* make relative to 's' */
  253. }
  254. return endptr;
  255. }
  256. #define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
  257. #define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
  258. static const char *l_str2int (const char *s, lua_Integer *result) {
  259. lua_Unsigned a = 0;
  260. int empty = 1;
  261. int neg;
  262. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  263. neg = isneg(&s);
  264. if (s[0] == '0' &&
  265. (s[1] == 'x' || s[1] == 'X')) { /* hex? */
  266. s += 2; /* skip '0x' */
  267. for (; lisxdigit(cast_uchar(*s)); s++) {
  268. a = a * 16 + luaO_hexavalue(*s);
  269. empty = 0;
  270. }
  271. }
  272. else { /* decimal */
  273. for (; lisdigit(cast_uchar(*s)); s++) {
  274. int d = *s - '0';
  275. if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
  276. return NULL; /* do not accept it (as integer) */
  277. a = a * 10 + d;
  278. empty = 0;
  279. }
  280. }
  281. while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
  282. if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
  283. else {
  284. *result = l_castU2S((neg) ? 0u - a : a);
  285. return s;
  286. }
  287. }
  288. size_t luaO_str2num (const char *s, TValue *o) {
  289. lua_Integer i; lua_Number n;
  290. const char *e;
  291. if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
  292. setivalue(o, i);
  293. }
  294. else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
  295. setfltvalue(o, n);
  296. }
  297. else
  298. return 0; /* conversion failed */
  299. return (e - s) + 1; /* success; return string size */
  300. }
  301. int luaO_utf8esc (char *buff, unsigned long x) {
  302. int n = 1; /* number of bytes put in buffer (backwards) */
  303. lua_assert(x <= 0x7FFFFFFFu);
  304. if (x < 0x80) /* ascii? */
  305. buff[UTF8BUFFSZ - 1] = cast_char(x);
  306. else { /* need continuation bytes */
  307. unsigned int mfb = 0x3f; /* maximum that fits in first byte */
  308. do { /* add continuation bytes */
  309. buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f));
  310. x >>= 6; /* remove added bits */
  311. mfb >>= 1; /* now there is one less bit available in first byte */
  312. } while (x > mfb); /* still needs continuation byte? */
  313. buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */
  314. }
  315. return n;
  316. }
  317. /* maximum length of the conversion of a number to a string */
  318. #define MAXNUMBER2STR 50
  319. /*
  320. ** Convert a number object to a string, adding it to a buffer
  321. */
  322. static int tostringbuff (TValue *obj, char *buff) {
  323. int len;
  324. lua_assert(ttisnumber(obj));
  325. if (ttisinteger(obj))
  326. len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj));
  327. else {
  328. len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj));
  329. if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
  330. buff[len++] = lua_getlocaledecpoint();
  331. buff[len++] = '0'; /* adds '.0' to result */
  332. }
  333. }
  334. return len;
  335. }
  336. /*
  337. ** Convert a number object to a Lua string, replacing the value at 'obj'
  338. */
  339. void luaO_tostring (lua_State *L, TValue *obj) {
  340. char buff[MAXNUMBER2STR];
  341. int len = tostringbuff(obj, buff);
  342. setsvalue(L, obj, luaS_newlstr(L, buff, len));
  343. }
  344. /*
  345. ** {==================================================================
  346. ** 'luaO_pushvfstring'
  347. ** ===================================================================
  348. */
  349. /* size for buffer space used by 'luaO_pushvfstring' */
  350. #define BUFVFS 400
  351. /* buffer used by 'luaO_pushvfstring' */
  352. typedef struct BuffFS {
  353. lua_State *L;
  354. int pushed; /* number of string pieces already on the stack */
  355. int blen; /* length of partial string in 'space' */
  356. char space[BUFVFS]; /* holds last part of the result */
  357. } BuffFS;
  358. /*
  359. ** Push given string to the stack, as part of the buffer. If the stack
  360. ** is almost full, join all partial strings in the stack into one.
  361. */
  362. static void pushstr (BuffFS *buff, const char *str, size_t l) {
  363. lua_State *L = buff->L;
  364. setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
  365. L->top++; /* may use one extra slot */
  366. buff->pushed++;
  367. if (buff->pushed > 1 && L->top + 1 >= L->stack_last) {
  368. luaV_concat(L, buff->pushed); /* join all partial results into one */
  369. buff->pushed = 1;
  370. }
  371. }
  372. /*
  373. ** empty the buffer space into the stack
  374. */
  375. static void clearbuff (BuffFS *buff) {
  376. pushstr(buff, buff->space, buff->blen); /* push buffer contents */
  377. buff->blen = 0; /* space now is empty */
  378. }
  379. /*
  380. ** Get a space of size 'sz' in the buffer. If buffer has not enough
  381. ** space, empty it. 'sz' must fit in an empty buffer.
  382. */
  383. static char *getbuff (BuffFS *buff, int sz) {
  384. lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS);
  385. if (sz > BUFVFS - buff->blen) /* not enough space? */
  386. clearbuff(buff);
  387. return buff->space + buff->blen;
  388. }
  389. #define addsize(b,sz) ((b)->blen += (sz))
  390. /*
  391. ** Add 'str' to the buffer. If string is larger than the buffer space,
  392. ** push the string directly to the stack.
  393. */
  394. static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
  395. if (slen <= BUFVFS) { /* does string fit into buffer? */
  396. char *bf = getbuff(buff, cast_int(slen));
  397. memcpy(bf, str, slen); /* add string to buffer */
  398. addsize(buff, cast_int(slen));
  399. }
  400. else { /* string larger than buffer */
  401. clearbuff(buff); /* string comes after buffer's content */
  402. pushstr(buff, str, slen); /* push string */
  403. }
  404. }
  405. /*
  406. ** Add a number to the buffer.
  407. */
  408. static void addnum2buff (BuffFS *buff, TValue *num) {
  409. char *numbuff = getbuff(buff, MAXNUMBER2STR);
  410. int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */
  411. addsize(buff, len);
  412. }
  413. /*
  414. ** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%'
  415. conventional formats, plus Lua-specific '%I' and '%U'
  416. */
  417. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  418. BuffFS buff; /* holds last part of the result */
  419. const char *e; /* points to next '%' */
  420. buff.pushed = buff.blen = 0;
  421. buff.L = L;
  422. while ((e = strchr(fmt, '%')) != NULL) {
  423. addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */
  424. switch (*(e + 1)) { /* conversion specifier */
  425. case 's': { /* zero-terminated string */
  426. const char *s = va_arg(argp, char *);
  427. if (s == NULL) s = "(null)";
  428. addstr2buff(&buff, s, strlen(s));
  429. break;
  430. }
  431. case 'c': { /* an 'int' as a character */
  432. char c = cast_uchar(va_arg(argp, int));
  433. addstr2buff(&buff, &c, sizeof(char));
  434. break;
  435. }
  436. case 'd': { /* an 'int' */
  437. TValue num;
  438. setivalue(&num, va_arg(argp, int));
  439. addnum2buff(&buff, &num);
  440. break;
  441. }
  442. case 'I': { /* a 'lua_Integer' */
  443. TValue num;
  444. setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt)));
  445. addnum2buff(&buff, &num);
  446. break;
  447. }
  448. case 'f': { /* a 'lua_Number' */
  449. TValue num;
  450. setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber)));
  451. addnum2buff(&buff, &num);
  452. break;
  453. }
  454. case 'p': { /* a pointer */
  455. const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
  456. char *bf = getbuff(&buff, sz);
  457. void *p = va_arg(argp, void *);
  458. int len = lua_pointer2str(bf, sz, p);
  459. addsize(&buff, len);
  460. break;
  461. }
  462. case 'U': { /* a 'long' as a UTF-8 sequence */
  463. char bf[UTF8BUFFSZ];
  464. int len = luaO_utf8esc(bf, va_arg(argp, long));
  465. addstr2buff(&buff, bf + UTF8BUFFSZ - len, len);
  466. break;
  467. }
  468. case '%': {
  469. addstr2buff(&buff, "%", 1);
  470. break;
  471. }
  472. default: {
  473. luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
  474. *(e + 1));
  475. }
  476. }
  477. fmt = e + 2; /* skip '%' and the specifier */
  478. }
  479. addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */
  480. clearbuff(&buff); /* empty buffer into the stack */
  481. if (buff.pushed > 1)
  482. luaV_concat(L, buff.pushed); /* join all partial results */
  483. return svalue(s2v(L->top - 1));
  484. }
  485. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  486. const char *msg;
  487. va_list argp;
  488. va_start(argp, fmt);
  489. msg = luaO_pushvfstring(L, fmt, argp);
  490. va_end(argp);
  491. return msg;
  492. }
  493. /* }================================================================== */
  494. #define RETS "..."
  495. #define PRE "[string \""
  496. #define POS "\"]"
  497. #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
  498. void luaO_chunkid (char *out, const char *source, size_t srclen) {
  499. size_t bufflen = LUA_IDSIZE; /* free space in buffer */
  500. if (*source == '=') { /* 'literal' source */
  501. if (srclen <= bufflen) /* small enough? */
  502. memcpy(out, source + 1, srclen * sizeof(char));
  503. else { /* truncate it */
  504. addstr(out, source + 1, bufflen - 1);
  505. *out = '\0';
  506. }
  507. }
  508. else if (*source == '@') { /* file name */
  509. if (srclen <= bufflen) /* small enough? */
  510. memcpy(out, source + 1, srclen * sizeof(char));
  511. else { /* add '...' before rest of name */
  512. addstr(out, RETS, LL(RETS));
  513. bufflen -= LL(RETS);
  514. memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char));
  515. }
  516. }
  517. else { /* string; format as [string "source"] */
  518. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  519. addstr(out, PRE, LL(PRE)); /* add prefix */
  520. bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
  521. if (srclen < bufflen && nl == NULL) { /* small one-line source? */
  522. addstr(out, source, srclen); /* keep it */
  523. }
  524. else {
  525. if (nl != NULL) srclen = nl - source; /* stop at first newline */
  526. if (srclen > bufflen) srclen = bufflen;
  527. addstr(out, source, srclen);
  528. addstr(out, RETS, LL(RETS));
  529. }
  530. memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
  531. }
  532. }