Skip to content

Commit

Permalink
feat: implemented support for formatting decimal numbers to strings.
Browse files Browse the repository at this point in the history
Closes #30

Note: This implementation may require future improvements.
  • Loading branch information
jacopodl committed Sep 25, 2023
1 parent f7be205 commit 9529e38
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions argon/vm/datatype/stringformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,28 @@ int StringFormatter::FormatChar() {
}

int StringFormatter::FormatDecimal(unsigned char op) {
// TODO: format decimal
assert(false);
return 0;
// TODO: Improve the implementation so that a call to snprintf is no longer necessary.

char fmt[3]{};

fmt[0] = '%';
fmt[1] = (char) op;
fmt[2] = '\0';

const auto *decimal = (Decimal *) this->NextArg();

auto len = snprintf(nullptr, 0, (const char *) fmt, decimal->decimal);

if (!this->BufferResize(len))
return -1;

snprintf((char *) this->output_.cursor, len, (const char *) fmt, decimal->decimal);

len -= 1; // ignore '\0'

this->output_.cursor += len;

return len;
}

int StringFormatter::FormatInteger(int base, bool unsign, bool upper) {
Expand Down

0 comments on commit 9529e38

Please sign in to comment.