diff options
Diffstat (limited to 'lstrlib.c')
| -rw-r--r-- | lstrlib.c | 45 |
1 files changed, 23 insertions, 22 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstrlib.c,v 1.24 1999/02/04 19:10:30 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.24 1999/02/04 19:29:51 roberto Exp roberto $ |
| 3 | ** Standard library for strings and pattern-matching | 3 | ** Standard library for strings and pattern-matching |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -470,12 +470,12 @@ static void str_format (void) { | |||
| 470 | luaL_addchar(*strfrmt++); /* %% */ | 470 | luaL_addchar(*strfrmt++); /* %% */ |
| 471 | else { /* format item */ | 471 | else { /* format item */ |
| 472 | struct Capture cap; | 472 | struct Capture cap; |
| 473 | char form[MAX_FORMAT]; /* store the format ('%...') */ | 473 | char form[MAX_FORMAT]; /* to store the format ('%...') */ |
| 474 | char *buff; | 474 | char *buff; /* to store the formated item */ |
| 475 | char *initf = strfrmt; | 475 | char *initf = strfrmt; |
| 476 | form[0] = '%'; | 476 | form[0] = '%'; |
| 477 | if (isdigit((unsigned char)initf[0]) && initf[1] == '$') { | 477 | if (isdigit((unsigned char)*initf) && *(initf+1) == '$') { |
| 478 | arg = initf[0] - '0'; | 478 | arg = *initf - '0'; |
| 479 | initf += 2; /* skip the 'n$' */ | 479 | initf += 2; /* skip the 'n$' */ |
| 480 | } | 480 | } |
| 481 | arg++; | 481 | arg++; |
| @@ -487,24 +487,8 @@ static void str_format (void) { | |||
| 487 | lua_error("invalid format (width or precision too long)"); | 487 | lua_error("invalid format (width or precision too long)"); |
| 488 | strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */ | 488 | strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */ |
| 489 | form[strfrmt-initf+2] = 0; | 489 | form[strfrmt-initf+2] = 0; |
| 490 | buff = luaL_openspace(450); /* 450 > size of format('%99.99f', -1e308) */ | 490 | buff = luaL_openspace(512); /* 512 > soid luaI_addquot99.99f', -1e308) */ |
| 491 | switch (*strfrmt++) { | 491 | switch (*strfrmt++) { |
| 492 | case 'q': | ||
| 493 | luaI_addquoted(arg); | ||
| 494 | continue; | ||
| 495 | case 's': { | ||
| 496 | char *s = luaL_check_string(arg); | ||
| 497 | int l = strlen(s); | ||
| 498 | buff = luaL_openspace(l+1); | ||
| 499 | if (cap.capture[1].len == 0 && l >= 100) { | ||
| 500 | /* no precision and string is too big to be formated; | ||
| 501 | keep original string */ | ||
| 502 | strcpy(buff, s); | ||
| 503 | } | ||
| 504 | else | ||
| 505 | sprintf(buff, form, s); | ||
| 506 | break; | ||
| 507 | } | ||
| 508 | case 'c': case 'd': case 'i': | 492 | case 'c': case 'd': case 'i': |
| 509 | sprintf(buff, form, luaL_check_int(arg)); | 493 | sprintf(buff, form, luaL_check_int(arg)); |
| 510 | break; | 494 | break; |
| @@ -514,6 +498,23 @@ static void str_format (void) { | |||
| 514 | case 'e': case 'E': case 'f': case 'g': case 'G': | 498 | case 'e': case 'E': case 'f': case 'g': case 'G': |
| 515 | sprintf(buff, form, luaL_check_number(arg)); | 499 | sprintf(buff, form, luaL_check_number(arg)); |
| 516 | break; | 500 | break; |
| 501 | case 'q': | ||
| 502 | luaI_addquoted(arg); | ||
| 503 | continue; /* skip the "addsize" at the end */ | ||
| 504 | case 's': { | ||
| 505 | long l; | ||
| 506 | char *s = luaL_check_lstr(arg, &l); | ||
| 507 | if (cap.capture[1].len == 0 && l >= 100) { | ||
| 508 | /* no precision and string is too big to be formated; | ||
| 509 | keep original string */ | ||
| 510 | addnchar(s, l); | ||
| 511 | continue; /* skip the "addsize" at the end */ | ||
| 512 | } | ||
| 513 | else { | ||
| 514 | sprintf(buff, form, s); | ||
| 515 | break; | ||
| 516 | } | ||
| 517 | } | ||
| 517 | default: /* also treat cases 'pnLlh' */ | 518 | default: /* also treat cases 'pnLlh' */ |
| 518 | lua_error("invalid option in `format'"); | 519 | lua_error("invalid option in `format'"); |
| 519 | } | 520 | } |
