diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-04-26 17:47:18 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-04-27 12:19:55 +0200 |
commit | d506ff97c25b5f433aa25d8b373f1a732af493d1 (patch) | |
tree | 600e211e3426a3b43407b01d6f93e5379d608b26 /sax/source/tools/converter.cxx | |
parent | 148f45253f75bc724804f3231a0b04b2d453e0c7 (diff) |
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 <noel.grandin@collabora.co.uk>
Diffstat (limited to 'sax/source/tools/converter.cxx')
-rw-r--r-- | sax/source/tools/converter.cxx | 35 |
1 files changed, 35 insertions, 0 deletions
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) { |