Commit b498c6ba

b498c6baea03Merged

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

Description

Support positional arguments in sprintf/% formatting

Details

Commit
b498c6baea03
Parent
89e3aee06af0

lib/sp_runtime.hmodifiedView file

@@ -3569,6 +3569,12 @@
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;
3575+ while (*q >= '0' && *q <= '9') { argnum = argnum * 10 + (*q - '0'); q++; }
3576+ if (argnum > 0 && *q == '$') { idx = argnum - 1; p = q + 1; }
3577+ }
35723578 while (*p && sl < sizeof(spec) - 4) {
35733579 char c = *p;
35743580 if (c == '-' || c == '+' || c == ' ' || c == '#' || c == '0' || c == '.' || (c >= '0' && c <= '9')) { spec[sl++] = c; p++; }