diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-23 23:48:24 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | 3dc9bc3a6cefa30c553c6ec21b1545db98e26b6d (patch) | |
tree | 375698480c4f21c48f1f59fed4878a78468dd2a6 /asl | |
parent | a7ebfdedeee84bd01615ad62ac448adae12787db (diff) |
Add float formatting
Diffstat (limited to 'asl')
-rw-r--r-- | asl/BUILD.bazel | 1 | ||||
-rw-r--r-- | asl/format.cpp | 10 | ||||
-rw-r--r-- | asl/format.hpp | 7 | ||||
-rw-r--r-- | asl/format_float.cpp | 21 | ||||
-rw-r--r-- | asl/tests/format_tests.cpp | 13 |
5 files changed, 41 insertions, 11 deletions
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("<FLOAT>"); // @Todo Float formatting
-}
-
-void asl::AslFormat(Formatter& f, double)
-{
- f.write("<DOUBLE>"); // @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<isize_t N>
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 <cstdio>
+
+// @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;
|