Bug 1815558 - Tweak nsAttrValue::Equals(nsAtom*). r=smaug

To avoid an unconditional string copy when comparing case-insensitively
(by using nsDependentAtomString).

Since the string path is not much different than the generic code-path,
just use that elsewhere.

This shows up in selector matching.

Differential Revision: https://phabricator.services.mozilla.com/D169142
This commit is contained in:
Emilio Cobos Álvarez 2023-02-08 00:55:18 +00:00
parent 37a433323c
commit 94d41e8d17

View File

@ -960,8 +960,7 @@ bool nsAttrValue::Equals(const nsAString& aValue,
nsCaseTreatment aCaseSensitive) const { nsCaseTreatment aCaseSensitive) const {
switch (BaseType()) { switch (BaseType()) {
case eStringBase: { case eStringBase: {
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr()); if (auto* str = static_cast<nsStringBuffer*>(GetPtr())) {
if (str) {
nsDependentString dep(static_cast<char16_t*>(str->Data()), nsDependentString dep(static_cast<char16_t*>(str->Data()),
str->StorageSize() / sizeof(char16_t) - 1); str->StorageSize() / sizeof(char16_t) - 1);
return aCaseSensitive == eCaseMatters return aCaseSensitive == eCaseMatters
@ -970,12 +969,14 @@ bool nsAttrValue::Equals(const nsAString& aValue,
} }
return aValue.IsEmpty(); return aValue.IsEmpty();
} }
case eAtomBase: case eAtomBase: {
auto* atom = static_cast<nsAtom*>(GetPtr());
if (aCaseSensitive == eCaseMatters) { if (aCaseSensitive == eCaseMatters) {
return static_cast<nsAtom*>(GetPtr())->Equals(aValue); return atom->Equals(aValue);
}
return nsContentUtils::EqualsIgnoreASCIICase(nsDependentAtomString(atom),
aValue);
} }
return nsContentUtils::EqualsIgnoreASCIICase(
nsDependentAtomString(static_cast<nsAtom*>(GetPtr())), aValue);
default: default:
break; break;
} }
@ -989,33 +990,19 @@ bool nsAttrValue::Equals(const nsAString& aValue,
bool nsAttrValue::Equals(const nsAtom* aValue, bool nsAttrValue::Equals(const nsAtom* aValue,
nsCaseTreatment aCaseSensitive) const { nsCaseTreatment aCaseSensitive) const {
if (aCaseSensitive != eCaseMatters) { if (BaseType() == eAtomBase) {
// Need a better way to handle this! auto* atom = static_cast<nsAtom*>(GetPtr());
nsAutoString value; if (atom == aValue) {
aValue->ToString(value); return true;
return Equals(value, aCaseSensitive);
} }
if (aCaseSensitive == eCaseMatters) {
switch (BaseType()) { return false;
case eStringBase: {
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
if (str) {
nsDependentString dep(static_cast<char16_t*>(str->Data()),
str->StorageSize() / sizeof(char16_t) - 1);
return aValue->Equals(dep);
} }
return aValue == nsGkAtoms::_empty; if (atom->IsAsciiLowercase() && aValue->IsAsciiLowercase()) {
return false;
} }
case eAtomBase: {
return static_cast<nsAtom*>(GetPtr()) == aValue;
} }
default: return Equals(nsDependentAtomString(aValue), aCaseSensitive);
break;
}
nsAutoString val;
ToString(val);
return aValue->Equals(val);
} }
struct HasPrefixFn { struct HasPrefixFn {