sbutil/searchEOL: Fix mixed endings with first one \r

Fixes the case when the string to be searched contains mixed ending
types and the first one is “\r”. Previously it would have searched for
the “\n” first and skipped over the “\r”.

Change-Id: I945087f2b93d92e73781c07b9b3cdaf9064d1122
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/207061
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
This commit is contained in:
Neil Roberts
2026-07-22 15:59:00 +02:00
parent af830cc577
commit 0b7d925276
2 changed files with 10 additions and 5 deletions
+2
View File
@@ -33,6 +33,8 @@ void TestUtil::testSearchEOL()
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), sb::searchEOL(u"one\ntwo\r\three\r\n"));
// Find the ending when \r\n is the first one
CPPUNIT_ASSERT_EQUAL(sal_Int32(4), sb::searchEOL(u"one\r\ntwo\r\three\n"));
// Find the ending when \r is the first one
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), sb::searchEOL(u"one\rtwo\n\three\r\n"));
// The three types of ending at at the end of the string
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), sb::searchEOL(u"one\n"));
+8 -5
View File
@@ -25,12 +25,15 @@ namespace sb
{
sal_Int32 searchEOL(std::u16string_view rStr, sal_Int32 fromIndex)
{
size_t iLF = rStr.find('\n', fromIndex);
if (iLF != std::u16string_view::npos)
return iLF;
size_t nPos = rStr.find_first_of(u"\r\n", fromIndex);
size_t iCR = rStr.find('\r', fromIndex);
return iCR == std::u16string_view::npos ? -1 : iCR;
if (nPos == std::u16string_view::npos)
return -1;
if (rStr.substr(nPos).starts_with(u"\r\n"))
return nPos + 1;
else
return nPos;
}
}