diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-05-03 10:18:44 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-05-03 10:18:44 -0300 |
| commit | 7c5786479c1d617ec7c133f2c2b955726436267a (patch) | |
| tree | 614cbcd89903fdbb06208a0c17196264da6a4166 | |
| parent | b14609032cf328dea48b0803f3e585e223283b3d (diff) | |
| download | lua-7c5786479c1d617ec7c133f2c2b955726436267a.tar.gz lua-7c5786479c1d617ec7c133f2c2b955726436267a.tar.bz2 lua-7c5786479c1d617ec7c133f2c2b955726436267a.zip | |
A few more improvements in 'luaO_pushvfstring'
- 'L' added to the 'BuffFS' structure
- '%c' does not handle control characters (it is not its business.
This now is done by the lexer, who is the one in charge of that
kind of errors.)
- avoid the direct use of 'l_sprintf' in the Lua kernel
| -rw-r--r-- | llex.c | 5 | ||||
| -rw-r--r-- | lobject.c | 66 | ||||
| -rw-r--r-- | luaconf.h | 13 | ||||
| -rw-r--r-- | testes/strings.lua | 3 |
4 files changed, 52 insertions, 35 deletions
| @@ -82,7 +82,10 @@ void luaX_init (lua_State *L) { | |||
| 82 | const char *luaX_token2str (LexState *ls, int token) { | 82 | const char *luaX_token2str (LexState *ls, int token) { |
| 83 | if (token < FIRST_RESERVED) { /* single-byte symbols? */ | 83 | if (token < FIRST_RESERVED) { /* single-byte symbols? */ |
| 84 | lua_assert(token == cast_uchar(token)); | 84 | lua_assert(token == cast_uchar(token)); |
| 85 | return luaO_pushfstring(ls->L, "'%c'", token); | 85 | if (lisprint(token)) |
| 86 | return luaO_pushfstring(ls->L, "'%c'", token); | ||
| 87 | else /* control character */ | ||
| 88 | return luaO_pushfstring(ls->L, "'<\\%d>'", token); | ||
| 86 | } | 89 | } |
| 87 | else { | 90 | else { |
| 88 | const char *s = luaX_tokens[token - FIRST_RESERVED]; | 91 | const char *s = luaX_tokens[token - FIRST_RESERVED]; |
| @@ -392,11 +392,20 @@ void luaO_tostring (lua_State *L, TValue *obj) { | |||
| 392 | } | 392 | } |
| 393 | 393 | ||
| 394 | 394 | ||
| 395 | |||
| 396 | |||
| 397 | /* | ||
| 398 | ** {================================================================== | ||
| 399 | ** 'luaO_pushvfstring' | ||
| 400 | ** =================================================================== | ||
| 401 | */ | ||
| 402 | |||
| 395 | /* size for buffer space used by 'luaO_pushvfstring' */ | 403 | /* size for buffer space used by 'luaO_pushvfstring' */ |
| 396 | #define BUFVFS 400 | 404 | #define BUFVFS 400 |
| 397 | 405 | ||
| 398 | /* buffer used by 'luaO_pushvfstring' */ | 406 | /* buffer used by 'luaO_pushvfstring' */ |
| 399 | typedef struct BuffFS { | 407 | typedef struct BuffFS { |
| 408 | lua_State *L; | ||
| 400 | int pushed; /* number of string pieces already on the stack */ | 409 | int pushed; /* number of string pieces already on the stack */ |
| 401 | int blen; /* length of partial string in 'space' */ | 410 | int blen; /* length of partial string in 'space' */ |
| 402 | char space[BUFVFS]; /* holds last part of the result */ | 411 | char space[BUFVFS]; /* holds last part of the result */ |
| @@ -407,7 +416,8 @@ typedef struct BuffFS { | |||
| 407 | ** Push given string to the stack, as part of the buffer. If the stack | 416 | ** Push given string to the stack, as part of the buffer. If the stack |
| 408 | ** is almost full, join all partial strings in the stack into one. | 417 | ** is almost full, join all partial strings in the stack into one. |
| 409 | */ | 418 | */ |
| 410 | static void pushstr (lua_State *L, BuffFS *buff, const char *str, size_t l) { | 419 | static void pushstr (BuffFS *buff, const char *str, size_t l) { |
| 420 | lua_State *L = buff->L; | ||
| 411 | setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); | 421 | setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); |
| 412 | L->top++; | 422 | L->top++; |
| 413 | buff->pushed++; | 423 | buff->pushed++; |
| @@ -421,8 +431,8 @@ static void pushstr (lua_State *L, BuffFS *buff, const char *str, size_t l) { | |||
| 421 | /* | 431 | /* |
| 422 | ** empty the buffer space into the stack | 432 | ** empty the buffer space into the stack |
| 423 | */ | 433 | */ |
| 424 | static void clearbuff (lua_State *L, BuffFS *buff) { | 434 | static void clearbuff (BuffFS *buff) { |
| 425 | pushstr(L, buff, buff->space, buff->blen); /* push buffer contents */ | 435 | pushstr(buff, buff->space, buff->blen); /* push buffer contents */ |
| 426 | buff->blen = 0; /* space now is empty */ | 436 | buff->blen = 0; /* space now is empty */ |
| 427 | } | 437 | } |
| 428 | 438 | ||
| @@ -431,10 +441,10 @@ static void clearbuff (lua_State *L, BuffFS *buff) { | |||
| 431 | ** Get a space of size 'sz' in the buffer. If buffer has not enough | 441 | ** Get a space of size 'sz' in the buffer. If buffer has not enough |
| 432 | ** space, empty it. 'sz' must fit in an empty space. | 442 | ** space, empty it. 'sz' must fit in an empty space. |
| 433 | */ | 443 | */ |
| 434 | static char *getbuff (lua_State *L, BuffFS *buff, size_t sz) { | 444 | static char *getbuff (BuffFS *buff, size_t sz) { |
| 435 | lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); | 445 | lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); |
| 436 | if (sz > BUFVFS - cast_sizet(buff->blen)) /* string does not fit? */ | 446 | if (sz > BUFVFS - cast_sizet(buff->blen)) /* string does not fit? */ |
| 437 | clearbuff(L, buff); | 447 | clearbuff(buff); |
| 438 | return buff->space + buff->blen; | 448 | return buff->space + buff->blen; |
| 439 | } | 449 | } |
| 440 | 450 | ||
| @@ -446,16 +456,15 @@ static char *getbuff (lua_State *L, BuffFS *buff, size_t sz) { | |||
| 446 | ** Add 'str' to the buffer. If string is larger than the buffer space, | 456 | ** Add 'str' to the buffer. If string is larger than the buffer space, |
| 447 | ** push the string directly to the stack. | 457 | ** push the string directly to the stack. |
| 448 | */ | 458 | */ |
| 449 | static void addstr2buff (lua_State *L, BuffFS *buff, const char *str, | 459 | static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { |
| 450 | size_t slen) { | ||
| 451 | if (slen <= BUFVFS) { /* does string fit into buffer? */ | 460 | if (slen <= BUFVFS) { /* does string fit into buffer? */ |
| 452 | char *bf = getbuff(L, buff, slen); | 461 | char *bf = getbuff(buff, slen); |
| 453 | memcpy(bf, str, slen); /* add string to buffer */ | 462 | memcpy(bf, str, slen); /* add string to buffer */ |
| 454 | addsize(buff, slen); | 463 | addsize(buff, slen); |
| 455 | } | 464 | } |
| 456 | else { /* string larger than buffer */ | 465 | else { /* string larger than buffer */ |
| 457 | clearbuff(L, buff); /* string comes after buffer's content */ | 466 | clearbuff(buff); /* string comes after buffer's content */ |
| 458 | pushstr(L, buff, str, slen); /* push string */ | 467 | pushstr(buff, str, slen); /* push string */ |
| 459 | } | 468 | } |
| 460 | } | 469 | } |
| 461 | 470 | ||
| @@ -463,8 +472,8 @@ static void addstr2buff (lua_State *L, BuffFS *buff, const char *str, | |||
| 463 | /* | 472 | /* |
| 464 | ** Add a number to the buffer. | 473 | ** Add a number to the buffer. |
| 465 | */ | 474 | */ |
| 466 | static void addnum2buff (lua_State *L, BuffFS *buff, TValue *num) { | 475 | static void addnum2buff (BuffFS *buff, TValue *num) { |
| 467 | char *numbuff = getbuff(L, buff, MAXNUMBER2STR); | 476 | char *numbuff = getbuff(buff, MAXNUMBER2STR); |
| 468 | size_t len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ | 477 | size_t len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ |
| 469 | addsize(buff, len); | 478 | addsize(buff, len); |
| 470 | } | 479 | } |
| @@ -478,58 +487,55 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
| 478 | BuffFS buff; /* holds last part of the result */ | 487 | BuffFS buff; /* holds last part of the result */ |
| 479 | const char *e; /* points to next '%' */ | 488 | const char *e; /* points to next '%' */ |
| 480 | buff.pushed = buff.blen = 0; | 489 | buff.pushed = buff.blen = 0; |
| 490 | buff.L = L; | ||
| 481 | while ((e = strchr(fmt, '%')) != NULL) { | 491 | while ((e = strchr(fmt, '%')) != NULL) { |
| 482 | addstr2buff(L, &buff, fmt, e - fmt); /* add 'fmt' up to '%' */ | 492 | addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */ |
| 483 | switch (*(e + 1)) { /* conversion specifier */ | 493 | switch (*(e + 1)) { /* conversion specifier */ |
| 484 | case 's': { /* zero-terminated string */ | 494 | case 's': { /* zero-terminated string */ |
| 485 | const char *s = va_arg(argp, char *); | 495 | const char *s = va_arg(argp, char *); |
| 486 | if (s == NULL) s = "(null)"; | 496 | if (s == NULL) s = "(null)"; |
| 487 | addstr2buff(L, &buff, s, strlen(s)); | 497 | addstr2buff(&buff, s, strlen(s)); |
| 488 | break; | 498 | break; |
| 489 | } | 499 | } |
| 490 | case 'c': { /* an 'int' as a character */ | 500 | case 'c': { /* an 'int' as a character */ |
| 491 | /* if non-printable character, print its code */ | 501 | char c = cast_uchar(va_arg(argp, int)); |
| 492 | char *bf = getbuff(L, &buff, 10); | 502 | addstr2buff(&buff, &c, sizeof(char)); |
| 493 | int c = va_arg(argp, int); | ||
| 494 | int len = (lisprint(c)) ? l_sprintf(bf, 10, "%c", c) | ||
| 495 | : l_sprintf(bf, 10, "<\\%u>", c); | ||
| 496 | addsize(&buff, len); | ||
| 497 | break; | 503 | break; |
| 498 | } | 504 | } |
| 499 | case 'd': { /* an 'int' */ | 505 | case 'd': { /* an 'int' */ |
| 500 | TValue num; | 506 | TValue num; |
| 501 | setivalue(&num, va_arg(argp, int)); | 507 | setivalue(&num, va_arg(argp, int)); |
| 502 | addnum2buff(L, &buff, &num); | 508 | addnum2buff(&buff, &num); |
| 503 | break; | 509 | break; |
| 504 | } | 510 | } |
| 505 | case 'I': { /* a 'lua_Integer' */ | 511 | case 'I': { /* a 'lua_Integer' */ |
| 506 | TValue num; | 512 | TValue num; |
| 507 | setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); | 513 | setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); |
| 508 | addnum2buff(L, &buff, &num); | 514 | addnum2buff(&buff, &num); |
| 509 | break; | 515 | break; |
| 510 | } | 516 | } |
| 511 | case 'f': { /* a 'lua_Number' */ | 517 | case 'f': { /* a 'lua_Number' */ |
| 512 | TValue num; | 518 | TValue num; |
| 513 | setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); | 519 | setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); |
| 514 | addnum2buff(L, &buff, &num); | 520 | addnum2buff(&buff, &num); |
| 515 | break; | 521 | break; |
| 516 | } | 522 | } |
| 517 | case 'p': { /* a pointer */ | 523 | case 'p': { /* a pointer */ |
| 518 | const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ | 524 | const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ |
| 519 | char *bf = getbuff(L, &buff, sz); | 525 | char *bf = getbuff(&buff, sz); |
| 520 | void *p = va_arg(argp, void *); | 526 | void *p = va_arg(argp, void *); |
| 521 | int len = l_sprintf(bf, sz, "%p", p); | 527 | int len = lua_pointer2str(bf, sz, p); |
| 522 | addsize(&buff, len); | 528 | addsize(&buff, len); |
| 523 | break; | 529 | break; |
| 524 | } | 530 | } |
| 525 | case 'U': { /* a 'long' as a UTF-8 sequence */ | 531 | case 'U': { /* a 'long' as a UTF-8 sequence */ |
| 526 | char bf[UTF8BUFFSZ]; | 532 | char bf[UTF8BUFFSZ]; |
| 527 | int len = luaO_utf8esc(bf, va_arg(argp, long)); | 533 | int len = luaO_utf8esc(bf, va_arg(argp, long)); |
| 528 | addstr2buff(L, &buff, bf + UTF8BUFFSZ - len, len); | 534 | addstr2buff(&buff, bf + UTF8BUFFSZ - len, len); |
| 529 | break; | 535 | break; |
| 530 | } | 536 | } |
| 531 | case '%': { | 537 | case '%': { |
| 532 | addstr2buff(L, &buff, "%", 1); | 538 | addstr2buff(&buff, "%", 1); |
| 533 | break; | 539 | break; |
| 534 | } | 540 | } |
| 535 | default: { | 541 | default: { |
| @@ -539,8 +545,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
| 539 | } | 545 | } |
| 540 | fmt = e + 2; /* skip '%' and the specifier */ | 546 | fmt = e + 2; /* skip '%' and the specifier */ |
| 541 | } | 547 | } |
| 542 | addstr2buff(L, &buff, fmt, strlen(fmt)); /* rest of 'fmt' */ | 548 | addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ |
| 543 | clearbuff(L, &buff); /* empty buffer into the stack */ | 549 | clearbuff(&buff); /* empty buffer into the stack */ |
| 544 | if (buff.pushed > 1) | 550 | if (buff.pushed > 1) |
| 545 | luaV_concat(L, buff.pushed); /* join all partial results */ | 551 | luaV_concat(L, buff.pushed); /* join all partial results */ |
| 546 | return svalue(s2v(L->top - 1)); | 552 | return svalue(s2v(L->top - 1)); |
| @@ -556,6 +562,8 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { | |||
| 556 | return msg; | 562 | return msg; |
| 557 | } | 563 | } |
| 558 | 564 | ||
| 565 | /* }================================================================== */ | ||
| 566 | |||
| 559 | 567 | ||
| 560 | #define RETS "..." | 568 | #define RETS "..." |
| 561 | #define PRE "[string \"" | 569 | #define PRE "[string \"" |
| @@ -376,7 +376,7 @@ | |||
| 376 | @@ lua_number2str converts a float to a string. | 376 | @@ lua_number2str converts a float to a string. |
| 377 | @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. | 377 | @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. |
| 378 | @@ l_floor takes the floor of a float. | 378 | @@ l_floor takes the floor of a float. |
| 379 | @@ lua_str2number converts a decimal numeric string to a number. | 379 | @@ lua_str2number converts a decimal numeral to a number. |
| 380 | */ | 380 | */ |
| 381 | 381 | ||
| 382 | 382 | ||
| @@ -568,7 +568,7 @@ | |||
| 568 | 568 | ||
| 569 | 569 | ||
| 570 | /* | 570 | /* |
| 571 | @@ lua_strx2number converts a hexadecimal numeric string to a number. | 571 | @@ lua_strx2number converts a hexadecimal numeral to a number. |
| 572 | ** In C99, 'strtod' does that conversion. Otherwise, you can | 572 | ** In C99, 'strtod' does that conversion. Otherwise, you can |
| 573 | ** leave 'lua_strx2number' undefined and Lua will provide its own | 573 | ** leave 'lua_strx2number' undefined and Lua will provide its own |
| 574 | ** implementation. | 574 | ** implementation. |
| @@ -579,7 +579,14 @@ | |||
| 579 | 579 | ||
| 580 | 580 | ||
| 581 | /* | 581 | /* |
| 582 | @@ lua_number2strx converts a float to a hexadecimal numeric string. | 582 | @@ lua_pointer2str converts a pointer to a readable string in a |
| 583 | ** non-specified way. | ||
| 584 | */ | ||
| 585 | #define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) | ||
| 586 | |||
| 587 | |||
| 588 | /* | ||
| 589 | @@ lua_number2strx converts a float to a hexadecimal numeral. | ||
| 583 | ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. | 590 | ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. |
| 584 | ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will | 591 | ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will |
| 585 | ** provide its own implementation. | 592 | ** provide its own implementation. |
diff --git a/testes/strings.lua b/testes/strings.lua index bc123d1a..3e32f2c4 100644 --- a/testes/strings.lua +++ b/testes/strings.lua | |||
| @@ -453,8 +453,7 @@ else | |||
| 453 | 453 | ||
| 454 | str = "abc %c def" | 454 | str = "abc %c def" |
| 455 | testpfs("I", str, string.byte("A")) | 455 | testpfs("I", str, string.byte("A")) |
| 456 | -- non-printable character | 456 | testpfs("I", str, 255) |
| 457 | assert(callpfs("I", str, 255) == "abc <\\255> def") | ||
| 458 | 457 | ||
| 459 | str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen) | 458 | str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen) |
| 460 | testpfs("P", str, {}) | 459 | testpfs("P", str, {}) |
