diff options
22 files changed, 168 insertions, 83 deletions
diff --git a/basegfx/source/tools/stringconversiontools.cxx b/basegfx/source/tools/stringconversiontools.cxx index d6a702faa687..3a671ae539f0 100644 --- a/basegfx/source/tools/stringconversiontools.cxx +++ b/basegfx/source/tools/stringconversiontools.cxx @@ -110,7 +110,7 @@ namespace basegfx::internal if(sNumberString.getLength()) { rtl_math_ConversionStatus eStatus; - o_fRetval = ::rtl::math::stringToDouble( sNumberString.makeStringAndClear(), + o_fRetval = ::rtl::math::stringToDouble( sNumberString, '.', ',', &eStatus ); diff --git a/basic/source/sbx/sbxscan.cxx b/basic/source/sbx/sbxscan.cxx index ae6d50b9cfdf..82080df4a62a 100644 --- a/basic/source/sbx/sbxscan.cxx +++ b/basic/source/sbx/sbxscan.cxx @@ -172,11 +172,10 @@ ErrCode ImpScan( const OUString& rWSrc, double& nVal, SbxDataType& rType, if( decsep > 1 || exp > 1 ) bRes = false; - OUString aBufStr( aBuf.makeStringAndClear()); rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok; sal_Int32 nParseEnd = 0; - nVal = rtl::math::stringToDouble( aBufStr, cIntntlDecSep, cIntntlGrpSep, &eStatus, &nParseEnd ); - if( eStatus != rtl_math_ConversionStatus_Ok || nParseEnd != aBufStr.getLength() ) + nVal = rtl::math::stringToDouble( aBuf, cIntntlDecSep, cIntntlGrpSep, &eStatus, &nParseEnd ); + if( eStatus != rtl_math_ConversionStatus_Ok || nParseEnd != aBuf.getLength() ) bRes = false; if( !decsep && !exp ) diff --git a/chart2/source/inc/CommonFunctors.hxx b/chart2/source/inc/CommonFunctors.hxx index 0340c82b692e..4abdcc168ae6 100644 --- a/chart2/source/inc/CommonFunctors.hxx +++ b/chart2/source/inc/CommonFunctors.hxx @@ -91,7 +91,7 @@ struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString */ struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble { - double operator() ( const OUString & rStr ) + double operator() ( std::u16string_view rStr ) { rtl_math_ConversionStatus eConversionStatus; double fResult = ::rtl::math::stringToDouble( rStr, '.', ',', & eConversionStatus ); diff --git a/connectivity/source/drivers/dbase/DTable.cxx b/connectivity/source/drivers/dbase/DTable.cxx index eedba74a57cd..c157e795e72b 100644 --- a/connectivity/source/drivers/dbase/DTable.cxx +++ b/connectivity/source/drivers/dbase/DTable.cxx @@ -1648,7 +1648,7 @@ Reference<XPropertySet> ODbaseTable::isUniqueByColumnName(sal_Int32 _nColumnPos) return Reference<XPropertySet>(); } -static double toDouble(const OString& rString) +static double toDouble(std::string_view rString) { return ::rtl::math::stringToDouble( rString, '.', ',' ); } diff --git a/include/rtl/math.hxx b/include/rtl/math.hxx index 7da30df6832b..661ddf1d131d 100644 --- a/include/rtl/math.hxx +++ b/include/rtl/math.hxx @@ -173,6 +173,23 @@ inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue, /** A wrapper around rtl_math_stringToDouble. */ +#ifdef LIBO_INTERNAL_ONLY +inline double stringToDouble(std::string_view aString, + char cDecSeparator, char cGroupSeparator, + rtl_math_ConversionStatus * pStatus = NULL, + sal_Int32 * pParsedEnd = NULL) +{ + char const * pBegin = aString.data(); + char const * pEnd; + double fResult = rtl_math_stringToDouble(pBegin, + pBegin + aString.size(), + cDecSeparator, cGroupSeparator, + pStatus, &pEnd); + if (pParsedEnd != NULL) + *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin); + return fResult; +} +#else inline double stringToDouble(rtl::OString const & rString, char cDecSeparator, char cGroupSeparator, rtl_math_ConversionStatus * pStatus = NULL, @@ -188,9 +205,29 @@ inline double stringToDouble(rtl::OString const & rString, *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin); return fResult; } +#endif + /** A wrapper around rtl_math_uStringToDouble. */ +#ifdef LIBO_INTERNAL_ONLY +inline double stringToDouble(std::u16string_view aString, + sal_Unicode cDecSeparator, + sal_Unicode cGroupSeparator, + rtl_math_ConversionStatus * pStatus = NULL, + sal_Int32 * pParsedEnd = NULL) +{ + sal_Unicode const * pBegin = aString.data(); + sal_Unicode const * pEnd; + double fResult = rtl_math_uStringToDouble(pBegin, + pBegin + aString.size(), + cDecSeparator, cGroupSeparator, + pStatus, &pEnd); + if (pParsedEnd != NULL) + *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin); + return fResult; +} +#else inline double stringToDouble(rtl::OUString const & rString, sal_Unicode cDecSeparator, sal_Unicode cGroupSeparator, @@ -207,6 +244,7 @@ inline double stringToDouble(rtl::OUString const & rString, *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin); return fResult; } +#endif /** A wrapper around rtl_math_round. */ diff --git a/include/sax/tools/converter.hxx b/include/sax/tools/converter.hxx index f435244a9504..c4391a7893fd 100644 --- a/include/sax/tools/converter.hxx +++ b/include/sax/tools/converter.hxx @@ -27,6 +27,7 @@ #include <sax/saxdllapi.h> +#include <rtl/strbuf.hxx> #include <sal/types.h> #include <rtl/ustrbuf.hxx> #include <com/sun/star/util/MeasureUnit.hpp> @@ -184,6 +185,13 @@ public: sal_Int16 nSourceUnit, sal_Int16 nTargetUnit ); + /** convert string to double number (using ::rtl::math) and DO convert from + source unit to target unit. */ + static bool convertDouble( double& rValue, + std::string_view rString, + sal_Int16 nSourceUnit, + sal_Int16 nTargetUnit ); + /** convert string to double number (using ::rtl::math) without unit conversion */ static bool convertDouble(double& rValue, std::u16string_view rString); @@ -281,6 +289,7 @@ public: sal_Int32 nPos ); static double GetConversionFactor(OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit); + static double GetConversionFactor(OStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit); static sal_Int16 GetUnitFromString(std::u16string_view rString, sal_Int16 nDefaultUnit); static sal_Int16 GetUnitFromString(std::string_view rString, sal_Int16 nDefaultUnit); diff --git a/include/unotools/localedatawrapper.hxx b/include/unotools/localedatawrapper.hxx index 2b77e8d94d30..6c2502c2a786 100644 --- a/include/unotools/localedatawrapper.hxx +++ b/include/unotools/localedatawrapper.hxx @@ -252,7 +252,7 @@ public: rtl::math::stringToDouble() does. The caller is responsible for proper error checking and end comparison. - @param rString + @param aString The string to parse as floating point number. @param bUseGroupSep Whether group separator is used/accepted during parsing. @@ -264,7 +264,7 @@ public: rtl::math::stringToDouble(). @return The floating point number as parsed. */ - double stringToDouble( const OUString& rString, bool bUseGroupSep, + double stringToDouble( std::u16string_view aString, bool bUseGroupSep, rtl_math_ConversionStatus* pStatus, sal_Int32* pParseEnd ) const; /** A wrapper around rtl_math_uStringToDouble() using the locale dependent diff --git a/include/xmloff/xmluconv.hxx b/include/xmloff/xmluconv.hxx index 38c84270635d..cd05b7508811 100644 --- a/include/xmloff/xmluconv.hxx +++ b/include/xmloff/xmluconv.hxx @@ -222,6 +222,9 @@ public: /** convert string to double number (using ::rtl::math) and DO convert. */ bool convertDouble(double& rValue, std::u16string_view rString) const; + /** convert string to double number (using ::rtl::math) and DO convert. */ + bool convertDouble(double& rValue, std::string_view rString) const; + /** get the Null Date of the XModel and set it to the UnitConverter */ bool setNullDate ( const css::uno::Reference <css::frame::XModel>& xModel); diff --git a/oox/source/ppt/timenodelistcontext.cxx b/oox/source/ppt/timenodelistcontext.cxx index 5a6535e314c5..ae35e8e4d7eb 100644 --- a/oox/source/ppt/timenodelistcontext.cxx +++ b/oox/source/ppt/timenodelistcontext.cxx @@ -304,7 +304,7 @@ namespace oox::ppt { } else if (msCommand.startsWith("playFrom")) { - const OUString aMediaTime( msCommand.copy( 9, msCommand.getLength() - 10 ) ); + std::u16string_view aMediaTime( msCommand.subView( 9, msCommand.getLength() - 10 ) ); rtl_math_ConversionStatus eStatus; double fMediaTime = ::rtl::math::stringToDouble( aMediaTime, u'.', u',', &eStatus ); if( eStatus == rtl_math_ConversionStatus_Ok ) diff --git a/oox/source/vml/vmlformatting.cxx b/oox/source/vml/vmlformatting.cxx index f3b29150b1b6..655069842eda 100644 --- a/oox/source/vml/vmlformatting.cxx +++ b/oox/source/vml/vmlformatting.cxx @@ -65,11 +65,11 @@ using ::com::sun::star::drawing::PolygonFlags_CONTROL; namespace { -bool lclExtractDouble( double& orfValue, sal_Int32& ornEndPos, const OUString& rValue ) +bool lclExtractDouble( double& orfValue, sal_Int32& ornEndPos, std::u16string_view aValue ) { // extract the double value and find start position of unit characters rtl_math_ConversionStatus eConvStatus = rtl_math_ConversionStatus_Ok; - orfValue = ::rtl::math::stringToDouble( rValue, '.', '\0', &eConvStatus, &ornEndPos ); + orfValue = ::rtl::math::stringToDouble( aValue, '.', '\0', &eConvStatus, &ornEndPos ); return eConvStatus == rtl_math_ConversionStatus_Ok; } diff --git a/sal/qa/rtl/math/test-rtl-math.cxx b/sal/qa/rtl/math/test-rtl-math.cxx index 94840dbdb7e8..ee4ae55a1a4b 100644 --- a/sal/qa/rtl/math/test-rtl-math.cxx +++ b/sal/qa/rtl/math/test-rtl-math.cxx @@ -54,42 +54,42 @@ public: rtl_math_ConversionStatus status; sal_Int32 end; double res = rtl::math::stringToDouble( - OUString(" +1.E01foo"), + " +1.E01foo", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(RTL_CONSTASCII_LENGTH(" +1.E01"), end); CPPUNIT_ASSERT_EQUAL(10.0, res); res = rtl::math::stringToDouble( - OUString("NaN"), + "NaN", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(3), end); CPPUNIT_ASSERT(std::isnan(res)); res = rtl::math::stringToDouble( - OUString("NaN1.23"), + "NaN1.23", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(3), end); CPPUNIT_ASSERT(std::isnan(res)); res = rtl::math::stringToDouble( - OUString("+NaN"), + "+NaN", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); CPPUNIT_ASSERT_EQUAL(0.0, res); res = rtl::math::stringToDouble( - OUString("-NaN"), + "-NaN", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); CPPUNIT_ASSERT_EQUAL(0.0, res); res = rtl::math::stringToDouble( - OUString("+1.#NAN"), + "+1.#NAN", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(7), end); @@ -97,7 +97,7 @@ public: CPPUNIT_ASSERT(!std::signbit(res)); res = rtl::math::stringToDouble( - OUString("-1.#NAN"), + "-1.#NAN", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(7), end); @@ -105,28 +105,28 @@ public: CPPUNIT_ASSERT(std::signbit(res)); res = rtl::math::stringToDouble( - OUString("INF"), + "INF", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_OutOfRange, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(3), end); CPPUNIT_ASSERT(std::isinf(res)); res = rtl::math::stringToDouble( - OUString("INF1.23"), + "INF1.23", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_OutOfRange, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(3), end); CPPUNIT_ASSERT(std::isinf(res)); res = rtl::math::stringToDouble( - OUString(".5"), + ".5", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); CPPUNIT_ASSERT_EQUAL(0.5, res); res = rtl::math::stringToDouble( - OUString("5."), + "5.", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); @@ -134,7 +134,7 @@ public: // Leading 0 and group separator. res = rtl::math::stringToDouble( - OUString("0,123"), + "0,123", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(5), end); @@ -142,7 +142,7 @@ public: // Leading 0 and two consecutive group separators are none. res = rtl::math::stringToDouble( - OUString("0,,1"), + "0,,1", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(1), end); @@ -150,7 +150,7 @@ public: // Leading 0 and group separator at end is none. res = rtl::math::stringToDouble( - OUString("0,"), + "0,", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(1), end); @@ -158,7 +158,7 @@ public: // Leading 0 and group separator before non-digit is none. res = rtl::math::stringToDouble( - OUString("0,x"), + "0,x", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(1), end); @@ -166,7 +166,7 @@ public: // Trailing group separator is none. res = rtl::math::stringToDouble( - OUString("1,234,"), + "1,234,", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(5), end); @@ -174,7 +174,7 @@ public: // Group separator followed by non-digit is none. res = rtl::math::stringToDouble( - OUString("1,234,x"), + "1,234,x", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(5), end); @@ -182,67 +182,67 @@ public: // Check that the value is the nearest double-precision representation of the decimal 0.0042 // (it was 0.0042000000000000006 instead of 0.0041999999999999997) - res = rtl::math::stringToDouble(OUString("0,0042"), ',', ' ', &status, &end); + res = rtl::math::stringToDouble("0,0042", ',', ' ', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(0.0042, res); // "- 1" is nothing - res = rtl::math::stringToDouble(OUString("- 1"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("- 1", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); CPPUNIT_ASSERT_EQUAL(0.0, res); // "-1E+E" : no exponent - res = rtl::math::stringToDouble(OUString("-1E+E"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("-1E+E", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); CPPUNIT_ASSERT_EQUAL(-1.0, res); // "-0" is negative zero - res = rtl::math::stringToDouble(OUString("-0"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("-0", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); CPPUNIT_ASSERT_EQUAL(0.0, res); CPPUNIT_ASSERT(std::signbit(res)); // Compensating: "0.001E311" is 1E308, not overflow/inf - res = rtl::math::stringToDouble(OUString("0.001E311"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("0.001E311", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(9), end); CPPUNIT_ASSERT_EQUAL(1E308, res); - res = rtl::math::stringToDouble(OUString("1E8589934590"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("1E8589934590", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_OutOfRange, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(12), end); CPPUNIT_ASSERT_EQUAL(std::numeric_limits<double>::infinity(), res); // DBL_MAX and 4 nextafters double fValAfter = DBL_MAX; - res = rtl::math::stringToDouble(OUString("1.7976931348623157e+308"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("1.7976931348623157e+308", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); CPPUNIT_ASSERT_EQUAL(fValAfter, res); fValAfter = std::nextafter( fValAfter, 0); - res = rtl::math::stringToDouble(OUString("1.7976931348623155e+308"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("1.7976931348623155e+308", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); CPPUNIT_ASSERT_EQUAL(fValAfter, res); fValAfter = std::nextafter( fValAfter, 0); - res = rtl::math::stringToDouble(OUString("1.7976931348623153e+308"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("1.7976931348623153e+308", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); CPPUNIT_ASSERT_EQUAL(fValAfter, res); fValAfter = std::nextafter( fValAfter, 0); - res = rtl::math::stringToDouble(OUString("1.7976931348623151e+308"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("1.7976931348623151e+308", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); CPPUNIT_ASSERT_EQUAL(fValAfter, res); fValAfter = std::nextafter( fValAfter, 0); - res = rtl::math::stringToDouble(OUString("1.7976931348623149e+308"), '.', ',', &status, &end); + res = rtl::math::stringToDouble("1.7976931348623149e+308", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); CPPUNIT_ASSERT_EQUAL(fValAfter, res); @@ -252,28 +252,28 @@ public: rtl_math_ConversionStatus status; sal_Int32 end; double res = rtl::math::stringToDouble( - OUString(" +Efoo"), + " +Efoo", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); CPPUNIT_ASSERT_EQUAL(0.0, res); res = rtl::math::stringToDouble( - OUString("."), + ".", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); CPPUNIT_ASSERT_EQUAL(0.0, res); res = rtl::math::stringToDouble( - OUString(" +.Efoo"), + " +.Efoo", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); CPPUNIT_ASSERT_EQUAL(0.0, res); res = rtl::math::stringToDouble( - OUString(" +,.Efoo"), + " +,.Efoo", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); @@ -281,7 +281,7 @@ public: // Leading group separator is none. res = rtl::math::stringToDouble( - OUString(",1234"), + ",1234", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); @@ -292,13 +292,13 @@ public: rtl_math_ConversionStatus status; sal_Int32 end; double res = rtl::math::stringToDouble( - OUString("1e"), + "1e", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(RTL_CONSTASCII_LENGTH("1"), end); CPPUNIT_ASSERT_EQUAL(1.0, res); res = rtl::math::stringToDouble( - OUString("0e"), + "0e", '.', ',', &status, &end); CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(RTL_CONSTASCII_LENGTH("1"), end); diff --git a/sax/source/tools/converter.cxx b/sax/source/tools/converter.cxx index b882ac076cd9..297070a670dc 100644 --- a/sax/source/tools/converter.cxx +++ b/sax/source/tools/converter.cxx @@ -687,6 +687,22 @@ bool Converter::convertDouble(double& rValue, } /** convert string to double number (using ::rtl::math) */ +bool Converter::convertDouble(double& rValue, + std::string_view rString, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit) +{ + if (!convertDouble(rValue, rString)) + return false; + + OStringBuffer sUnit; + // fdo#48969: switch source and target because factor is used to divide! + double const fFactor = + GetConversionFactor(sUnit, nTargetUnit, nSourceUnit); + if(fFactor != 1.0 && fFactor != 0.0) + rValue /= fFactor; + return true; +} + +/** convert string to double number (using ::rtl::math) */ bool Converter::convertDouble(double& rValue, std::u16string_view rString) { rtl_math_ConversionStatus eStatus; @@ -2280,6 +2296,25 @@ double Converter::GetConversionFactor(OUStringBuffer& rUnit, sal_Int16 nSourceUn return fRetval; } +double Converter::GetConversionFactor(OStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit) +{ + double fRetval(1.0); + rUnit.setLength(0); + + + if(nSourceUnit != nTargetUnit) + { + const o3tl::Length eFrom = Measure2O3tlUnit(nSourceUnit); + const o3tl::Length eTo = Measure2O3tlUnit(nTargetUnit); + fRetval = o3tl::convert(1.0, eFrom, eTo); + + if (const auto sUnit = Measure2UnitString(nTargetUnit); sUnit.size() > 0) + rUnit.append(sUnit.data(), sUnit.size()); + } + + return fRetval; +} + template<typename V> static sal_Int16 lcl_GetUnitFromString(V rString, sal_Int16 nDefaultUnit) { diff --git a/sc/source/core/tool/stringutil.cxx b/sc/source/core/tool/stringutil.cxx index 131853a5f162..493f3fdeed93 100644 --- a/sc/source/core/tool/stringutil.cxx +++ b/sc/source/core/tool/stringutil.cxx @@ -191,9 +191,8 @@ bool ScStringUtil::parseSimpleNumber( rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok; sal_Int32 nParseEnd = 0; - OUString aString( aBuf.makeStringAndClear()); - rVal = ::rtl::math::stringToDouble( aString, dsep, gsep, &eStatus, &nParseEnd); - if (eStatus != rtl_math_ConversionStatus_Ok || nParseEnd < aString.getLength()) + rVal = ::rtl::math::stringToDouble( aBuf, dsep, gsep, &eStatus, &nParseEnd); + if (eStatus != rtl_math_ConversionStatus_Ok || nParseEnd < aBuf.getLength()) // Not a valid number or not entire string consumed. return false; @@ -337,9 +336,8 @@ bool ScStringUtil::parseSimpleNumber( rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok; sal_Int32 nParseEnd = 0; - OString aString( aBuf.makeStringAndClear()); - rVal = ::rtl::math::stringToDouble( aString, dsep, gsep, &eStatus, &nParseEnd); - if (eStatus != rtl_math_ConversionStatus_Ok || nParseEnd < aString.getLength()) + rVal = ::rtl::math::stringToDouble( aBuf, dsep, gsep, &eStatus, &nParseEnd); + if (eStatus != rtl_math_ConversionStatus_Ok || nParseEnd < aBuf.getLength()) // Not a valid number or not entire string consumed. return false; diff --git a/sc/source/filter/oox/condformatbuffer.cxx b/sc/source/filter/oox/condformatbuffer.cxx index 13ca11ed9333..dbe8f626fee0 100644 --- a/sc/source/filter/oox/condformatbuffer.cxx +++ b/sc/source/filter/oox/condformatbuffer.cxx @@ -26,6 +26,7 @@ #include <com/sun/star/sheet/ConditionOperator2.hpp> #include <sal/log.hxx> #include <osl/diagnose.h> +#include <o3tl/string_view.hxx> #include <svl/sharedstringpool.hxx> #include <oox/core/filterbase.hxx> #include <oox/helper/binaryinputstream.hxx> @@ -101,12 +102,12 @@ const sal_uInt16 BIFF12_CFRULE_ABOVEAVERAGE = 0x0004; const sal_uInt16 BIFF12_CFRULE_BOTTOM = 0x0008; const sal_uInt16 BIFF12_CFRULE_PERCENT = 0x0010; -bool isValue(const OUString& rStr, double& rVal) +bool isValue(std::u16string_view rStr, double& rVal) { sal_Int32 nEnd = -1; - rVal = rtl::math::stringToDouble(rStr.trim(), '.', ',', nullptr, &nEnd); + rVal = rtl::math::stringToDouble(o3tl::trim(rStr), '.', ',', nullptr, &nEnd); - return nEnd >= rStr.getLength(); + return nEnd >= static_cast<sal_Int32>(rStr.size()); } void SetCfvoData( ColorScaleRuleModelEntry* pEntry, const AttributeList& rAttribs ) diff --git a/sd/source/filter/ppt/pptinanimations.cxx b/sd/source/filter/ppt/pptinanimations.cxx index 45af5f0e8f95..a760a0d896a5 100644 --- a/sd/source/filter/ppt/pptinanimations.cxx +++ b/sd/source/filter/ppt/pptinanimations.cxx @@ -1727,7 +1727,7 @@ void AnimationImporter::importCommandContainer( const Atom* pAtom, const Referen } else if( aParam.startsWith( "playFrom" ) ) { - const OUString aMediaTime( aParam.copy( 9, aParam.getLength() - 10 ) ); + const std::u16string_view aMediaTime( aParam.subView( 9, aParam.getLength() - 10 ) ); rtl_math_ConversionStatus eStatus; double fMediaTime = ::rtl::math::stringToDouble( aMediaTime, u'.', u',', &eStatus ); if( eStatus == rtl_math_ConversionStatus_Ok ) diff --git a/svgio/source/svgreader/svgtools.cxx b/svgio/source/svgreader/svgtools.cxx index b873404939da..38764729e33e 100644 --- a/svgio/source/svgreader/svgtools.cxx +++ b/svgio/source/svgreader/svgtools.cxx @@ -273,7 +273,7 @@ namespace svgio::svgreader rtl_math_ConversionStatus eStatus; fNum = rtl::math::stringToDouble( - aNum.makeStringAndClear(), '.', ',', + aNum, '.', ',', &eStatus); return eStatus == rtl_math_ConversionStatus_Ok; diff --git a/svl/source/numbers/zforfind.cxx b/svl/source/numbers/zforfind.cxx index b0207a4df2be..52bf1a9d1131 100644 --- a/svl/source/numbers/zforfind.cxx +++ b/svl/source/numbers/zforfind.cxx @@ -4057,7 +4057,7 @@ bool ImpSvNumberInputScan::IsNumberFormat( const OUString& rString, // s } sResString.append(sStrArray[nNums[nNumericsCnt-1]]); rtl_math_ConversionStatus eStatus; - fOutNumber = ::rtl::math::stringToDouble( sResString.makeStringAndClear(), '.', ',', &eStatus ); + fOutNumber = ::rtl::math::stringToDouble( sResString, '.', ',', &eStatus ); if ( eStatus == rtl_math_ConversionStatus_OutOfRange ) { F_Type = SvNumFormatType::TEXT; // overflow/underflow -> Text diff --git a/sw/source/core/doc/docsort.cxx b/sw/source/core/doc/docsort.cxx index 2c3ac9de29b5..bd86cf29c649 100644 --- a/sw/source/core/doc/docsort.cxx +++ b/sw/source/core/doc/docsort.cxx @@ -101,7 +101,7 @@ SwSortElement::~SwSortElement() { } -double SwSortElement::StrToDouble( const OUString& rStr ) +double SwSortElement::StrToDouble( std::u16string_view rStr ) { if( !pLclData ) pLclData = new LocaleDataWrapper( LanguageTag( *pLocale )); diff --git a/sw/source/core/inc/docsort.hxx b/sw/source/core/inc/docsort.hxx index 82f14c9761b2..0aad16da4a85 100644 --- a/sw/source/core/inc/docsort.hxx +++ b/sw/source/core/inc/docsort.hxx @@ -82,7 +82,7 @@ struct SwSortElement bool operator<(const SwSortElement& ) const; - static double StrToDouble(const OUString& rStr); + static double StrToDouble(std::u16string_view rStr); private: int keycompare(const SwSortElement& rCmp, sal_uInt16 nKey) const; }; diff --git a/unotools/source/i18n/localedatawrapper.cxx b/unotools/source/i18n/localedatawrapper.cxx index 9c4c5badabe3..24b19371a5a9 100644 --- a/unotools/source/i18n/localedatawrapper.cxx +++ b/unotools/source/i18n/localedatawrapper.cxx @@ -1413,22 +1413,13 @@ OUString LocaleDataWrapper::getCurr( sal_Int64 nNumber, sal_uInt16 nDecimals, // --- number parsing ------------------------------------------------- -double LocaleDataWrapper::stringToDouble( const OUString& rString, bool bUseGroupSep, +double LocaleDataWrapper::stringToDouble( std::u16string_view aString, bool bUseGroupSep, rtl_math_ConversionStatus* pStatus, sal_Int32* pParseEnd ) const { - const sal_Unicode cGroupSep = (bUseGroupSep ? aLocaleDataItem.thousandSeparator[0] : 0); - rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok; - sal_Int32 nParseEnd = 0; - double fValue = rtl::math::stringToDouble( rString, aLocaleDataItem.decimalSeparator[0], cGroupSep, &eStatus, &nParseEnd); - bool bTryAlt = (nParseEnd < rString.getLength() && !aLocaleDataItem.decimalSeparatorAlternative.isEmpty() && - rString[nParseEnd] == aLocaleDataItem.decimalSeparatorAlternative.toChar()); - // Try re-parsing with alternative if that was the reason to stop. - if (bTryAlt) - fValue = rtl::math::stringToDouble( rString, aLocaleDataItem.decimalSeparatorAlternative.toChar(), cGroupSep, &eStatus, &nParseEnd); - if (pStatus) - *pStatus = eStatus; + const sal_Unicode* pParseEndChar; + double fValue = stringToDouble(aString.data(), aString.data() + aString.size(), bUseGroupSep, pStatus, &pParseEndChar); if (pParseEnd) - *pParseEnd = nParseEnd; + *pParseEnd = pParseEndChar - aString.data(); return fValue; } diff --git a/vcl/inc/strhelper.hxx b/vcl/inc/strhelper.hxx index be2f5467861f..e5141db475a9 100644 --- a/vcl/inc/strhelper.hxx +++ b/vcl/inc/strhelper.hxx @@ -44,12 +44,12 @@ namespace psp // parses the first double in the string; decimal is '.' only - inline double StringToDouble( const OUString& rStr ) + inline double StringToDouble( std::u16string_view rStr ) { return rtl::math::stringToDouble(rStr, u'.', u'\0'); } - inline double StringToDouble(const OString& rStr) + inline double StringToDouble(std::string_view rStr) { return rtl::math::stringToDouble(rStr, '.', static_cast<char>(0)); } diff --git a/xmloff/source/core/xmluconv.cxx b/xmloff/source/core/xmluconv.cxx index e1fe1796488c..2bce27032844 100644 --- a/xmloff/source/core/xmluconv.cxx +++ b/xmloff/source/core/xmluconv.cxx @@ -348,6 +348,17 @@ bool SvXMLUnitConverter::convertDouble(double& rValue, eSrcUnit, m_pImpl->m_eCoreMeasureUnit); } +/** convert string to double number (using ::rtl::math) */ +bool SvXMLUnitConverter::convertDouble(double& rValue, + std::string_view rString) const +{ + sal_Int16 const eSrcUnit = ::sax::Converter::GetUnitFromString( + rString, m_pImpl->m_eCoreMeasureUnit); + + return ::sax::Converter::convertDouble(rValue, rString, + eSrcUnit, m_pImpl->m_eCoreMeasureUnit); +} + /** get the Null Date of the XModel and set it to the UnitConverter */ bool SvXMLUnitConverter::setNullDate(const css::uno::Reference <css::frame::XModel>& xModel) { @@ -539,7 +550,7 @@ bool SvXMLTokenEnumerator::getNextToken( std::u16string_view& rToken ) return true; } -static bool lcl_getPositions(std::u16string_view _sValue, OUString& _rContentX, OUString& _rContentY, OUString& _rContentZ) +static bool lcl_getPositions(std::u16string_view _sValue, std::u16string_view& _rContentX, std::u16string_view& _rContentY, std::u16string_view& _rContentZ) { if(_sValue.empty() || _sValue[0] != '(') return false; @@ -570,7 +581,7 @@ static bool lcl_getPositions(std::u16string_view _sValue, OUString& _rContentX, return true; } -static bool lcl_getPositions(std::string_view _sValue,OUString& _rContentX,OUString& _rContentY,OUString& _rContentZ) +static bool lcl_getPositions(std::string_view _sValue, std::string_view& _rContentX, std::string_view& _rContentY, std::string_view& _rContentZ) { if(_sValue.empty() || _sValue[0] != '(') return false; @@ -581,7 +592,7 @@ static bool lcl_getPositions(std::string_view _sValue,OUString& _rContentX,OUStr if(nFound == std::string_view::npos || nFound <= nPos) return false; - _rContentX = OUString::fromUtf8(_sValue.substr(nPos, nFound - nPos)); + _rContentX = _sValue.substr(nPos, nFound - nPos); nPos = nFound + 1; nFound = _sValue.find(' ', nPos); @@ -589,7 +600,7 @@ static bool lcl_getPositions(std::string_view _sValue,OUString& _rContentX,OUStr if(nFound == std::string_view::npos || nFound <= nPos) return false; - _rContentY = OUString::fromUtf8(_sValue.substr(nPos, nFound - nPos)); + _rContentY = _sValue.substr(nPos, nFound - nPos); nPos = nFound + 1; nFound = _sValue.find(')', nPos); @@ -597,7 +608,7 @@ static bool lcl_getPositions(std::string_view _sValue,OUString& _rContentX,OUStr if(nFound == std::string_view::npos || nFound <= nPos) return false; - _rContentZ = OUString::fromUtf8(_sValue.substr(nPos, nFound - nPos)); + _rContentZ = _sValue.substr(nPos, nFound - nPos); return true; } @@ -605,7 +616,7 @@ static bool lcl_getPositions(std::string_view _sValue,OUString& _rContentX,OUStr /** convert string to ::basegfx::B3DVector */ bool SvXMLUnitConverter::convertB3DVector( ::basegfx::B3DVector& rVector, std::u16string_view rValue ) { - OUString aContentX,aContentY,aContentZ; + std::u16string_view aContentX,aContentY,aContentZ; if ( !lcl_getPositions(rValue,aContentX,aContentY,aContentZ) ) return false; @@ -633,7 +644,7 @@ bool SvXMLUnitConverter::convertB3DVector( ::basegfx::B3DVector& rVector, std::u /** convert string to ::basegfx::B3DVector */ bool SvXMLUnitConverter::convertB3DVector( ::basegfx::B3DVector& rVector, std::string_view rValue ) { - OUString aContentX,aContentY,aContentZ; + std::string_view aContentX,aContentY,aContentZ; if ( !lcl_getPositions(rValue,aContentX,aContentY,aContentZ) ) return false; @@ -674,7 +685,7 @@ void SvXMLUnitConverter::convertB3DVector( OUStringBuffer &rBuffer, const ::base bool SvXMLUnitConverter::convertPosition3D( drawing::Position3D& rPosition, std::string_view rValue ) const { - OUString aContentX,aContentY,aContentZ; + std::string_view aContentX,aContentY,aContentZ; if ( !lcl_getPositions(rValue,aContentX,aContentY,aContentZ) ) return false; |