i18npool/qa/cppunit/test_textsearch.cxx | 23 ++++++++++++++++++ i18npool/source/search/textsearch.cxx | 40 +++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 8 deletions(-)
New commits: commit 5291e4826a9512bd6d8d3b03199c49222a83e972 Author: Eike Rathke <er...@redhat.com> Date: Sun Apr 24 16:40:33 2016 +0200 unit test for tdf#99468 Change-Id: I833ad2779d0eda6f5183b2dd062dffaa410a7937 diff --git a/i18npool/qa/cppunit/test_textsearch.cxx b/i18npool/qa/cppunit/test_textsearch.cxx index 893bed5..c4e98c9 100644 --- a/i18npool/qa/cppunit/test_textsearch.cxx +++ b/i18npool/qa/cppunit/test_textsearch.cxx @@ -251,6 +251,29 @@ void TestTextSearch::testWildcardSearch() aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0); CPPUNIT_ASSERT(aRes.subRegExpressions == 1); CPPUNIT_ASSERT((aRes.startOffset[0] == 5) && (aRes.endOffset[0] == 0)); + + aText = "123123"; + aOptions.searchString = "*2?"; + m_xSearch2->setOptions2( aOptions ); + // match first "123", [0,3) + aRes = m_xSearch2->searchForward( aText, 0, aText.getLength()); + CPPUNIT_ASSERT(aRes.subRegExpressions == 1); + CPPUNIT_ASSERT((aRes.startOffset[0] == 0) && (aRes.endOffset[0] == 3)); + // match "123123", (6,0] Yes this looks odd, but it is as searching "?2*" forward. + aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0); + CPPUNIT_ASSERT(aRes.subRegExpressions == 1); + CPPUNIT_ASSERT((aRes.startOffset[0] == 6) && (aRes.endOffset[0] == 0)); + + aOptions.searchFlag |= util::SearchFlags::WILD_MATCH_SELECTION; + m_xSearch2->setOptions2( aOptions ); + // match "123123", [0,6) with greedy '*' + aRes = m_xSearch2->searchForward( aText, 0, aText.getLength()); + CPPUNIT_ASSERT(aRes.subRegExpressions == 1); + CPPUNIT_ASSERT((aRes.startOffset[0] == 0) && (aRes.endOffset[0] == 6)); + // match "123123", (6,0] + aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0); + CPPUNIT_ASSERT(aRes.subRegExpressions == 1); + CPPUNIT_ASSERT((aRes.startOffset[0] == 6) && (aRes.endOffset[0] == 0)); } void TestTextSearch::setUp() commit ffbe1b43bff59aae506446a5f408bba8ba9315c2 Author: Eike Rathke <er...@redhat.com> Date: Sun Apr 24 16:16:51 2016 +0200 Resolves: tdf#99468 do greedy '*' match if substring match is not allowed Change-Id: I89ac29b7e8c2e8c567e85a5025dbc1f50050465d diff --git a/i18npool/source/search/textsearch.cxx b/i18npool/source/search/textsearch.cxx index 07cceb0..5791ff6 100644 --- a/i18npool/source/search/textsearch.cxx +++ b/i18npool/source/search/textsearch.cxx @@ -1188,7 +1188,7 @@ SearchResult TextSearch::WildcardSrchFrwrd( const OUString& searchStr, sal_Int32 rPattern.iterateCodePoints( &nAfterFakePattern); } - sal_Int32 nString = nStartPos, nPat = -1, nStr = -1; + sal_Int32 nString = nStartPos, nPat = -1, nStr = -1, nLastAsterisk = -1; sal_uInt32 cPatternAfterAsterisk = 0; bool bEscaped = false, bEscapedAfterAsterisk = false; @@ -1215,11 +1215,21 @@ SearchResult TextSearch::WildcardSrchFrwrd( const OUString& searchStr, sal_Int32 } else { - // A trailing '*' is handled below. If the pattern is consumed and - // substring match allowed we're good. + // A trailing '*' is handled below. if (mbWildcardAllowSubstring) + { + // If the pattern is consumed and substring match allowed we're good. setWildcardMatch( aRes, nStartOffset, nString); - return aRes; + return aRes; + } + else if (nString < nEndPos && nLastAsterisk >= 0) + { + // If substring match is not allowed try a greedy '*' match. + nPattern = nLastAsterisk; + continue; // do + } + else + return aRes; } if (cPattern == '*' && !bEscaped) @@ -1235,6 +1245,8 @@ SearchResult TextSearch::WildcardSrchFrwrd( const OUString& searchStr, sal_Int32 return aRes; } + nLastAsterisk = nPattern; // Remember last encountered '*'. + // cPattern will be the next non-'*' character, nPattern // incremented. cPattern = rPattern.iterateCodePoints( &nPattern); @@ -1406,7 +1418,7 @@ SearchResult TextSearch::WildcardSrchBkwrd( const OUString& searchStr, sal_Int32 rReversePattern.iterateCodePoints( &nAfterFakePattern, -1); } - sal_Int32 nString = nStartPos, nPat = -1, nStr = -1; + sal_Int32 nString = nStartPos, nPat = -1, nStr = -1, nLastAsterisk = -1; sal_uInt32 cPatternAfterAsterisk = 0; bool bEscaped = false, bEscapedAfterAsterisk = false; @@ -1433,11 +1445,21 @@ SearchResult TextSearch::WildcardSrchBkwrd( const OUString& searchStr, sal_Int32 } else { - // A leading '*' is handled below. If the pattern is consumed and - // substring match allowed we're good. + // A trailing '*' is handled below. if (mbWildcardAllowSubstring) + { + // If the pattern is consumed and substring match allowed we're good. setWildcardMatch( aRes, nStartOffset, nString); - return aRes; + return aRes; + } + else if (nString > nEndPos && nLastAsterisk >= 0) + { + // If substring match is not allowed try a greedy '*' match. + nPattern = nLastAsterisk; + continue; // do + } + else + return aRes; } if (cPattern == '*' && !bEscaped) @@ -1453,6 +1475,8 @@ SearchResult TextSearch::WildcardSrchBkwrd( const OUString& searchStr, sal_Int32 return aRes; } + nLastAsterisk = nPattern; // Remember last encountered '*'. + // cPattern will be the previous non-'*' character, nPattern // decremented. cPattern = rReversePattern.iterateCodePoints( &nPattern, -1); _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits