diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-28 23:53:55 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | 27c3969e69f5525ccb0bc462a615a14142b5f178 (patch) | |
tree | a11ebc28cb6c2609d5e04913f63d5c1ba1181e0e /asl/format.cpp | |
parent | 3dc9bc3a6cefa30c553c6ec21b1545db98e26b6d (diff) |
Add float utilities, & float formatting with dragonbox
Diffstat (limited to 'asl/format.cpp')
-rw-r--r-- | asl/format.cpp | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/asl/format.cpp b/asl/format.cpp index 6a3e5de..1371431 100644 --- a/asl/format.cpp +++ b/asl/format.cpp @@ -106,7 +106,9 @@ void asl::AslFormat(Formatter& f, uint32_t v) AslFormat(f, static_cast<uint64_t>(v));
}
-void asl::AslFormat(Formatter& f, uint64_t v)
+static constexpr int32_t kMaxUint64Digits = 20;
+
+asl::string_view asl::format_uint64(uint64_t v, asl::span<char, kMaxUint64Digits> buffer)
{
static constexpr char s_pairs_storage[] = {
'0', '0', '0', '1', '0', '2', '0', '3', '0', '4',
@@ -132,10 +134,7 @@ void asl::AslFormat(Formatter& f, uint64_t v) };
static constexpr span s_pairs = s_pairs_storage;
-
- static constexpr int32_t kMaxDigits = 20;
- char buffer[kMaxDigits];
- int32_t cursor = kMaxDigits;
+ int32_t cursor = kMaxUint64Digits;
auto write_two = [&buffer, &cursor](span<const char, 2> str)
{
@@ -161,13 +160,19 @@ void asl::AslFormat(Formatter& f, uint64_t v) {
write_two(s_pairs.subspan(static_cast<isize_t>(v * 2)).first<2>());
}
- else if (v > 0 || cursor == kMaxDigits)
+ else if (v > 0 || cursor == kMaxUint64Digits)
{
ASL_ASSERT(v < 10);
write_one(static_cast<char>('0' + v));
}
- f.write(string_view(buffer, kMaxDigits).substr(cursor));
+ return string_view(buffer.data(), kMaxUint64Digits).substr(cursor);
+}
+
+void asl::AslFormat(Formatter& f, uint64_t v)
+{
+ char buffer[kMaxUint64Digits];
+ f.write(format_uint64(v, buffer));
}
void asl::AslFormat(Formatter& f, int8_t v)
|