tdf#152690: Fix "!" behavior when it is not the first in a group

Fix "!" behavior when it is not the first in a group.
Add testcases for "!" and Like.

Change-Id: Ia76fa26722b6546d08dd8842d83f55bb0c0ea5ac
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148720
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
diff --git a/basic/qa/basic_coverage/test_like.bas b/basic/qa/basic_coverage/test_like.bas
new file mode 100644
index 0000000..2d018bf
--- /dev/null
+++ b/basic/qa/basic_coverage/test_like.bas
@@ -0,0 +1,41 @@
'
' This file is part of the LibreOffice project.
'
' This Source Code Form is subject to the terms of the Mozilla Public
' License, v. 2.0. If a copy of the MPL was not distributed with this
' file, You can obtain one at http://mozilla.org/MPL/2.0/.
'

Option Explicit

Function doUnitTest() As String
    TestUtil.TestInit
    verify_testLike
    doUnitTest = TestUtil.GetResult()
End Function

Sub verify_testLike()
    On Error GoTo errorHandler
    ' Negation test
    TestUtil.AssertEqual("!" Like "[.!?]",  True, "Negation1")
    TestUtil.AssertEqual("a" Like "[!abc]",  False, "Negation2")
    TestUtil.AssertEqual("!" Like "[!!?]", False, "Negation3")
    TestUtil.AssertEqual("^" Like "[.!?]", False, "Negation4")
    TestUtil.AssertEqual("^" Like "[.^?]", True, "Negation5")
    ' Like test from microsoft vba
    TestUtil.AssertEqual("aBBBa" Like "a*a", True, "Like1")
    TestUtil.AssertEqual("F" Like "[A-Z]", True, "Like2")
    TestUtil.AssertEqual("F" Like "[!A-Z]", False, "Like3")
    TestUtil.AssertEqual("a2a" Like "a#a", True, "Like4")
    TestUtil.AssertEqual("aM5b" Like "a[L-P]#[!c-e]", True, "Like5")
    TestUtil.AssertEqual("BAT123khg" Like "B?T*", True, "Like6")
    TestUtil.AssertEqual("CAT123khg" Like "B?T*", False, "Like7")
    TestUtil.AssertEqual("ab" Like "a*b", True, "Like8")
    TestUtil.AssertEqual("a*b" Like "a [*]b", False, "Like9")
    TestUtil.AssertEqual("axxxxxb" Like "a [*]b", False, "Like10")
    TestUtil.AssertEqual("a [xyz" Like "a [[]*", True, "Like11")

    Exit Sub
errorHandler:
    TestUtil.ReportErrorHandler("verify_testLike", Err, Error$, Erl)
End Sub
diff --git a/basic/qa/cppunit/test_vba.cxx b/basic/qa/cppunit/test_vba.cxx
index b778d39..8a15ba7 100644
--- a/basic/qa/cppunit/test_vba.cxx
+++ b/basic/qa/cppunit/test_vba.cxx
@@ -114,6 +114,7 @@ void VBATest::testMiscVBAFunctions()
        "minute.vb",
        "month.vb",
        "monthname.vb",
        "like.vb",
        "oct.vb",
        "optional_paramters.vb",
        "qbcolor.vb",
diff --git a/basic/qa/vba_tests/like.vb b/basic/qa/vba_tests/like.vb
new file mode 100644
index 0000000..c3be1e0
--- /dev/null
+++ b/basic/qa/vba_tests/like.vb
@@ -0,0 +1,42 @@
'
' This file is part of the LibreOffice project.
'
' This Source Code Form is subject to the terms of the Mozilla Public
' License, v. 2.0. If a copy of the MPL was not distributed with this
' file, You can obtain one at http://mozilla.org/MPL/2.0/.
'

Option VBASupport 1
Option Explicit

Function doUnitTest() As String
    TestUtil.TestInit
    verify_testLike
    doUnitTest = TestUtil.GetResult()
End Function

Sub verify_testLike()
    On Error GoTo errorHandler
    ' Negation test
    TestUtil.AssertEqual("!" Like "[.!?]",  True, "Negation1")
    TestUtil.AssertEqual("a" Like "[!abc]",  False, "Negation2")
    TestUtil.AssertEqual("!" Like "[!!?]", False, "Negation3")
    TestUtil.AssertEqual("^" Like "[.!?]", False, "Negation4")
    TestUtil.AssertEqual("^" Like "[.^?]", True, "Negation5")
    ' Like test from microsoft vba
    TestUtil.AssertEqual("aBBBa" Like "a*a", True, "Like1")
    TestUtil.AssertEqual("F" Like "[A-Z]", True, "Like2")
    TestUtil.AssertEqual("F" Like "[!A-Z]", False, "Like3")
    TestUtil.AssertEqual("a2a" Like "a#a", True, "Like4")
    TestUtil.AssertEqual("aM5b" Like "a[L-P]#[!c-e]", True, "Like5")
    TestUtil.AssertEqual("BAT123khg" Like "B?T*", True, "Like6")
    TestUtil.AssertEqual("CAT123khg" Like "B?T*", False, "Like7")
    TestUtil.AssertEqual("ab" Like "a*b", True, "Like8")
    TestUtil.AssertEqual("a*b" Like "a [*]b", False, "Like9")
    TestUtil.AssertEqual("axxxxxb" Like "a [*]b", False, "Like10")
    TestUtil.AssertEqual("a [xyz" Like "a [[]*", True, "Like11")

    Exit Sub
errorHandler:
    TestUtil.ReportErrorHandler("verify_testLike", Err, Error$, Erl)
End Sub
diff --git a/basic/source/runtime/runtime.cxx b/basic/source/runtime/runtime.cxx
index 060676c..69eeb19 100644
--- a/basic/source/runtime/runtime.cxx
+++ b/basic/source/runtime/runtime.cxx
@@ -1491,6 +1491,10 @@ namespace
            case '[':
                sResult.append(*start++);
                seenright = 0;
                if (start < end && *start=='!'){
                    sResult.append('^');
                    start++;
                }
                while (start < end && !seenright)
                {
                    switch (*start)
@@ -1505,9 +1509,6 @@ namespace
                        sResult.append(*start);
                        seenright = 1;
                        break;
                    case '!':
                        sResult.append('^');
                        break;
                    default:
                        if (NeedEsc(*start))
                        {