New C++ and Rust functions for comparing user-visible strings for sorting in the UI

14 views
Skip to first unread message

Henri Sivonen

unread,
Mar 16, 2026, 2:38:44 AMMar 16
to firef...@mozilla.org
Hello firefox-dev,

There are now new functions in C++ and Rust for comparing user-visible strings according to the rules of the Firefox UI locale for sorting purposes.

From C++, there is mozilla::intl::AppCollator::Compare(), which is overloaded for potentially-not-well-formed UTF-8 and potentially-not-well-formed UTF-16. That is, nsACString and nsAString arguments work.

From Rust, there is app_collator_glue::compare() for &str. (If you need to compare potentially-not-well-formed UTF-8 or potentially-not-well-formed UTF-16 from Rust, please add the trivial copypaste to delegate to the same back end methods as the C++ API instead of allocating and converting in the caller.)

These functions have the numeric mode turned on, so "10" compares greater than "2". Whether punctuation is ignored (for Thai) or whether upper case sorts first (Danish and Maltese) depends on the language. This corresponds to the defaults of the JavaScript Intl.Collator and localeCompare APIs, except the numeric mode has been turned on.

No case folding or Unicode normalization should be performed on the arguments before passing them to these functions. The back end handles that stuff appropriately.

These functions are only for the Firefox (and Thunderbird) UI. They deliberately ignore the "spoof English" setting, so it's inappropriate to use these functions as the back end of Web-exposed features. For Web-exposed features, use mozilla::intl::Collator::TryCreate() from C++ or icu_collator::CollatorBorrowed::try_new() from Rust after performing the "spoof English" check as appropriate to decide which locale to pass in.

Also, if you have a Rust crate that is meant to work independently of Firefox, then you need to use icu_collator::CollatorBorrowed::try_new() and figure out how to pass the Firefox UI locale to it in the Firefox context.

https://bugzilla.mozilla.org/show_bug.cgi?id=2023163 is on file about making app_collator_glue::compare() available to application-services on mobile.

--
Henri Sivonen
hsiv...@mozilla.com
Reply all
Reply to author
Forward
0 new messages