mathlib.h: replaced slow `std::ostringstream` with `std::to_string()` in `MathLib::toString()` (#4382)

This commit is contained in:
Oliver Stöneberg 2022-08-19 20:44:24 +02:00 committed by GitHub
parent 80a486dda0
commit f138df2909
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 8 deletions

View File

@ -23,7 +23,6 @@
#include "config.h"
#include <sstream>
#include <string>
/// @addtogroup Core
@ -74,9 +73,7 @@ public:
static biguint toULongNumber(const std::string & str);
template<class T> static std::string toString(T value) {
std::ostringstream result;
result << value;
return result.str();
return std::to_string(value);
}
static double toDoubleNumber(const std::string & str);

View File

@ -1217,11 +1217,19 @@ private:
ASSERT_EQUALS("0.0", MathLib::toString(+0.0));
ASSERT_EQUALS("0.0", MathLib::toString(-0.0));
// float (trailing f or F)
ASSERT_EQUALS("0", MathLib::toString(+0.0f));
ASSERT_EQUALS("-0", MathLib::toString(-0.0F));
ASSERT_EQUALS("0.000000", MathLib::toString(0.0f));
ASSERT_EQUALS("0.000000", MathLib::toString(+0.0f));
ASSERT_EQUALS("-0.000000", MathLib::toString(-0.0f));
ASSERT_EQUALS("0.000000", MathLib::toString(0.0F));
ASSERT_EQUALS("0.000000", MathLib::toString(+0.0F));
ASSERT_EQUALS("-0.000000", MathLib::toString(-0.0F));
// double (tailing l or L)
ASSERT_EQUALS("0", MathLib::toString(+0.0l));
ASSERT_EQUALS("-0", MathLib::toString(-0.0L));
ASSERT_EQUALS("0.000000", MathLib::toString(0.0l));
ASSERT_EQUALS("0.000000", MathLib::toString(+0.0l));
ASSERT_EQUALS("-0.000000", MathLib::toString(-0.0l));
ASSERT_EQUALS("0.000000", MathLib::toString(0.0L));
ASSERT_EQUALS("0.000000", MathLib::toString(+0.0L));
ASSERT_EQUALS("-0.000000", MathLib::toString(-0.0L));
}
void CPP14DigitSeparators() const { // Ticket #7137, #7565