Add float formatting

This commit is contained in:
2024-11-23 23:48:24 +01:00
parent a7ebfdedee
commit 3dc9bc3a6c
6 changed files with 48 additions and 18 deletions

14
MODULE.bazel.lock generated
View File

@ -64,20 +64,20 @@
"@@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": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf",
"attributes": {}
},
"local_config_apple_cc_toolchains": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf_toolchains",
"attributes": {}
},
"local_config_apple_cc": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf",
"attributes": {}
}
},
"recordedRepoMappingEntries": [
@ -92,7 +92,7 @@
"@@platforms//host:extension.bzl%host_platform": {
"general": {
"bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=",
"usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=",
"usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=",
"recordedFileInputs": {},
"recordedDirentsInputs": {},
"envVariables": {},

View File

@ -23,6 +23,7 @@ cc_library(
srcs = [
"allocator.cpp",
"format.cpp",
"format_float.cpp",
"print.cpp",
],
visibility = ["//visibility:public"],

View File

@ -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)

View File

@ -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);

21
asl/format_float.cpp Normal file
View File

@ -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));
}

View File

@ -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;