Commit 5d9e4b62 by Simon Gene Gottlieb Committed by Jesse Beder

patch: split fp_to_string.h into fptostring.h and fptostring.cpp

parent 28c0a1bc
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include "yaml-cpp/emittermanip.h" #include "yaml-cpp/emittermanip.h"
#include "yaml-cpp/null.h" #include "yaml-cpp/null.h"
#include "yaml-cpp/ostream_wrapper.h" #include "yaml-cpp/ostream_wrapper.h"
#include "yaml-cpp/fp_to_string.h" #include "yaml-cpp/fptostring.h"
namespace YAML { namespace YAML {
class Binary; class Binary;
......
// 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 <array>
#include <cassert>
#include <cmath>
#include <sstream>
#include <tuple>
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
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include "yaml-cpp/node/node.h" #include "yaml-cpp/node/node.h"
#include "yaml-cpp/node/type.h" #include "yaml-cpp/node/type.h"
#include "yaml-cpp/null.h" #include "yaml-cpp/null.h"
#include "yaml-cpp/fp_to_string.h" #include "yaml-cpp/fptostring.h"
namespace YAML { namespace YAML {
......
// SPDX-FileCopyrightText: 2024 Simon Gene Gottlieb // SPDX-FileCopyrightText: 2024 Simon Gene Gottlieb
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
#ifndef YAML_H_FP_TO_STRING
#define YAML_H_FP_TO_STRING
#include "contrib/dragonbox.h" #include "contrib/dragonbox.h"
#include <array> #include <array>
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
#include <limits>
#include <sstream> #include <sstream>
#include <tuple> #include <tuple>
...@@ -32,7 +30,7 @@ namespace fp_formatting { ...@@ -32,7 +30,7 @@ namespace fp_formatting {
* assert(buffer[1] == '2'); * assert(buffer[1] == '2');
* assert(buffer[2] == '3'); * 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(width >= 1);
assert(end >= begin); // end must be after begin assert(end >= begin); // end must be after begin
assert(end-begin >= width); // Buffer must be large enough assert(end-begin >= width); // Buffer must be large enough
...@@ -184,24 +182,26 @@ std::string FpToString(T v, int precision = 0) { ...@@ -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); 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); return detail::fp_formatting::FpToString(v, precision);
} }
/** /**
* dragonbox only works for floats/doubles not long double * dragonbox only works for floats/doubles not long double
*/ */
inline std::string FpToString(long double v, size_t precision = std::numeric_limits<long double>::max_digits10) { std::string FpToString(long double v, size_t precision) {
std::stringstream ss; std::stringstream ss;
ss.imbue(std::locale("C")); ss.imbue(std::locale("C"));
if (precision == 0) {
precision = std::numeric_limits<long double>::max_digits10;
}
ss.precision(precision); ss.precision(precision);
ss << v; ss << v;
return ss.str(); return ss.str();
} }
} }
#endif
#include "yaml-cpp/fp_to_string.h" #include "yaml-cpp/fptostring.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
namespace YAML { namespace YAML {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment