Kaydet (Commit) 230e9978 authored tarafından Mike Kaganski's avatar Mike Kaganski Kaydeden (comit) Andras Timar

tdf#94810: fix reverse offset mapping

With simple transliteration, TextSearch::searchForward used to use
whole string to perform the search, then started to create substring
to search. But it left the precautions from
commit c00601da by Eike Rathke:
searching for $ may actually return a result set pointing behind the
search string which it does with the ICU regex engine.
The precaution made it to skip reverse mapping if index was 0.

Commit 9aae521b by Michael Stahl
didn't consider the case when nStop is 0, and startPos > 0. Then it
tried to get offset[-1].

Anyway, using value of startPos in those conditions seems illogical.

Removed those precautions (and made assertions for that).
Fixed handling zero indexes.

Reviewed-on: https://gerrit.libreoffice.org/19840Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarEike Rathke <erack@redhat.com>
Tested-by: 's avatarEike Rathke <erack@redhat.com>
(cherry picked from commit 4cf1d290)

remove comment that makes no sense, tdf#94810 follow-up

nStart should not become <0, not even when searching for $ end.

(cherry picked from commit 67ab2ce3)

similar to searchForward() use the correct offsets, tdf#94810 related

(cherry picked from commit ce91f3c1)

Change-Id: I2066abc51fff7fb7323bc7f6198bdea06439d4f3
79586471f1a7908372fa3ee044a6f66499b1790f
cb5a45acaad9029e0998594de8b3dc2e9211866e
Reviewed-on: https://gerrit.libreoffice.org/20136Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarMike Kaganski <mikekaganski@hotmail.com>

(cherry picked from commit 028d7d84)
üst 61bef812
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <com/sun/star/util/SearchOptions.hpp> #include <com/sun/star/util/SearchOptions.hpp>
#include <com/sun/star/util/SearchAlgorithms.hpp> #include <com/sun/star/util/SearchAlgorithms.hpp>
#include <com/sun/star/util/XTextSearch.hpp> #include <com/sun/star/util/XTextSearch.hpp>
#include <com/sun/star/i18n/Transliteration.hpp>
#include <unotest/bootstrapfixturebase.hxx> #include <unotest/bootstrapfixturebase.hxx>
#include <unicode/regex.h> #include <unicode/regex.h>
...@@ -123,6 +124,16 @@ void TestTextSearch::testSearches() ...@@ -123,6 +124,16 @@ void TestTextSearch::testSearches()
CPPUNIT_ASSERT( aRes.subRegExpressions > 0 ); CPPUNIT_ASSERT( aRes.subRegExpressions > 0 );
CPPUNIT_ASSERT( aRes.startOffset[0] == bStartRes ); CPPUNIT_ASSERT( aRes.startOffset[0] == bStartRes );
CPPUNIT_ASSERT( aRes.endOffset[0] == bEndRes ); CPPUNIT_ASSERT( aRes.endOffset[0] == bEndRes );
aOptions.transliterateFlags = ::css::i18n::TransliterationModules::TransliterationModules_IGNORE_CASE
| ::css::i18n::TransliterationModules::TransliterationModules_IGNORE_WIDTH;
aOptions.searchString = "([^ ]*)[ ]*([^ ]*)";
m_xSearch->setOptions(aOptions);
aRes = m_xSearch->searchForward("11 22 33", 2, 7);
CPPUNIT_ASSERT(aRes.subRegExpressions == 3);
CPPUNIT_ASSERT((aRes.startOffset[0] == 2) && (aRes.endOffset[0] == 5));
CPPUNIT_ASSERT((aRes.startOffset[1] == 2) && (aRes.endOffset[1] == 2));
CPPUNIT_ASSERT((aRes.startOffset[2] == 3) && (aRes.endOffset[2] == 5));
} }
void TestTextSearch::setUp() void TestTextSearch::setUp()
......
...@@ -301,15 +301,18 @@ SearchResult TextSearch::searchForward( const OUString& searchStr, sal_Int32 sta ...@@ -301,15 +301,18 @@ SearchResult TextSearch::searchForward( const OUString& searchStr, sal_Int32 sta
for ( sal_Int32 k = 0; k < nGroups; k++ ) for ( sal_Int32 k = 0; k < nGroups; k++ )
{ {
const sal_Int32 nStart = sres.startOffset[k] - nExtraOffset; const sal_Int32 nStart = sres.startOffset[k] - nExtraOffset;
if (startPos > 0 || nStart > 0) assert(nStart >= 0);
sres.startOffset[k] = (nStart < nOffsets ? offset[nStart] : (offset[nOffsets - 1] + 1)); sres.startOffset[k] = (nStart < nOffsets ? offset[nStart] : (offset[nOffsets - 1] + 1));
// JP 20.6.2001: end is ever exclusive and then don't return // JP 20.6.2001: end is ever exclusive and then don't return
// the position of the next character - return the // the position of the next character - return the
// next position behind the last found character! // next position behind the last found character!
// "a b c" find "b" must return 2,3 and not 2,4!!! // "a b c" find "b" must return 2,3 and not 2,4!!!
const sal_Int32 nStop = sres.endOffset[k] - nExtraOffset; const sal_Int32 nStop = sres.endOffset[k] - nExtraOffset;
if (startPos > 0 || nStop > 0) assert(nStop >= 0);
if (nStop > 0)
sres.endOffset[k] = offset[(nStop <= nOffsets ? nStop : nOffsets) - 1] + 1; sres.endOffset[k] = offset[(nStop <= nOffsets ? nStop : nOffsets) - 1] + 1;
else
sres.endOffset[k] = offset[0];
} }
} }
} }
...@@ -401,15 +404,18 @@ SearchResult TextSearch::searchBackward( const OUString& searchStr, sal_Int32 st ...@@ -401,15 +404,18 @@ SearchResult TextSearch::searchBackward( const OUString& searchStr, sal_Int32 st
for ( sal_Int32 k = 0; k < nGroups; k++ ) for ( sal_Int32 k = 0; k < nGroups; k++ )
{ {
const sal_Int32 nStart = sres.startOffset[k]; const sal_Int32 nStart = sres.startOffset[k];
if (endPos > 0 || nStart > 0) assert(nStart >= 0);
if (nStart > 0)
sres.startOffset[k] = offset[(nStart <= nOffsets ? nStart : nOffsets) - 1] + 1; sres.startOffset[k] = offset[(nStart <= nOffsets ? nStart : nOffsets) - 1] + 1;
else
sres.startOffset[k] = offset[0];
// JP 20.6.2001: end is ever exclusive and then don't return // JP 20.6.2001: end is ever exclusive and then don't return
// the position of the next character - return the // the position of the next character - return the
// next position behind the last found character! // next position behind the last found character!
// "a b c" find "b" must return 2,3 and not 2,4!!! // "a b c" find "b" must return 2,3 and not 2,4!!!
const sal_Int32 nStop = sres.endOffset[k]; const sal_Int32 nStop = sres.endOffset[k];
if (endPos > 0 || nStop > 0) assert(nStop >= 0);
sres.endOffset[k] = (nStop < nOffsets ? offset[nStop] : (offset[nOffsets - 1] + 1)); sres.endOffset[k] = (nStop < nOffsets ? offset[nStop] : (offset[nOffsets - 1] + 1));
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment