Simplify format specs to not use a per-character state machine

This commit is contained in:
Rangi42
2025-08-30 12:23:01 -04:00
parent 531278961f
commit 0ccdbf509a
6 changed files with 90 additions and 151 deletions

View File

@@ -551,40 +551,30 @@ std::string act_StringFormat(
std::string str;
size_t argIndex = 0;
for (size_t i = 0; spec[i] != '\0'; ++i) {
int c = spec[i];
if (c != '%') {
for (size_t i = 0; spec[i] != '\0';) {
if (int c = spec[i]; c != '%') {
str += c;
++i;
continue;
}
c = spec[++i];
if (c == '%') {
if (int c = spec[++i]; c == '%') {
str += c;
++i;
continue;
}
FormatSpec fmt{};
while (c != '\0') {
fmt.useCharacter(c);
if (fmt.isFinished()) {
break;
}
c = spec[++i];
}
if (fmt.isEmpty()) {
} else if (c == '\0') {
error("STRFMT: Illegal '%%' at end of format string");
str += '%';
break;
}
FormatSpec fmt{};
size_t n = fmt.parseSpec(spec.c_str() + i);
i += n;
if (!fmt.isValid()) {
error("STRFMT: Invalid format spec for argument %zu", argIndex + 1);
str += '%';
str += spec.substr(i - n - 1, n + 1); // include the '%'
} else if (argIndex >= args.size()) {
// Will warn after formatting is done.
str += '%';