mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-08 12:21:17 +00:00
patch: split fp_to_string.h into fptostring.h and fptostring.cpp
This commit is contained in:

committed by
Jesse Beder

parent
28c0a1bc25
commit
5d9e4b6251
@@ -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;
|
||||||
|
22
include/yaml-cpp/fptostring.h
Normal file
22
include/yaml-cpp/fptostring.h
Normal file
@@ -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 <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 @@
|
|||||||
#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 {
|
||||||
|
@@ -1,14 +1,12 @@
|
|||||||
// 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 {
|
|||||||
* 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) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
@@ -1,4 +1,4 @@
|
|||||||
#include "yaml-cpp/fp_to_string.h"
|
#include "yaml-cpp/fptostring.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
Reference in New Issue
Block a user