Bug 1719550 - Unify collator in nsXULSortService and nsXULContentUtils; r=platform-i18n-reviewers,nordzilla

Differential Revision: https://phabricator.services.mozilla.com/D121429
This commit is contained in:
Greg Tatum 2021-08-10 16:01:17 +00:00
Родитель 6bd8bc49dc
Коммит 26533165c0
3 изменённых файлов: 41 добавлений и 27 удалений

Просмотреть файл

@ -11,12 +11,12 @@
*/
#include "mozilla/ArrayUtils.h"
#include "mozilla/intl/LocaleService.h"
#include "mozilla/intl/Collator.h"
#include "nsCollationCID.h"
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsIContent.h"
#include "nsICollation.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Element.h"
#include "nsXULContentUtils.h"
@ -28,30 +28,46 @@ using namespace mozilla;
//------------------------------------------------------------------------
nsICollation* nsXULContentUtils::gCollation;
const mozilla::intl::Collator* nsXULContentUtils::gCollator;
//------------------------------------------------------------------------
// Constructors n' stuff
//
nsresult nsXULContentUtils::Finish() {
NS_IF_RELEASE(gCollation);
if (gCollator) {
delete gCollator;
gCollator = nullptr;
}
return NS_OK;
}
nsICollation* nsXULContentUtils::GetCollation() {
if (!gCollation) {
nsCOMPtr<nsICollationFactory> colFactory =
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
if (colFactory) {
DebugOnly<nsresult> rv = colFactory->CreateCollation(&gCollation);
NS_ASSERTION(NS_SUCCEEDED(rv), "couldn't create collation instance");
} else
NS_ERROR("couldn't create instance of collation factory");
const mozilla::intl::Collator* nsXULContentUtils::GetCollator() {
if (!gCollator) {
// Lazily initialize the Collator.
auto result = mozilla::intl::LocaleService::TryCreateComponent<
mozilla::intl::Collator>();
if (result.isErr()) {
NS_ERROR("couldn't create a mozilla::intl::Collator");
return nullptr;
}
auto collator = result.unwrap();
// Sort in a case-insensitive way, where "base" letters are considered
// equal, e.g: a = á, a = A, a ≠ b.
mozilla::intl::Collator::Options options{};
options.sensitivity = mozilla::intl::Collator::Sensitivity::Base;
auto optResult = collator->SetOptions(options);
if (optResult.isErr()) {
NS_ERROR("couldn't set options for mozilla::intl::Collator");
return nullptr;
}
gCollator = collator.release();
}
return gCollation;
return gCollator;
}
//------------------------------------------------------------------------

Просмотреть файл

@ -15,18 +15,18 @@
#include "nsISupports.h"
class nsAtom;
class nsICollation;
class nsIContent;
namespace mozilla {
namespace dom {
namespace mozilla::dom {
class Element;
}
} // namespace mozilla
namespace mozilla::intl {
class Collator;
}
class nsXULContentUtils {
protected:
static nsICollation* gCollation;
const static mozilla::intl::Collator* gCollator;
static bool gDisableXULCache;
@ -39,7 +39,7 @@ class nsXULContentUtils {
static nsresult FindChildByTag(nsIContent* aElement, int32_t aNameSpaceID,
nsAtom* aTag, mozilla::dom::Element** aResult);
static nsICollation* GetCollation();
static const mozilla::intl::Collator* GetCollator();
};
#endif // nsXULContentUtils_h__

Просмотреть файл

@ -18,11 +18,11 @@
#include "nsWhitespaceTokenizer.h"
#include "nsXULSortService.h"
#include "nsXULElement.h"
#include "nsICollation.h"
#include "nsTArray.h"
#include "nsUnicharUtils.h"
#include "mozilla/dom/Element.h"
#include "mozilla/intl/Collator.h"
using mozilla::dom::Element;
const unsigned long SORT_COMPARECASE = 0x0001;
@ -177,12 +177,10 @@ static int32_t CompareValues(const nsAString& aLeft, const nsAString& aRight,
return ::Compare(aLeft, aRight);
}
nsICollation* collation = nsXULContentUtils::GetCollation();
if (collation) {
int32_t result;
collation->CompareString(nsICollation::kCollationCaseInSensitive, aLeft,
aRight, &result);
return result;
using mozilla::intl::Collator;
const Collator* collator = nsXULContentUtils::GetCollator();
if (collator) {
return collator->CompareStrings(aLeft, aRight);
}
return ::Compare(aLeft, aRight, nsCaseInsensitiveStringComparator);