comphelper/source/misc/string.cxx | 27 +++++++++++++++++++++++++++ include/comphelper/string.hxx | 9 +++++++++ sw/source/filter/ww8/ww8par3.cxx | 28 +--------------------------- 3 files changed, 37 insertions(+), 27 deletions(-)
New commits: commit 2b6d04b8493a6b05c8ed0afb0c1e96d441ba876b Author: Caolán McNamara <caol...@redhat.com> AuthorDate: Sat Dec 17 20:21:47 2022 +0000 Commit: Caolán McNamara <caol...@redhat.com> CommitDate: Sat Dec 17 21:50:27 2022 +0000 move and rename sanitizeString for reuse Change-Id: I8378f3df79e511cf2d385ace1cd7964ab1c76e7b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/144388 Tested-by: Caolán McNamara <caol...@redhat.com> Reviewed-by: Caolán McNamara <caol...@redhat.com> diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx index d0f7cb9ca423..da5c8b92c05c 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -32,6 +32,7 @@ #include <rtl/ustrbuf.hxx> #include <rtl/string.hxx> #include <rtl/strbuf.hxx> +#include <sal/log.hxx> #include <sal/types.h> #include <comphelper/string.hxx> @@ -652,6 +653,32 @@ void replaceAt(OUStringBuffer& rIn, sal_Int32 nIndex, sal_Int32 nCount, std::u16 rIn.setLength(nNewLength); } +OUString sanitizeStringSurrogates(const OUString& rString) +{ + sal_Int32 i=0; + while (i < rString.getLength()) + { + sal_Unicode c = rString[i]; + if (rtl::isHighSurrogate(c)) + { + if (i+1 == rString.getLength() + || !rtl::isLowSurrogate(rString[i+1])) + { + SAL_WARN("comphelper", "Surrogate error: high without low"); + return rString.copy(0, i); + } + ++i; //skip correct low + } + if (rtl::isLowSurrogate(c)) //bare low without preceding high + { + SAL_WARN("comphelper", "Surrogate error: low without high"); + return rString.copy(0, i); + } + ++i; + } + return rString; +} + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/comphelper/string.hxx b/include/comphelper/string.hxx index b43086a77d30..06c825b36832 100644 --- a/include/comphelper/string.hxx +++ b/include/comphelper/string.hxx @@ -375,6 +375,15 @@ COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::string_view rString); */ COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::u16string_view rString); +/** Santitize an OUString to not have invalid surrogates + + @param rString An OUString + + @return same string if no surrogates or surrogates are valid. + Otherwise the string truncated to the valid sequence. + */ +COMPHELPER_DLLPUBLIC OUString sanitizeStringSurrogates(const OUString& rString); + } // namespace comphelper::string /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/filter/ww8/ww8par3.cxx b/sw/source/filter/ww8/ww8par3.cxx index f02a2972e058..445e5442a0fe 100644 --- a/sw/source/filter/ww8/ww8par3.cxx +++ b/sw/source/filter/ww8/ww8par3.cxx @@ -490,32 +490,6 @@ WW8LSTInfo* WW8ListManager::GetLSTByListId( sal_uInt32 nIdLst ) const return aResult->get(); } -static OUString sanitizeString(const OUString& rString) -{ - sal_Int32 i=0; - while (i < rString.getLength()) - { - sal_Unicode c = rString[i]; - if (rtl::isHighSurrogate(c)) - { - if (i+1 == rString.getLength() - || !rtl::isLowSurrogate(rString[i+1])) - { - SAL_WARN("sw.ww8", "Surrogate error: high without low"); - return rString.copy(0, i); - } - ++i; //skip correct low - } - if (rtl::isLowSurrogate(c)) //bare low without preceding high - { - SAL_WARN("sw.ww8", "Surrogate error: low without high"); - return rString.copy(0, i); - } - ++i; - } - return rString; -} - SvxNumType WW8ListManager::GetSvxNumTypeFromMSONFC(sal_uInt16 nNFC) { SvxNumType nType(SVX_NUM_ARABIC); @@ -874,7 +848,7 @@ bool WW8ListManager::ReadLVL(SwNumFormat& rNumFormat, std::unique_ptr<SfxItemSet // 4. Read numbering String. Results in prefix and postfix - OUString sNumString(sanitizeString(read_uInt16_PascalString(m_rSt))); + OUString sNumString(comphelper::string::sanitizeStringSurrogates(read_uInt16_PascalString(m_rSt))); // 5. convert read values into Writer syntax