From a79f9a79329fa0cbb385c6f9995ae113fa9b4c55 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Fri, 26 Jul 2024 07:56:18 +0200 Subject: [PATCH] Delegate match to main internal starts/endsWith* This simplifies and deduplicates the code, and even where it was simple, it has an advantage that if a change in algorithm is decided, it is done in a single place. Change-Id: I9ec55a29c5f2b2c972ac81bf07c20fc0a52a2a8b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/171033 Reviewed-by: Mike Kaganski Tested-by: Jenkins --- include/rtl/string.hxx | 48 +++++------------- include/rtl/ustring.hxx | 106 ++++++---------------------------------- 2 files changed, 27 insertions(+), 127 deletions(-) diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx index a62a33e2f590..545059a18d91 100644 --- a/include/rtl/string.hxx +++ b/include/rtl/string.hxx @@ -1088,7 +1088,7 @@ public: */ bool startsWith(std::string_view str, OString * rest) const { assert(rest); - bool b = match(str); + bool b = startsWith(str); if (b) { *rest = copy(str.size()); } @@ -1109,7 +1109,7 @@ public: */ bool startsWith(std::string_view str, std::string_view * rest) const { assert(rest); - bool b = match(str); + bool b = startsWith(str); if (b) { *rest = subView(str.size()); } @@ -1163,7 +1163,7 @@ public: { RTL_STRING_CONST_FUNCTION assert(rest); - bool b = match(literal, 0); + bool b = startsWith(literal); if (b) { *rest = copy( libreoffice_internal::ConstCharArrayDetector::length); @@ -1180,7 +1180,7 @@ public: { RTL_STRING_CONST_FUNCTION assert(rest); - bool b = match(literal, 0); + bool b = startsWith(literal); if (b) { *rest = subView( libreoffice_internal::ConstCharArrayDetector::length); @@ -1252,7 +1252,7 @@ public: const { assert(rest); - bool b = matchIgnoreAsciiCase(str); + bool b = startsWithIgnoreAsciiCase(str); if (b) { *rest = copy(str.size()); } @@ -1281,7 +1281,7 @@ public: const { assert(rest); - bool b = matchIgnoreAsciiCase(str); + bool b = startsWithIgnoreAsciiCase(str); if (b) { *rest = subView(str.size()); } @@ -1343,10 +1343,8 @@ public: startsWithIgnoreAsciiCase(T & literal, OString * rest) const { RTL_STRING_CONST_FUNCTION - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b = matchIgnoreAsciiCase(literal); + bool b = startsWithIgnoreAsciiCase(literal); if (b) { *rest = copy( libreoffice_internal::ConstCharArrayDetector::length); @@ -1358,10 +1356,8 @@ public: startsWithIgnoreAsciiCase(T & literal, std::string_view * rest) const { RTL_STRING_CONST_FUNCTION - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b = matchIgnoreAsciiCase(literal); + bool b = startsWithIgnoreAsciiCase(literal); if (b) { *rest = subView( libreoffice_internal::ConstCharArrayDetector::length); @@ -1421,8 +1417,7 @@ public: */ bool endsWith(std::string_view str, OString * rest) const { assert(rest); - bool b = str.size() <= sal_uInt32(getLength()) - && match(str, getLength() - str.size()); + bool b = endsWith(str); if (b) { *rest = copy(0, getLength() - str.size()); } @@ -1443,8 +1438,7 @@ public: */ bool endsWith(std::string_view str, std::string_view * rest) const { assert(rest); - bool b = str.size() <= sal_uInt32(getLength()) - && match(str, getLength() - str.size()); + bool b = endsWith(str); if (b) { *rest = subView(0, getLength() - str.size()); } @@ -1508,17 +1502,8 @@ public: T & literal, OString * rest) const { RTL_STRING_CONST_FUNCTION - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b - = (libreoffice_internal::ConstCharArrayDetector::length - <= sal_uInt32(getLength())) - && match( - libreoffice_internal::ConstCharArrayDetector::toPointer( - literal), - (getLength() - - libreoffice_internal::ConstCharArrayDetector::length)); + bool b = endsWith(literal); if (b) { *rest = copy( 0, @@ -1537,17 +1522,8 @@ public: T & literal, std::string_view * rest) const { RTL_STRING_CONST_FUNCTION - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b - = (libreoffice_internal::ConstCharArrayDetector::length - <= sal_uInt32(getLength())) - && match( - libreoffice_internal::ConstCharArrayDetector::toPointer( - literal), - (getLength() - - libreoffice_internal::ConstCharArrayDetector::length)); + bool b = endsWith(literal); if (b) { *rest = subView( 0, diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx index aeab698c8238..d4f54f76a1f6 100644 --- a/include/rtl/ustring.hxx +++ b/include/rtl/ustring.hxx @@ -1476,7 +1476,7 @@ public: */ bool startsWith(std::u16string_view sv, OUString * rest) const { assert(rest); - auto const b = match(sv); + auto const b = startsWith(sv); if (b) { *rest = copy(sv.size()); } @@ -1497,7 +1497,7 @@ public: */ bool startsWith(std::u16string_view sv, std::u16string_view * rest) const { assert(rest); - auto const b = match(sv); + auto const b = startsWith(sv); if (b) { *rest = subView(sv.size()); } @@ -1557,17 +1557,8 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith( T & literal, OUString * rest) const { - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b - = (libreoffice_internal::ConstCharArrayDetector::length - <= sal_uInt32(pData->length)) - && rtl_ustr_asciil_reverseEquals_WithLength( - pData->buffer, - libreoffice_internal::ConstCharArrayDetector::toPointer( - literal), - libreoffice_internal::ConstCharArrayDetector::length); + bool b = startsWith(literal); if (b) { *rest = copy( libreoffice_internal::ConstCharArrayDetector::length); @@ -1582,17 +1573,8 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith( T & literal, std::u16string_view * rest) const { - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b - = (libreoffice_internal::ConstCharArrayDetector::length - <= sal_uInt32(pData->length)) - && rtl_ustr_asciil_reverseEquals_WithLength( - pData->buffer, - libreoffice_internal::ConstCharArrayDetector::toPointer( - literal), - libreoffice_internal::ConstCharArrayDetector::length); + bool b = startsWith(literal); if (b) { *rest = subView( libreoffice_internal::ConstCharArrayDetector::length); @@ -1653,7 +1635,7 @@ public: } bool startsWithIgnoreAsciiCase(std::u16string_view sv, OUString * rest) const { assert(rest); - auto const b = matchIgnoreAsciiCase(sv); + auto const b = startsWithIgnoreAsciiCase(sv); if (b) { *rest = copy(sv.size()); } @@ -1661,7 +1643,7 @@ public: } bool startsWithIgnoreAsciiCase(std::u16string_view sv, std::u16string_view * rest) const { assert(rest); - auto const b = matchIgnoreAsciiCase(sv); + auto const b = startsWithIgnoreAsciiCase(sv); if (b) { *rest = subView(sv.size()); } @@ -1712,19 +1694,8 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWithIgnoreAsciiCase(T & literal, OUString * rest) const { - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b - = (libreoffice_internal::ConstCharArrayDetector::length - <= sal_uInt32(pData->length)) - && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( - pData->buffer, - libreoffice_internal::ConstCharArrayDetector::length, - libreoffice_internal::ConstCharArrayDetector::toPointer( - literal), - libreoffice_internal::ConstCharArrayDetector::length) - == 0); + bool b = startsWithIgnoreAsciiCase(literal); if (b) { *rest = copy( libreoffice_internal::ConstCharArrayDetector::length); @@ -1739,19 +1710,8 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWithIgnoreAsciiCase(T & literal, std::u16string_view * rest) const { - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b - = (libreoffice_internal::ConstCharArrayDetector::length - <= sal_uInt32(pData->length)) - && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( - pData->buffer, - libreoffice_internal::ConstCharArrayDetector::length, - libreoffice_internal::ConstCharArrayDetector::toPointer( - literal), - libreoffice_internal::ConstCharArrayDetector::length) - == 0); + bool b = startsWithIgnoreAsciiCase(literal); if (b) { *rest = subView( libreoffice_internal::ConstCharArrayDetector::length); @@ -1804,8 +1764,7 @@ public: && match(sv, pData->length - sv.size()); } bool endsWith(std::u16string_view sv, OUString * rest) const { - auto const b = sv.size() <= sal_uInt32(pData->length) - && match(sv, pData->length - sv.size()); + auto const b = endsWith(sv); if (b && rest != nullptr) { *rest = copy(0, (pData->length - sv.size())); } @@ -1826,8 +1785,7 @@ public: */ bool endsWith(std::u16string_view sv, std::u16string_view * rest) const { assert(rest); - auto const b = sv.size() <= sal_uInt32(pData->length) - && match(sv, pData->length - sv.size()); + auto const b = endsWith(sv); if (b) { *rest = subView(0, (pData->length - sv.size())); } @@ -1885,18 +1843,8 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type endsWith(T & literal, OUString * rest) const { - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b - = (libreoffice_internal::ConstCharArrayDetector::length - <= sal_uInt32(pData->length)) - && rtl_ustr_asciil_reverseEquals_WithLength( - (pData->buffer + pData->length - - libreoffice_internal::ConstCharArrayDetector::length), - libreoffice_internal::ConstCharArrayDetector::toPointer( - literal), - libreoffice_internal::ConstCharArrayDetector::length); + bool b = endsWith(literal); if (b) { *rest = copy( 0, @@ -1909,18 +1857,8 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type endsWith(T & literal, std::u16string_view * rest) const { - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b - = (libreoffice_internal::ConstCharArrayDetector::length - <= sal_uInt32(pData->length)) - && rtl_ustr_asciil_reverseEquals_WithLength( - (pData->buffer + pData->length - - libreoffice_internal::ConstCharArrayDetector::length), - libreoffice_internal::ConstCharArrayDetector::toPointer( - literal), - libreoffice_internal::ConstCharArrayDetector::length); + bool b = endsWith(literal); if (b) { *rest = subView( 0, @@ -2006,8 +1944,7 @@ public: && matchIgnoreAsciiCase(sv, pData->length - sv.size()); } bool endsWithIgnoreAsciiCase(std::u16string_view sv, OUString * rest) const { - auto const b = sv.size() <= sal_uInt32(pData->length) - && matchIgnoreAsciiCase(sv, pData->length - sv.size()); + auto const b = endsWithIgnoreAsciiCase(sv); if (b && rest != nullptr) { *rest = copy(0, pData->length - sv.size()); } @@ -2034,8 +1971,7 @@ public: */ bool endsWithIgnoreAsciiCase(std::u16string_view sv, std::u16string_view * rest) const { assert(rest); - auto const b = sv.size() <= sal_uInt32(pData->length) - && matchIgnoreAsciiCase(sv, pData->length - sv.size()); + auto const b = endsWithIgnoreAsciiCase(sv); if (b) { *rest = subView(0, pData->length - sv.size()); } @@ -2105,20 +2041,8 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type endsWithIgnoreAsciiCase(T & literal, std::u16string_view * rest) const { - assert( - libreoffice_internal::ConstCharArrayDetector::isValid(literal)); assert(rest); - bool b - = (libreoffice_internal::ConstCharArrayDetector::length - <= sal_uInt32(pData->length)) - && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( - (pData->buffer + pData->length - - libreoffice_internal::ConstCharArrayDetector::length), - libreoffice_internal::ConstCharArrayDetector::length, - libreoffice_internal::ConstCharArrayDetector::toPointer( - literal), - libreoffice_internal::ConstCharArrayDetector::length) - == 0); + bool b = endsWithIgnoreAsciiCase(literal); if (b) { *rest = subView( 0,