Refactor string-formatting routines to append to an existing string

This commit is contained in:
ISSOtm
2024-03-21 20:21:51 -04:00
committed by Sylvie
parent 52e8e1f9fc
commit 412073774c
4 changed files with 16 additions and 24 deletions

View File

@@ -37,8 +37,8 @@ public:
void useCharacter(int c); void useCharacter(int c);
void finishCharacters(); void finishCharacters();
std::string formatString(std::string const &value) const; void appendString(std::string &str, std::string const &value) const;
std::string formatNumber(uint32_t value) const; void appendNumber(std::string &str, uint32_t value) const;
}; };
#endif // RGBDS_FORMAT_SPEC_H #endif // RGBDS_FORMAT_SPEC_H

View File

@@ -3,9 +3,7 @@
#include "asm/format.hpp" #include "asm/format.hpp"
#include <algorithm> #include <algorithm>
#include <assert.h>
#include <inttypes.h> #include <inttypes.h>
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -105,7 +103,7 @@ void FormatSpec::finishCharacters() {
state = FORMAT_INVALID; state = FORMAT_INVALID;
} }
std::string FormatSpec::formatString(std::string const &value) const { void FormatSpec::appendString(std::string &str, std::string const &value) const {
int useType = type; int useType = type;
if (isEmpty()) { if (isEmpty()) {
// No format was specified // No format was specified
@@ -127,8 +125,7 @@ std::string FormatSpec::formatString(std::string const &value) const {
size_t totalLen = width > valueLen ? width : valueLen; size_t totalLen = width > valueLen ? width : valueLen;
size_t padLen = totalLen - valueLen; size_t padLen = totalLen - valueLen;
std::string str; str.reserve(str.length() + totalLen);
str.reserve(totalLen);
if (alignLeft) { if (alignLeft) {
str.append(value); str.append(value);
str.append(padLen, ' '); str.append(padLen, ' ');
@@ -141,11 +138,9 @@ std::string FormatSpec::formatString(std::string const &value) const {
error("Formatted string value too long\n"); error("Formatted string value too long\n");
str.resize(MAXSTRLEN); str.resize(MAXSTRLEN);
} }
return str;
} }
std::string FormatSpec::formatNumber(uint32_t value) const { void FormatSpec::appendNumber(std::string &str, uint32_t value) const {
int useType = type; int useType = type;
bool usePrefix = prefix; bool usePrefix = prefix;
if (isEmpty()) { if (isEmpty()) {
@@ -224,8 +219,7 @@ std::string FormatSpec::formatNumber(uint32_t value) const {
size_t totalLen = width > numLen ? width : numLen; size_t totalLen = width > numLen ? width : numLen;
size_t padLen = totalLen - numLen; size_t padLen = totalLen - numLen;
std::string str; str.reserve(str.length() + totalLen);
str.reserve(totalLen);
if (alignLeft) { if (alignLeft) {
if (signChar) if (signChar)
str += signChar; str += signChar;
@@ -256,6 +250,4 @@ std::string FormatSpec::formatNumber(uint32_t value) const {
error("Formatted numeric value too long\n"); error("Formatted numeric value too long\n");
str.resize(MAXSTRLEN); str.resize(MAXSTRLEN);
} }
return str;
} }

View File

@@ -1170,20 +1170,20 @@ static char const *readInterpolation(size_t depth) {
// Don't return before `lexerState->disableInterpolation` is reset! // Don't return before `lexerState->disableInterpolation` is reset!
lexerState->disableInterpolation = disableInterpolation; lexerState->disableInterpolation = disableInterpolation;
static char buf[MAXSTRLEN + 1];
Symbol const *sym = sym_FindScopedValidSymbol(fmtBuf); Symbol const *sym = sym_FindScopedValidSymbol(fmtBuf);
if (!sym) { if (!sym) {
error("Interpolated symbol \"%s\" does not exist\n", fmtBuf.c_str()); error("Interpolated symbol \"%s\" does not exist\n", fmtBuf.c_str());
} else if (sym->type == SYM_EQUS) { } else if (sym->type == SYM_EQUS) {
std::string str = fmt.formatString(*sym->getEqus()); static std::string buf;
memcpy(buf, str.c_str(), str.length() + 1); buf.clear();
return buf; fmt.appendString(buf, *sym->getEqus());
return buf.c_str();
} else if (sym->isNumeric()) { } else if (sym->isNumeric()) {
std::string str = fmt.formatNumber(sym->getConstantValue()); static std::string buf;
memcpy(buf, str.c_str(), str.length() + 1); buf.clear();
return buf; fmt.appendNumber(buf, sym->getConstantValue());
return buf.c_str();
} else { } else {
error("Only numerical and string symbols can be interpolated\n"); error("Only numerical and string symbols can be interpolated\n");
} }

View File

@@ -2696,11 +2696,11 @@ static std::string strfmt(
// Will warn after formatting is done. // Will warn after formatting is done.
str += '%'; str += '%';
} else if (auto *n = std::get_if<uint32_t>(&args[argIndex]); n) { } else if (auto *n = std::get_if<uint32_t>(&args[argIndex]); n) {
str.append(fmt.formatNumber(*n)); fmt.appendNumber(str, *n);
} else { } else {
assert(std::holds_alternative<std::string>(args[argIndex])); assert(std::holds_alternative<std::string>(args[argIndex]));
auto &s = std::get<std::string>(args[argIndex]); auto &s = std::get<std::string>(args[argIndex]);
str.append(fmt.formatString(s)); fmt.appendString(str, s);
} }
argIndex++; argIndex++;