diff --git a/include/yaml-cpp/emitter.h b/include/yaml-cpp/emitter.h index 3756be1..41675fa 100644 --- a/include/yaml-cpp/emitter.h +++ b/include/yaml-cpp/emitter.h @@ -21,7 +21,7 @@ #include "yaml-cpp/emittermanip.h" #include "yaml-cpp/null.h" #include "yaml-cpp/ostream_wrapper.h" -#include "yaml-cpp/fp_to_string.h" +#include "yaml-cpp/fptostring.h" namespace YAML { class Binary; diff --git a/include/yaml-cpp/fptostring.h b/include/yaml-cpp/fptostring.h new file mode 100644 index 0000000..ace79bf --- /dev/null +++ b/include/yaml-cpp/fptostring.h @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: 2024 Simon Gene Gottlieb +// SPDX-License-Identifier: MIT + +#ifndef YAML_H_FP_TO_STRING +#define YAML_H_FP_TO_STRING + +#include "contrib/dragonbox.h" + +#include +#include +#include +#include +#include + +namespace YAML { +// "precision = 0" refers to shortest known unique representation of the value +std::string FpToString(float v, size_t precision = 0); +std::string FpToString(double v, size_t precision = 0); +std::string FpToString(long double v, size_t precision = 0); +} + +#endif diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 3761f79..9ccbf53 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -28,7 +28,7 @@ #include "yaml-cpp/node/node.h" #include "yaml-cpp/node/type.h" #include "yaml-cpp/null.h" -#include "yaml-cpp/fp_to_string.h" +#include "yaml-cpp/fptostring.h" namespace YAML { diff --git a/include/yaml-cpp/fp_to_string.h b/src/fptostring.cpp similarity index 93% rename from include/yaml-cpp/fp_to_string.h rename to src/fptostring.cpp index 380c01f..d12bbc0 100644 --- a/include/yaml-cpp/fp_to_string.h +++ b/src/fptostring.cpp @@ -1,14 +1,12 @@ // SPDX-FileCopyrightText: 2024 Simon Gene Gottlieb // SPDX-License-Identifier: MIT -#ifndef YAML_H_FP_TO_STRING -#define YAML_H_FP_TO_STRING - #include "contrib/dragonbox.h" #include #include #include +#include #include #include @@ -32,7 +30,7 @@ namespace fp_formatting { * assert(buffer[1] == '2'); * assert(buffer[2] == '3'); */ -inline int ConvertToChars(char* begin, char* end, size_t value, int width=1) { +int ConvertToChars(char* begin, char* end, size_t value, int width=1) { assert(width >= 1); assert(end >= begin); // end must be after begin assert(end-begin >= width); // Buffer must be large enough @@ -184,24 +182,26 @@ std::string FpToString(T v, int precision = 0) { } } -inline std::string FpToString(float v, size_t precision = 0) { +std::string FpToString(float v, size_t precision) { return detail::fp_formatting::FpToString(v, precision); } -inline std::string FpToString(double v, size_t precision = 0) { +std::string FpToString(double v, size_t precision) { return detail::fp_formatting::FpToString(v, precision); } /** * dragonbox only works for floats/doubles not long double */ -inline std::string FpToString(long double v, size_t precision = std::numeric_limits::max_digits10) { +std::string FpToString(long double v, size_t precision) { std::stringstream ss; ss.imbue(std::locale("C")); + if (precision == 0) { + precision = std::numeric_limits::max_digits10; + } ss.precision(precision); ss << v; return ss.str(); } } -#endif diff --git a/test/fp_to_string_test.cpp b/test/fptostring_test.cpp similarity index 99% rename from test/fp_to_string_test.cpp rename to test/fptostring_test.cpp index 416cdfd..3f377ab 100644 --- a/test/fp_to_string_test.cpp +++ b/test/fptostring_test.cpp @@ -1,4 +1,4 @@ -#include "yaml-cpp/fp_to_string.h" +#include "yaml-cpp/fptostring.h" #include "gtest/gtest.h" namespace YAML {