From 1475267b8cb2f7ca50dc1ac08fea764be1094f85 Mon Sep 17 00:00:00 2001 From: Nika Layzell Date: Fri, 10 Jun 2022 21:12:07 +0000 Subject: [PATCH] Bug 1772006 - Part 5: Simplify and move the string comparison APIs from ns[T]StringObsolete, r=xpcom-reviewers,necko-reviewers,dragana,barret In addition to moving these methods to a more appropriate file, they were simplified to make them easier to maintain in the future. nsTStringRepr::Compare was extended to also work on char16_t strings, and the case insensitive and other options were removed as they aren't necessary. This required some changes to callers in the tree. The EqualsIgnoreCase method was also simplified by using `std::string_view`. Differential Revision: https://phabricator.services.mozilla.com/D148299 --- netwerk/dns/DNSPacket.cpp | 2 +- startupcache/StartupCacheUtils.cpp | 5 +- .../backgroundtasks/BackgroundTasks.cpp | 2 +- xpcom/string/nsStringObsolete.cpp | 59 ------------------- xpcom/string/nsTStringRepr.cpp | 14 +++++ xpcom/string/nsTStringRepr.h | 22 ++----- 6 files changed, 23 insertions(+), 81 deletions(-) diff --git a/netwerk/dns/DNSPacket.cpp b/netwerk/dns/DNSPacket.cpp index 68565672f74f..31230620a6d9 100644 --- a/netwerk/dns/DNSPacket.cpp +++ b/netwerk/dns/DNSPacket.cpp @@ -621,7 +621,7 @@ nsresult DNSPacket::DecodeInternal( bool responseMatchesQuestion = (qname.Length() == aHost.Length() || (aHost.Length() == qname.Length() + 1 && aHost.Last() == '.')) && - qname.Compare(aHost.BeginReading(), true, qname.Length()) == 0; + qname.EqualsIgnoreCase(aHost.BeginReading(), qname.Length()); if (responseMatchesQuestion) { // RDATA diff --git a/startupcache/StartupCacheUtils.cpp b/startupcache/StartupCacheUtils.cpp index 34ca60204550..8f3622328ded 100644 --- a/startupcache/StartupCacheUtils.cpp +++ b/startupcache/StartupCacheUtils.cpp @@ -102,9 +102,8 @@ static inline bool canonicalizeBase(nsAutoCString& spec, nsACString& out) { rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::APP, appBase); if (NS_FAILED(rv)) return false; - bool underGre = !greBase.Compare(spec.get(), false, greBase.Length()); - bool underApp = - appBase.Length() && !appBase.Compare(spec.get(), false, appBase.Length()); + bool underGre = StringBeginsWith(spec, greBase); + bool underApp = appBase.Length() && StringBeginsWith(spec, appBase); if (!underGre && !underApp) return false; diff --git a/toolkit/components/backgroundtasks/BackgroundTasks.cpp b/toolkit/components/backgroundtasks/BackgroundTasks.cpp index 158aca318c53..f59dadff5383 100644 --- a/toolkit/components/backgroundtasks/BackgroundTasks.cpp +++ b/toolkit/components/backgroundtasks/BackgroundTasks.cpp @@ -276,7 +276,7 @@ nsresult BackgroundTasks::RemoveStaleTemporaryProfileDirectories( } // Find profile folders matching our prefix. - if (aPrefix.Compare(entryName.get(), false, aPrefix.Length()) != 0) { + if (!StringBeginsWith(entryName, aPrefix)) { continue; } diff --git a/xpcom/string/nsStringObsolete.cpp b/xpcom/string/nsStringObsolete.cpp index 62d01ffde060..9a64e5e6e17c 100644 --- a/xpcom/string/nsStringObsolete.cpp +++ b/xpcom/string/nsStringObsolete.cpp @@ -874,62 +874,3 @@ void nsTSubstring::ReplaceChar(const char* aSet, char16_t aNewChar) { lenRemaining -= i; } } - -namespace mozilla::detail { - -template -template -int32_t nsTStringRepr::Compare(const char_type* aString, bool aIgnoreCase, - size_type aCount) const { - size_t strLen = char_traits::length(aString); - - size_t minLen = XPCOM_MIN(this->Length(), strLen); - - // NOTE: As `minLen <= this->Length()` this value must fit in a `uint32_t`. - uint32_t compareCount = - ReleaseAssertedCast(XPCOM_MIN(minLen, aCount)); - - int32_t result = nsBufferRoutines::compare(this->mData, aString, - compareCount, aIgnoreCase); - - if (result == 0 && minLen < aCount && this->Length() != strLen) { - // Since the caller didn't give us a length to test, or strings shorter - // than aCount, and compareCount characters matched, we have to assume - // that the longer string is greater. - - return (this->Length() < strLen) ? -1 : 1; - } - return result; -} - -template int32_t nsTStringRepr::Compare(const char_type*, bool, - size_type) const; - -template -template -bool nsTStringRepr::EqualsIgnoreCase(const incompatible_char_type* aString, - size_type aCount) const { - size_t strLen = nsCharTraits::length(aString); - - size_t minLen = XPCOM_MIN(this->Length(), strLen); - - // NOTE: As `minLen <= this->Length()` this value must fit in a `uint32_t`. - uint32_t compareCount = - ReleaseAssertedCast(XPCOM_MIN(minLen, aCount)); - - int32_t result = - nsBufferRoutines::compare(this->mData, aString, compareCount, true); - - if (result == 0 && minLen < aCount && this->Length() != strLen) { - // Since the caller didn't give us a length to test, or strings shorter - // than aCount, and compareCount characters matched, we have to assume - // that the longer string is greater. - return false; - } - return result == 0; -} - -template bool nsTStringRepr::EqualsIgnoreCase( - const incompatible_char_type*, size_type) const; - -} // namespace mozilla::detail diff --git a/xpcom/string/nsTStringRepr.cpp b/xpcom/string/nsTStringRepr.cpp index bba31b8cb7a5..f62d2c240da2 100644 --- a/xpcom/string/nsTStringRepr.cpp +++ b/xpcom/string/nsTStringRepr.cpp @@ -107,4 +107,18 @@ bool nsTStringRepr::LowerCaseEqualsASCII(const char* aData) const { this->mData, this->mLength, aData) == 0; } +template +int32_t nsTStringRepr::Compare(const string_view& aString) const { + return View().compare(aString); +} + +template +bool nsTStringRepr::EqualsIgnoreCase(const std::string_view& aString) const { + return std::equal(BeginReading(), EndReading(), aString.begin(), + aString.end(), [](char_type l, char r) { + return char_traits::ASCIIToLower(l) == + char_traits::ASCIIToLower(char_type(r)); + }); +} + } // namespace mozilla::detail diff --git a/xpcom/string/nsTStringRepr.h b/xpcom/string/nsTStringRepr.h index 8c14d914bd14..55afd172b6df 100644 --- a/xpcom/string/nsTStringRepr.h +++ b/xpcom/string/nsTStringRepr.h @@ -237,35 +237,23 @@ class nsTStringRepr { * Compares a given string to this string. * * @param aString is the string to be compared - * @param aIgnoreCase tells us how to treat case - * @param aCount tells us how many chars to compare * @return -1,0,1 */ - template > - int32_t Compare( - const char_type* aString, bool aIgnoreCase = false, - size_type aCount = std::numeric_limits::max()) const; + int32_t Compare(const string_view& aString) const; /** * Equality check between given string and this string. * * @param aString is the string to check - * @param aIgnoreCase tells us how to treat case * @param aCount tells us how many chars to compare * @return boolean */ - template > - bool EqualsIgnoreCase( - const char_type* aString, - size_type aCount = std::numeric_limits::max()) const { - return Compare(aString, true, aCount) == 0; + bool EqualsIgnoreCase(const std::string_view& aString) const; + bool EqualsIgnoreCase(const char* aString, size_type aCount) const { + MOZ_DIAGNOSTIC_ASSERT(aCount != std::numeric_limits::max()); + return EqualsIgnoreCase(std::string_view(aString, aCount)); } - template > - bool EqualsIgnoreCase( - const incompatible_char_type* aString, - size_type aCount = std::numeric_limits::max()) const; - #if defined(MOZ_USE_CHAR16_WRAPPER) template > bool NS_FASTCALL Equals(char16ptr_t aData) const {