Add float formatting
This commit is contained in:
14
MODULE.bazel.lock
generated
14
MODULE.bazel.lock
generated
@ -64,20 +64,20 @@
|
|||||||
"@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": {
|
"@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": {
|
||||||
"general": {
|
"general": {
|
||||||
"bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=",
|
"bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=",
|
||||||
"usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=",
|
"usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=",
|
||||||
"recordedFileInputs": {},
|
"recordedFileInputs": {},
|
||||||
"recordedDirentsInputs": {},
|
"recordedDirentsInputs": {},
|
||||||
"envVariables": {},
|
"envVariables": {},
|
||||||
"generatedRepoSpecs": {
|
"generatedRepoSpecs": {
|
||||||
"local_config_apple_cc": {
|
|
||||||
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
|
|
||||||
"ruleClassName": "_apple_cc_autoconf",
|
|
||||||
"attributes": {}
|
|
||||||
},
|
|
||||||
"local_config_apple_cc_toolchains": {
|
"local_config_apple_cc_toolchains": {
|
||||||
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
|
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
|
||||||
"ruleClassName": "_apple_cc_autoconf_toolchains",
|
"ruleClassName": "_apple_cc_autoconf_toolchains",
|
||||||
"attributes": {}
|
"attributes": {}
|
||||||
|
},
|
||||||
|
"local_config_apple_cc": {
|
||||||
|
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
|
||||||
|
"ruleClassName": "_apple_cc_autoconf",
|
||||||
|
"attributes": {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"recordedRepoMappingEntries": [
|
"recordedRepoMappingEntries": [
|
||||||
@ -92,7 +92,7 @@
|
|||||||
"@@platforms//host:extension.bzl%host_platform": {
|
"@@platforms//host:extension.bzl%host_platform": {
|
||||||
"general": {
|
"general": {
|
||||||
"bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=",
|
"bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=",
|
||||||
"usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=",
|
"usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=",
|
||||||
"recordedFileInputs": {},
|
"recordedFileInputs": {},
|
||||||
"recordedDirentsInputs": {},
|
"recordedDirentsInputs": {},
|
||||||
"envVariables": {},
|
"envVariables": {},
|
||||||
|
@ -23,6 +23,7 @@ cc_library(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"allocator.cpp",
|
"allocator.cpp",
|
||||||
"format.cpp",
|
"format.cpp",
|
||||||
|
"format_float.cpp",
|
||||||
"print.cpp",
|
"print.cpp",
|
||||||
],
|
],
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
|
@ -79,16 +79,6 @@ void asl::AslFormat(Formatter& f, const char* str)
|
|||||||
f.write({str, asl::strlen(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)
|
void asl::AslFormat(Formatter& f, bool v)
|
||||||
{
|
{
|
||||||
if (v)
|
if (v)
|
||||||
|
@ -77,11 +77,16 @@ void format(Writer* w, string_view fmt, const Args&... args)
|
|||||||
template<isize_t N>
|
template<isize_t N>
|
||||||
void AslFormat(Formatter& f, const char (&str)[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);
|
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, float);
|
||||||
void AslFormat(Formatter& f, double);
|
void AslFormat(Formatter& f, double);
|
||||||
|
|
||||||
|
21
asl/format_float.cpp
Normal file
21
asl/format_float.cpp
Normal 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));
|
||||||
|
}
|
@ -104,6 +104,19 @@ ASL_TEST(format_integers)
|
|||||||
ASL_TEST_EXPECT(sink.str() == "-1 -23 -456 -7890"_sv);
|
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)
|
ASL_TEST(format_boolean)
|
||||||
{
|
{
|
||||||
StringSink sink;
|
StringSink sink;
|
||||||
|
Reference in New Issue
Block a user