From d506ff97c25b5f433aa25d8b373f1a732af493d1 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Tue, 26 Apr 2022 17:47:18 +0200 Subject: add string_view wrappers for rtl::math::stringToDouble Change-Id: I114bec72cb933238675e539a8388a607226827cd Reviewed-on: https://gerrit.libreoffice.org/c/core/+/133455 Tested-by: Jenkins Reviewed-by: Noel Grandin --- sax/source/tools/converter.cxx | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'sax/source/tools/converter.cxx') 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 @@ -686,6 +686,22 @@ bool Converter::convertDouble(double& rValue, return true; } +/** 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) { @@ -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 static sal_Int16 lcl_GetUnitFromString(V rString, sal_Int16 nDefaultUnit) { -- cgit