diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-04 11:45:26 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-04 11:45:26 -0300 |
commit | 5ca1075b714e825006e8ba4f8e7ea5544879bb41 (patch) | |
tree | ad085afa58f29e8e96071ae88c1e6e1dd73e0750 /lobject.c | |
parent | 38425e069243fe6d991f2e99b4bba192af3563c7 (diff) | |
download | lua-5ca1075b714e825006e8ba4f8e7ea5544879bb41.tar.gz lua-5ca1075b714e825006e8ba4f8e7ea5544879bb41.tar.bz2 lua-5ca1075b714e825006e8ba4f8e7ea5544879bb41.zip |
Added field 'srclen' to structure 'lua_Debug'
This new field gets the length of 'source' in the same structure.
Unlike the other strings in that structure, 'source' can be
relatively large, and Lua already has its length readily available.
Diffstat (limited to 'lobject.c')
-rw-r--r-- | lobject.c | 27 |
1 files changed, 12 insertions, 15 deletions
@@ -473,45 +473,42 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { | |||
473 | } | 473 | } |
474 | 474 | ||
475 | 475 | ||
476 | /* number of chars of a literal string without the ending \0 */ | ||
477 | #define LL(x) (sizeof(x)/sizeof(char) - 1) | ||
478 | |||
479 | #define RETS "..." | 476 | #define RETS "..." |
480 | #define PRE "[string \"" | 477 | #define PRE "[string \"" |
481 | #define POS "\"]" | 478 | #define POS "\"]" |
482 | 479 | ||
483 | #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) | 480 | #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) |
484 | 481 | ||
485 | void luaO_chunkid (char *out, const char *source, size_t bufflen) { | 482 | void luaO_chunkid (char *out, const char *source, size_t srclen) { |
486 | size_t l = strlen(source); | 483 | size_t bufflen = LUA_IDSIZE; /* free space in buffer */ |
487 | if (*source == '=') { /* 'literal' source */ | 484 | if (*source == '=') { /* 'literal' source */ |
488 | if (l <= bufflen) /* small enough? */ | 485 | if (srclen <= bufflen) /* small enough? */ |
489 | memcpy(out, source + 1, l * sizeof(char)); | 486 | memcpy(out, source + 1, srclen * sizeof(char)); |
490 | else { /* truncate it */ | 487 | else { /* truncate it */ |
491 | addstr(out, source + 1, bufflen - 1); | 488 | addstr(out, source + 1, bufflen - 1); |
492 | *out = '\0'; | 489 | *out = '\0'; |
493 | } | 490 | } |
494 | } | 491 | } |
495 | else if (*source == '@') { /* file name */ | 492 | else if (*source == '@') { /* file name */ |
496 | if (l <= bufflen) /* small enough? */ | 493 | if (srclen <= bufflen) /* small enough? */ |
497 | memcpy(out, source + 1, l * sizeof(char)); | 494 | memcpy(out, source + 1, srclen * sizeof(char)); |
498 | else { /* add '...' before rest of name */ | 495 | else { /* add '...' before rest of name */ |
499 | addstr(out, RETS, LL(RETS)); | 496 | addstr(out, RETS, LL(RETS)); |
500 | bufflen -= LL(RETS); | 497 | bufflen -= LL(RETS); |
501 | memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); | 498 | memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char)); |
502 | } | 499 | } |
503 | } | 500 | } |
504 | else { /* string; format as [string "source"] */ | 501 | else { /* string; format as [string "source"] */ |
505 | const char *nl = strchr(source, '\n'); /* find first new line (if any) */ | 502 | const char *nl = strchr(source, '\n'); /* find first new line (if any) */ |
506 | addstr(out, PRE, LL(PRE)); /* add prefix */ | 503 | addstr(out, PRE, LL(PRE)); /* add prefix */ |
507 | bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ | 504 | bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ |
508 | if (l < bufflen && nl == NULL) { /* small one-line source? */ | 505 | if (srclen < bufflen && nl == NULL) { /* small one-line source? */ |
509 | addstr(out, source, l); /* keep it */ | 506 | addstr(out, source, srclen); /* keep it */ |
510 | } | 507 | } |
511 | else { | 508 | else { |
512 | if (nl != NULL) l = nl - source; /* stop at first newline */ | 509 | if (nl != NULL) srclen = nl - source; /* stop at first newline */ |
513 | if (l > bufflen) l = bufflen; | 510 | if (srclen > bufflen) srclen = bufflen; |
514 | addstr(out, source, l); | 511 | addstr(out, source, srclen); |
515 | addstr(out, RETS, LL(RETS)); | 512 | addstr(out, RETS, LL(RETS)); |
516 | } | 513 | } |
517 | memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); | 514 | memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); |