From 3dc9bc3a6cefa30c553c6ec21b1545db98e26b6d Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sat, 23 Nov 2024 23:48:24 +0100 Subject: Add float formatting --- asl/BUILD.bazel | 1 + asl/format.cpp | 10 ---------- asl/format.hpp | 7 ++++++- asl/format_float.cpp | 21 +++++++++++++++++++++ asl/tests/format_tests.cpp | 13 +++++++++++++ 5 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 asl/format_float.cpp (limited to 'asl') diff --git a/asl/BUILD.bazel b/asl/BUILD.bazel index 20a1651..b9875ab 100644 --- a/asl/BUILD.bazel +++ b/asl/BUILD.bazel @@ -23,6 +23,7 @@ cc_library( srcs = [ "allocator.cpp", "format.cpp", + "format_float.cpp", "print.cpp", ], visibility = ["//visibility:public"], diff --git a/asl/format.cpp b/asl/format.cpp index 461fc77..6a3e5de 100644 --- a/asl/format.cpp +++ b/asl/format.cpp @@ -79,16 +79,6 @@ void asl::AslFormat(Formatter& f, const char* str) f.write({str, asl::strlen(str)}); } -void asl::AslFormat(Formatter& f, float) -{ - f.write(""); // @Todo Float formatting -} - -void asl::AslFormat(Formatter& f, double) -{ - f.write(""); // @Todo Float formatting -} - void asl::AslFormat(Formatter& f, bool v) { if (v) diff --git a/asl/format.hpp b/asl/format.hpp index 7cf3917..9504701 100644 --- a/asl/format.hpp +++ b/asl/format.hpp @@ -77,11 +77,16 @@ void format(Writer* w, string_view fmt, const Args&... args) template void AslFormat(Formatter& f, const char (&str)[N]) { - f.write(str, N - 1); + f.write(string_view(str, N - 1)); } void AslFormat(Formatter& f, const char* str); +inline void AslFormat(Formatter& f, string_view sv) +{ + f.write(sv); +} + void AslFormat(Formatter& f, float); void AslFormat(Formatter& f, double); diff --git a/asl/format_float.cpp b/asl/format_float.cpp new file mode 100644 index 0000000..425586f --- /dev/null +++ b/asl/format_float.cpp @@ -0,0 +1,21 @@ +#include "asl/format.hpp" + +#include + +// @Todo Use something like ryu or dragonbox + +void asl::AslFormat(Formatter& f, float value) +{ + static constexpr isize_t kBufferLength = 64; + char buffer[kBufferLength]; + int output_length = snprintf(buffer, kBufferLength, "%f", (double)value); + f.write(string_view(buffer, output_length)); +} + +void asl::AslFormat(Formatter& f, double value) +{ + static constexpr isize_t kBufferLength = 64; + char buffer[kBufferLength]; + int output_length = snprintf(buffer, kBufferLength, "%f", value); + f.write(string_view(buffer, output_length)); +} diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp index a8c1ad6..dd3db1c 100644 --- a/asl/tests/format_tests.cpp +++ b/asl/tests/format_tests.cpp @@ -104,6 +104,19 @@ ASL_TEST(format_integers) ASL_TEST_EXPECT(sink.str() == "-1 -23 -456 -7890"_sv); } +ASL_TEST(format_floats) +{ + StringSink sink; + + sink.reset(); + asl::format(&sink, "{} {} {}", 0.0F, 1.0, 2.0F); + ASL_TEST_EXPECT(sink.str() == "0.000000 1.000000 2.000000"_sv); + + sink.reset(); + asl::format(&sink, "{} {}", 10.25F, -22.3); + ASL_TEST_EXPECT(sink.str() == "10.250000 -22.300000"_sv); +} + ASL_TEST(format_boolean) { StringSink sink; -- cgit