basic/qa/basic_coverage/test_instr_method.vb |    6 ++++
 basic/qa/vba_tests/instr.vb                  |    6 ++++
 basic/source/runtime/methods.cxx             |   37 ++++++++++++++++-----------
 3 files changed, 34 insertions(+), 15 deletions(-)

New commits:
commit 632fd5fd504d9800d580ceeeb87bc2b5d626d56a
Author:     Andreas Heinisch <andreas.heini...@yahoo.de>
AuthorDate: Thu Jul 15 20:43:32 2021 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Mon Jul 19 14:05:43 2021 +0200

    tdf#139840 - Use utl::TextSearch to implement the InStr function
    
    In addtion, fixed a crash if the start position is greater than the
    length of the string being searched.
    
    Change-Id: I9bcda1131324bdfac6957018e91b3a36dd2dc3d6
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/118996
    Tested-by: Jenkins
    Reviewed-by: Andreas Heinisch <andreas.heini...@yahoo.de>
    Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119165

diff --git a/basic/qa/basic_coverage/test_instr_method.vb 
b/basic/qa/basic_coverage/test_instr_method.vb
index 47fa7a94ede1..630db562d261 100644
--- a/basic/qa/basic_coverage/test_instr_method.vb
+++ b/basic/qa/basic_coverage/test_instr_method.vb
@@ -16,6 +16,12 @@ Function doUnitTest as Integer
 
     ' tdf#139840 - case-insensitive operation for non-ASCII characters
     If (InStr(1, "α", "Α", 1) <> 1) Then Exit Function
+    ' tdf#139840 - German Eszett is uppercased to a two-character 'SS'.
+    ' This test should fail after tdf#110003 has been fixed.
+    If (InStr(2, "Straße", "s", 1) <> 5) Then Exit Function
+
+    ' Start position is greater than the length of the string being searched.
+    If (InStr(2, "α", "Α", 1) <> 0) Then Exit Function
 
     doUnitTest = 1
 
diff --git a/basic/qa/vba_tests/instr.vb b/basic/qa/vba_tests/instr.vb
index 3ced2a07c8ca..cda19712b296 100644
--- a/basic/qa/vba_tests/instr.vb
+++ b/basic/qa/vba_tests/instr.vb
@@ -32,6 +32,12 @@ Sub verify_testInStr()
 
     ' tdf#139840 - case-insensitive operation for non-ASCII characters
     TestUtil.AssertEqual(InStr(1, "α", "Α", 1), 1, "InStr(1, ""α"", ""Α"", 1)")
+    ' tdf#139840 - German Eszett is uppercased to a two-character 'SS'.
+    ' This test should fail after tdf#110003 has been fixed.
+    TestUtil.AssertEqual(InStr(2, "Straße", "s", 1), 5, "InStr(2, ""Straße"", 
""s"", 1)")
+
+    ' Start position is greater than the length of the string being searched.
+    TestUtil.AssertEqual(InStr(2, "α", "Α", 1), 0, "InStr(2, ""α"", ""Α"", 1)")
 
     Exit Sub
 errorHandler:
diff --git a/basic/source/runtime/methods.cxx b/basic/source/runtime/methods.cxx
index ab77fc895b6e..ff2474a094e1 100644
--- a/basic/source/runtime/methods.cxx
+++ b/basic/source/runtime/methods.cxx
@@ -885,25 +885,32 @@ void SbRtl_InStr(StarBASIC *, SbxArray & rPar, bool)
         }
         else
         {
-            if( !bTextMode )
+            const OUString& rStr1 = rPar.Get(nFirstStringPos)->GetOUString();
+            const sal_Int32 nrStr1Len = rStr1.getLength();
+            if (nStartPos > nrStr1Len)
             {
-                const OUString& rStr1 = 
rPar.Get(nFirstStringPos)->GetOUString();
-                nPos = rStr1.indexOf( rToken, nStartPos - 1 ) + 1;
+                // Start position is greater than the string being searched
+                nPos = 0;
             }
             else
             {
-                OUString aStr1 = rPar.Get(nFirstStringPos)->GetOUString();
-                OUString aToken = rToken;
-
-                // tdf#139840 - case-insensitive operation for non-ASCII 
characters
-                const css::lang::Locale& rLocale
-                    = Application::GetSettings().GetLanguageTag().getLocale();
-                css::uno::Reference<i18n::XCharacterClassification> xCharClass
-                    = vcl::unohelper::CreateCharacterClassification();
-                aStr1 = xCharClass->toUpper(aStr1, 0, aStr1.getLength(), 
rLocale);
-                aToken = xCharClass->toUpper(aToken, 0, aToken.getLength(), 
rLocale);
-
-                nPos = aStr1.indexOf( aToken, nStartPos-1 ) + 1;
+                if( !bTextMode )
+                {
+                    nPos = rStr1.indexOf( rToken, nStartPos - 1 ) + 1;
+                }
+                else
+                {
+                    // tdf#139840 - case-insensitive operation for non-ASCII 
characters
+                    i18nutil::SearchOptions2 aSearchOptions;
+                    aSearchOptions.searchString = rToken;
+                    aSearchOptions.AlgorithmType2 = 
util::SearchAlgorithms2::ABSOLUTE;
+                    aSearchOptions.transliterateFlags |= 
TransliterationFlags::IGNORE_CASE;
+                    utl::TextSearch textSearch(aSearchOptions);
+
+                    sal_Int32 nStart = nStartPos - 1;
+                    sal_Int32 nEnd = nrStr1Len;
+                    nPos = textSearch.SearchForward(rStr1, &nStart, &nEnd) ? 
nStart + 1 : 0;
+                }
             }
         }
         rPar.Get(0)->PutLong(nPos);
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to