From f138df2909e81d246f51e0e2466fa09e327b4d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Fri, 19 Aug 2022 20:44:24 +0200 Subject: [PATCH] mathlib.h: replaced slow `std::ostringstream` with `std::to_string()` in `MathLib::toString()` (#4382) --- lib/mathlib.h | 5 +---- test/testmathlib.cpp | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/mathlib.h b/lib/mathlib.h index d072170a7..8e0e790f4 100644 --- a/lib/mathlib.h +++ b/lib/mathlib.h @@ -23,7 +23,6 @@ #include "config.h" -#include #include /// @addtogroup Core @@ -74,9 +73,7 @@ public: static biguint toULongNumber(const std::string & str); template 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); diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 07d0fd622..5f81b5587 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -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