diff options
author | Luboš Luňák <l.lunak@collabora.com> | 2022-03-01 10:44:12 +0100 |
---|---|---|
committer | Luboš Luňák <l.lunak@collabora.com> | 2022-03-01 18:00:17 +0100 |
commit | ffe7850a1449c43dd077c5cb073d8eee178f7099 (patch) | |
tree | 0b68713d8aa234e861afc8172e392b41ee126248 | |
parent | ee2a192923bf709d05c174848e7054cd411b205a (diff) |
fix o3tl::equalsIgnoreAsciiCase()
As the OUString equivalent shows, it needs to check == 0. Commit
33ecd0d5c4fff9511a8436513936a3f7044a775a for some reason also
dropped the cheap checks (even from OUString) that OString has,
so add them.
Change-Id: I88e68b5ae10fd76c3c08b9b36d5abed0fad17bbf
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130753
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
Tested-by: Jenkins
-rw-r--r-- | include/o3tl/string_view.hxx | 7 | ||||
-rw-r--r-- | include/rtl/ustring.hxx | 4 | ||||
-rw-r--r-- | o3tl/qa/test-string_view.cxx | 8 |
3 files changed, 18 insertions, 1 deletions
diff --git a/include/o3tl/string_view.hxx b/include/o3tl/string_view.hxx index 0ca26a829932..74f15bf33b54 100644 --- a/include/o3tl/string_view.hxx +++ b/include/o3tl/string_view.hxx @@ -23,7 +23,12 @@ namespace o3tl // Like OUString::equalsIgnoreAsciiCase, but for two std::u16string_view: inline bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2) { - return rtl_ustr_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size()); + if (s1.size() != s2.size()) + return false; + if (s1.data() == s2.data()) + return true; + return rtl_ustr_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size()) + == 0; }; // Similar to OString::getToken, returning the first token of a std::string_view, starting at a diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx index cad257caeb58..4d983a089f72 100644 --- a/include/rtl/ustring.hxx +++ b/include/rtl/ustring.hxx @@ -977,6 +977,10 @@ public: */ #if defined LIBO_INTERNAL_ONLY bool equalsIgnoreAsciiCase(std::u16string_view sv) const { + if ( sal_uInt32(pData->length) != sv.size() ) + return false; + if ( pData->buffer == sv.data() ) + return true; return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, sv.data(), sv.size()) diff --git a/o3tl/qa/test-string_view.cxx b/o3tl/qa/test-string_view.cxx index b400c9605962..c068638f800e 100644 --- a/o3tl/qa/test-string_view.cxx +++ b/o3tl/qa/test-string_view.cxx @@ -60,6 +60,7 @@ private: CPPUNIT_TEST(testStartsWithRest); CPPUNIT_TEST(testEndsWith); CPPUNIT_TEST(testEndsWithRest); + CPPUNIT_TEST(testEqualsIgnoreAsciiCase); CPPUNIT_TEST_SUITE_END(); void testStartsWith() @@ -585,6 +586,13 @@ private: CPPUNIT_ASSERT_EQUAL(u""sv, rest); } } + + void testEqualsIgnoreAsciiCase() + { + using namespace std::string_view_literals; + CPPUNIT_ASSERT(o3tl::equalsIgnoreAsciiCase(u"test"sv, u"test"sv)); + CPPUNIT_ASSERT(!o3tl::equalsIgnoreAsciiCase(u"test"sv, u"test2"sv)); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(Test); |