tdf#106137 sc: process Unicode escapes in regex replacement strings

This uses i18nutil::processUnicodeEscapes(), introduced in the
parent commit.

Change-Id: Ieb780d494ea3cdb8a976bb4ca33d95baa7127c8e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/200739
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
AnamayNarkar
2026-03-03 07:19:46 +01:00
committed by Mike Kaganski
parent 5c7833fee2
commit fc800ec179
2 changed files with 53 additions and 0 deletions
+51
View File
@@ -4460,6 +4460,57 @@ CPPUNIT_TEST_FIXTURE(Test, testSearchCells)
m_pDoc->DeleteTab(0);
}
CPPUNIT_TEST_FIXTURE(Test, testTdf106137_UnicodeEscapeInReplacement)
{
m_pDoc->InsertTab(0, u"Test"_ustr);
// test basic unicode escape expansion
// unicode values in replacement strings should expand to Unicode character when regular expressions is selected
m_pDoc->SetString(ScAddress(0, 0, 0), u"hello world"_ustr);
SvxSearchItem aItem(SID_SEARCH_ITEM);
aItem.SetSearchString(u"world"_ustr);
aItem.SetReplaceString(u"\\u0041\\u0042\\u0043"_ustr);
aItem.SetCommand(SvxSearchCmd::REPLACE_ALL);
aItem.SetRegExp(true);
ScMarkData aMarkData(m_pDoc->GetSheetLimits());
aMarkData.SelectOneTable(0);
SCCOL nCol = 0;
SCROW nRow = 0;
SCTAB nTab = 0;
ScRangeList aMatchedRanges;
OUString aUndoStr;
bool bClamped = false;
m_pDoc->SearchAndReplace(aItem, nCol, nRow, nTab, aMarkData, aMatchedRanges, aUndoStr, nullptr,
bClamped);
CPPUNIT_ASSERT_EQUAL(u"hello ABC"_ustr, m_pDoc->GetString(ScAddress(0, 0, 0)));
// test Backreference edge case
// \uXXXX in replacement template should expand before back-references are substituted,
// so that the 'searched' content containing literal \u sequences is not accidentally expanded
// Insert text that contains an actual escaped unicode string
m_pDoc->SetString(ScAddress(0, 1, 0), u"find \\u0042"_ustr); // A2
// Capture the literal "\u0042" in a regex group
aItem.SetSearchString(u"(find .*)"_ustr);
// Replace with \u0041 (which should become 'A') + the backreference
aItem.SetReplaceString(u"\\u0041 $1"_ustr);
nCol = 0;
nRow = 0;
nTab = 0;
m_pDoc->SearchAndReplace(aItem, nCol, nRow, nTab, aMarkData, aMatchedRanges, aUndoStr, nullptr,
bClamped);
// shouldn't be 'A find B'
CPPUNIT_ASSERT_EQUAL(u"A find \\u0042"_ustr, m_pDoc->GetString(ScAddress(0, 1, 0)));
m_pDoc->DeleteTab(0);
}
CPPUNIT_TEST_FIXTURE(Test, testFormulaPosition)
{
m_pDoc->InsertTab(0, u"Test"_ustr);
+2
View File
@@ -32,6 +32,7 @@
#include <markdata.hxx>
#include <editutil.hxx>
#include <postit.hxx>
#include <i18nutil/unicodeescape.hxx>
namespace {
@@ -185,6 +186,7 @@ bool ScTable::SearchCell(const SvxSearchItem& rSearchItem, SCCOL nCol, sc::Colum
OUString sReplStr = rSearchItem.GetReplaceString();
if (rSearchItem.GetRegExp())
{
sReplStr = i18nutil::processUnicodeEscapes(sReplStr);
utl::TextSearch::ReplaceBackReferences( sReplStr, aString, aSearchResult );
OUStringBuffer aStrBuffer(aString);
aStrBuffer.remove(nStart, nEnd-nStart+1);