Bug 1730119 - Rewrite XSLT path result compare to use Collator::CompareStrings r=dom-core,hsivonen

Use `Collator::CompareStrings` instead of sort keys (see bug for more info).

For now I just removed `mCaseKey`/etc as it is unclear if it has a use
today, if so I can add it back in when we have the info on it.

Differential Revision: https://phabricator.services.mozilla.com/D192349
This commit is contained in:
Oliver Medhurst 2023-11-03 12:59:31 +00:00
parent 392b0cedb4
commit afb396d8fd
2 changed files with 10 additions and 69 deletions

View File

@ -58,87 +58,34 @@ nsresult txResultStringComparator::createSortableValue(Expr* aExpr,
return NS_ERROR_FAILURE;
}
val->mCaseKeyString = MakeUnique<nsString>();
nsString& nsCaseKey = *val->mCaseKeyString;
nsresult rv = aExpr->evaluateToString(aContext, nsCaseKey);
val->mString = MakeUnique<nsString>();
nsString& string = *val->mString;
nsresult rv = aExpr->evaluateToString(aContext, string);
NS_ENSURE_SUCCESS(rv, rv);
if (nsCaseKey.IsEmpty()) {
aResult = val.release();
return NS_OK;
}
auto result = mCollator->GetSortKey(nsCaseKey, val->mKey);
NS_ENSURE_TRUE(result.isOk(), NS_ERROR_FAILURE);
aResult = val.release();
return NS_OK;
}
int txResultStringComparator::compareValues(txObject* aVal1, txObject* aVal2) {
StringValue* strval1 = (StringValue*)aVal1;
StringValue* strval2 = (StringValue*)aVal2;
nsString& dval1 = *((StringValue*)aVal1)->mString;
nsString& dval2 = *((StringValue*)aVal2)->mString;
if (!mCollator) {
MOZ_ASSERT_UNREACHABLE("No mCollator");
return -1;
}
if (strval1->mKey.Length() == 0) {
if (strval2->mKey.Length() == 0) {
return 0;
}
return ((mSorting & kAscending) ? -1 : 1);
}
int32_t result = mCollator->CompareStrings(dval1, dval2);
if (strval2->mKey.Length() == 0) {
return ((mSorting & kAscending) ? 1 : -1);
}
nsresult rv;
int32_t result = mCollator->CompareSortKeys(strval1->mKey, strval2->mKey);
if (result != 0) {
return ((mSorting & kAscending) ? 1 : -1) * result;
}
if (strval1->mCaseKeyString && strval1->mKey.Length() != 0) {
rv = strval1->initCaseKey(*mCollator);
if (NS_FAILED(rv)) {
// XXX ErrorReport
return -1;
}
}
if (strval2->mCaseKeyString && strval2->mKey.Length() != 0) {
rv = strval2->initCaseKey(*mCollator);
if (NS_FAILED(rv)) {
// XXX ErrorReport
return -1;
}
}
result = mCollator->CompareSortKeys(strval1->mCaseKey, strval2->mCaseKey);
return ((mSorting & kAscending) ? 1 : -1) *
((mSorting & kUpperFirst) ? -1 : 1) * result;
return (mSorting & kAscending) ? result : -result;
}
txResultStringComparator::StringValue::StringValue() = default;
txResultStringComparator::StringValue::~StringValue() = default;
nsresult txResultStringComparator::StringValue::initCaseKey(
const mozilla::intl::Collator& aCollator) {
auto result = aCollator.GetSortKey(*mCaseKeyString, mCaseKey);
if (result.isErr()) {
mCaseKey.SetLength(0);
return NS_ERROR_FAILURE;
}
mCaseKeyString = nullptr;
return NS_OK;
}
txResultNumberComparator::txResultNumberComparator(bool aAscending) {
mAscending = aAscending ? 1 : -1;
}

View File

@ -38,7 +38,7 @@ class txXPathResultComparator {
};
/*
* Compare results as stings (data-type="text")
* Compare results as strings (data-type="text")
*/
class txResultStringComparator : public txXPathResultComparator {
public:
@ -59,13 +59,7 @@ class txResultStringComparator : public txXPathResultComparator {
StringValue();
~StringValue();
nsresult initCaseKey(const mozilla::intl::Collator& aCollator);
nsTArray<uint8_t> mKey;
// Either mCaseKeyString is non-null, or we have a usable key in mCaseKey
// already.
mozilla::UniquePtr<nsString> mCaseKeyString;
nsTArray<uint8_t> mCaseKey;
mozilla::UniquePtr<nsString> mString;
};
};