mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-23 21:01:08 +00:00
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
This commit is contained in:
parent
f9e47898c5
commit
1475267b8c
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -874,62 +874,3 @@ void nsTSubstring<T>::ReplaceChar(const char* aSet, char16_t aNewChar) {
|
||||
lenRemaining -= i;
|
||||
}
|
||||
}
|
||||
|
||||
namespace mozilla::detail {
|
||||
|
||||
template <typename T>
|
||||
template <typename Q, typename EnableIfChar>
|
||||
int32_t nsTStringRepr<T>::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<uint32_t>(XPCOM_MIN(minLen, aCount));
|
||||
|
||||
int32_t result = nsBufferRoutines<T>::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<char>::Compare(const char_type*, bool,
|
||||
size_type) const;
|
||||
|
||||
template <typename T>
|
||||
template <typename Q, typename EnableIfChar16>
|
||||
bool nsTStringRepr<T>::EqualsIgnoreCase(const incompatible_char_type* aString,
|
||||
size_type aCount) const {
|
||||
size_t strLen = nsCharTraits<char>::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<uint32_t>(XPCOM_MIN(minLen, aCount));
|
||||
|
||||
int32_t result =
|
||||
nsBufferRoutines<T>::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<char16_t>::EqualsIgnoreCase(
|
||||
const incompatible_char_type*, size_type) const;
|
||||
|
||||
} // namespace mozilla::detail
|
||||
|
@ -107,4 +107,18 @@ bool nsTStringRepr<T>::LowerCaseEqualsASCII(const char* aData) const {
|
||||
this->mData, this->mLength, aData) == 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int32_t nsTStringRepr<T>::Compare(const string_view& aString) const {
|
||||
return View().compare(aString);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool nsTStringRepr<T>::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
|
||||
|
@ -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 <typename Q = T, typename EnableIfChar = mozilla::CharOnlyT<Q>>
|
||||
int32_t Compare(
|
||||
const char_type* aString, bool aIgnoreCase = false,
|
||||
size_type aCount = std::numeric_limits<size_type>::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 <typename Q = T, typename EnableIfChar = mozilla::CharOnlyT<Q>>
|
||||
bool EqualsIgnoreCase(
|
||||
const char_type* aString,
|
||||
size_type aCount = std::numeric_limits<size_type>::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<size_type>::max());
|
||||
return EqualsIgnoreCase(std::string_view(aString, aCount));
|
||||
}
|
||||
|
||||
template <typename Q = T, typename EnableIfChar16 = mozilla::Char16OnlyT<Q>>
|
||||
bool EqualsIgnoreCase(
|
||||
const incompatible_char_type* aString,
|
||||
size_type aCount = std::numeric_limits<size_type>::max()) const;
|
||||
|
||||
#if defined(MOZ_USE_CHAR16_WRAPPER)
|
||||
template <typename Q = T, typename EnableIfChar16 = Char16OnlyT<Q>>
|
||||
bool NS_FASTCALL Equals(char16ptr_t aData) const {
|
||||
|
Loading…
Reference in New Issue
Block a user