summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--MODULE.bazel.lock12
-rw-r--r--asl/BUILD.bazel1
-rw-r--r--asl/format.cpp10
-rw-r--r--asl/format.hpp7
-rw-r--r--asl/format_float.cpp21
-rw-r--r--asl/tests/format_tests.cpp13
6 files changed, 47 insertions, 17 deletions
diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock
index 21b358e..d62a47c 100644
--- a/MODULE.bazel.lock
+++ b/MODULE.bazel.lock
@@ -64,19 +64,19 @@
"@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": {
"general": {
"bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=",
- "usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=",
+ "usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=",
"recordedFileInputs": {},
"recordedDirentsInputs": {},
"envVariables": {},
"generatedRepoSpecs": {
- "local_config_apple_cc": {
+ "local_config_apple_cc_toolchains": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
- "ruleClassName": "_apple_cc_autoconf",
+ "ruleClassName": "_apple_cc_autoconf_toolchains",
"attributes": {}
},
- "local_config_apple_cc_toolchains": {
+ "local_config_apple_cc": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
- "ruleClassName": "_apple_cc_autoconf_toolchains",
+ "ruleClassName": "_apple_cc_autoconf",
"attributes": {}
}
},
@@ -92,7 +92,7 @@
"@@platforms//host:extension.bzl%host_platform": {
"general": {
"bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=",
- "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=",
+ "usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=",
"recordedFileInputs": {},
"recordedDirentsInputs": {},
"envVariables": {},
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;