Commit 070c929e

070c929e6280Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #1414 from mattn/sprintf-positional-args

Support positional arguments in sprintf/% formatting

Details

Commit
070c929e6280
Parent
89e3aee06af0
Merge parent
d50c4e177404

lib/sp_runtime.hmodifiedView file

@@ -3569,6 +3569,16 @@
35693569 buf[out++] = '%'; p += 2; continue;
35703570 }
35713571 char spec[64]; size_t sl = 0; spec[sl++] = *p++;
3572+ /* positional argument reference: %N$conv selects the Nth (1-based) arg */
3573+ {
3574+ const char *q = p; mrb_int argnum = 0; mrb_bool overflow = FALSE;
3575+ while (*q >= '0' && *q <= '9') {
3576+ if (sp_int_mul_overflow_p(argnum, 10, &argnum) ||
3577+ sp_int_add_overflow_p(argnum, *q - '0', &argnum)) { overflow = TRUE; break; }
3578+ q++;
3579+ }
3580+ if (!overflow && argnum > 0 && *q == '$') { idx = argnum - 1; p = q + 1; }
3581+ }
35723582 while (*p && sl < sizeof(spec) - 4) {
35733583 char c = *p;
35743584 if (c == '-' || c == '+' || c == ' ' || c == '#' || c == '0' || c == '.' || (c >= '0' && c <= '9')) { spec[sl++] = c; p++; }