gecko-dev/intl/locale/nsUConvPropertySearch.cpp
Nika Layzell 997047e2a4 Bug 1772006 - Part 4: Simplify and move the string comparison APIs from ns[T]StringObsolete, r=xpcom-reviewers,necko-reviewers,dragana,barret
This patch moves EqualsIgnoreCase to ns[T]StringObsolete, and removes
the aCount argument, instead migrating callers to use `StringBeginsWith`
with a case-insensitive comparator.

In addition, nsTStringRepr::Compare was removed and replaced with either
calls to methods like `StringBeginsWith` or the global `Compare` method.

These changes required some modifications at call-sites but should make
the behaviour less surprising and more consistent.

Differential Revision: https://phabricator.services.mozilla.com/D148299
2022-07-30 00:12:48 +00:00

41 lines
1.2 KiB
C++

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsUConvPropertySearch.h"
#include "nsCRT.h"
#include "nsString.h"
#include "mozilla/BinarySearch.h"
namespace {
struct PropertyComparator {
const nsCString& mKey;
explicit PropertyComparator(const nsCString& aKey) : mKey(aKey) {}
int operator()(const nsUConvProp& aProperty) const {
return Compare(mKey, nsDependentCString(aProperty.mKey));
}
};
} // namespace
// static
nsresult nsUConvPropertySearch::SearchPropertyValue(
const nsUConvProp aProperties[], int32_t aNumberOfProperties,
const nsACString& aKey, nsACString& aValue) {
using mozilla::BinarySearchIf;
const nsCString& flat = PromiseFlatCString(aKey);
size_t index;
if (BinarySearchIf(aProperties, 0, aNumberOfProperties,
PropertyComparator(flat), &index)) {
nsDependentCString val(aProperties[index].mValue,
aProperties[index].mValueLength);
aValue.Assign(val);
return NS_OK;
}
aValue.Truncate();
return NS_ERROR_FAILURE;
}