Loading...
Searching...
No Matches
localematcher.h
Go to the documentation of this file.
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
// localematcher.h
// created: 2019may08 Markus W. Scherer
#ifndef __LOCALEMATCHER_H__
#define __LOCALEMATCHER_H__
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#include <optional>
#include "unicode/locid.h"
#include "unicode/stringpiece.h"
#include "unicode/uobject.h"
enum ULocMatchFavorSubtag {
ULOCMATCH_FAVOR_LANGUAGE,
ULOCMATCH_FAVOR_SCRIPT
};
#ifndef U_IN_DOXYGEN
typedef enum ULocMatchFavorSubtag ULocMatchFavorSubtag;
#endif
enum ULocMatchDemotion {
ULOCMATCH_DEMOTION_NONE,
ULOCMATCH_DEMOTION_REGION
};
#ifndef U_IN_DOXYGEN
typedef enum ULocMatchDemotion ULocMatchDemotion;
#endif
enum ULocMatchDirection {
ULOCMATCH_DIRECTION_WITH_ONE_WAY,
ULOCMATCH_DIRECTION_ONLY_TWO_WAY
};
#ifndef U_IN_DOXYGEN
typedef enum ULocMatchDirection ULocMatchDirection;
#endif
struct UHashtable;
U_NAMESPACE_BEGIN
struct LSR;
class LikelySubtags;
class LocaleDistance;
class LocaleLsrIterator;
class UVector;
class U_COMMON_API LocaleMatcher : public UMemory {
public:
class U_COMMON_API Result : public UMemory {
public:
Result(Result &&src) noexcept;
~Result();
Result &operator=(Result &&src) noexcept;
inline const Locale *getDesiredLocale() const { return desiredLocale; }
inline const Locale *getSupportedLocale() const { return supportedLocale; }
inline int32_t getDesiredIndex() const { return desiredIndex; }
inline int32_t getSupportedIndex() const { return supportedIndex; }
Locale makeResolvedLocale(UErrorCode &errorCode) const;
private:
Result(const Locale *desired, const Locale *supported,
int32_t desIndex, int32_t suppIndex, UBool owned) :
desiredLocale(desired), supportedLocale(supported),
desiredIndex(desIndex), supportedIndex(suppIndex),
desiredIsOwned(owned) {}
Result(const Result &other) = delete;
Result &operator=(const Result &other) = delete;
const Locale *desiredLocale;
const Locale *supportedLocale;
int32_t desiredIndex;
int32_t supportedIndex;
UBool desiredIsOwned;
friend class LocaleMatcher;
};
class U_COMMON_API Builder : public UMemory {
public:
Builder() {}
Builder(Builder &&src) noexcept;
~Builder();
Builder &operator=(Builder &&src) noexcept;
Builder &setSupportedLocalesFromListString(StringPiece locales);
Builder &setSupportedLocales(Locale::Iterator &locales);
template<typename Iter>
Builder &setSupportedLocales(Iter begin, Iter end) {
if (U_FAILURE(errorCode_)) { return *this; }
clearSupportedLocales();
while (begin != end) {
addSupportedLocale(*begin++);
}
return *this;
}
template<typename Iter, typename Conv>
Builder &setSupportedLocalesViaConverter(Iter begin, Iter end, Conv converter) {
if (U_FAILURE(errorCode_)) { return *this; }
clearSupportedLocales();
while (begin != end) {
addSupportedLocale(converter(*begin++));
}
return *this;
}
Builder &addSupportedLocale(const Locale &locale);
Builder &setNoDefaultLocale();
Builder &setDefaultLocale(const Locale *defaultLocale);
Builder &setFavorSubtag(ULocMatchFavorSubtag subtag);
Builder &setDemotionPerDesiredLocale(ULocMatchDemotion demotion);
Builder &setDirection(ULocMatchDirection matchDirection) {
if (U_SUCCESS(errorCode_)) {
direction_ = matchDirection;
}
return *this;
}
Builder &setMaxDistance(const Locale &desired, const Locale &supported);
UBool copyErrorTo(UErrorCode &outErrorCode) const;
LocaleMatcher build(UErrorCode &errorCode) const;
private:
friend class LocaleMatcher;
Builder(const Builder &other) = delete;
Builder &operator=(const Builder &other) = delete;
void clearSupportedLocales();
bool ensureSupportedLocaleVector();
UErrorCode errorCode_ = U_ZERO_ERROR;
UVector *supportedLocales_ = nullptr;
int32_t thresholdDistance_ = -1;
ULocMatchDemotion demotion_ = ULOCMATCH_DEMOTION_REGION;
Locale *defaultLocale_ = nullptr;
bool withDefault_ = true;
ULocMatchFavorSubtag favor_ = ULOCMATCH_FAVOR_LANGUAGE;
ULocMatchDirection direction_ = ULOCMATCH_DIRECTION_WITH_ONE_WAY;
Locale *maxDistanceDesired_ = nullptr;
Locale *maxDistanceSupported_ = nullptr;
};
// FYI No public LocaleMatcher constructors in C++; use the Builder.
LocaleMatcher(LocaleMatcher &&src) noexcept;
~LocaleMatcher();
LocaleMatcher &operator=(LocaleMatcher &&src) noexcept;
const Locale *getBestMatch(const Locale &desiredLocale, UErrorCode &errorCode) const;
const Locale *getBestMatch(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const;
const Locale *getBestMatchForListString(StringPiece desiredLocaleList, UErrorCode &errorCode) const;
Result getBestMatchResult(const Locale &desiredLocale, UErrorCode &errorCode) const;
Result getBestMatchResult(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const;
UBool isMatch(const Locale &desired, const Locale &supported, UErrorCode &errorCode) const;
#ifndef U_HIDE_INTERNAL_API
double internalMatch(const Locale &desired, const Locale &supported, UErrorCode &errorCode) const;
#endif // U_HIDE_INTERNAL_API
private:
LocaleMatcher(const Builder &builder, UErrorCode &errorCode);
LocaleMatcher(const LocaleMatcher &other) = delete;
LocaleMatcher &operator=(const LocaleMatcher &other) = delete;
int32_t putIfAbsent(const LSR &lsr, int32_t i, int32_t suppLength, UErrorCode &errorCode);
std::optional<int32_t> getBestSuppIndex(LSR desiredLSR, LocaleLsrIterator *remainingIter, UErrorCode &errorCode) const;
const LikelySubtags &likelySubtags;
const LocaleDistance &localeDistance;
int32_t thresholdDistance;
int32_t demotionPerDesiredLocale;
ULocMatchFavorSubtag favorSubtag;
ULocMatchDirection direction;
// These are in input order.
const Locale ** supportedLocales;
LSR *lsrs;
int32_t supportedLocalesLength;
// These are in preference order: 1. Default locale 2. paradigm locales 3. others.
UHashtable *supportedLsrToIndex; // Map<LSR, Integer>
// Array versions of the supportedLsrToIndex keys and values.
// The distance lookup loops over the supportedLSRs and returns the index of the best match.
const LSR **supportedLSRs;
int32_t *supportedIndexes;
int32_t supportedLSRsLength;
Locale *ownedDefaultLocale;
const Locale *defaultLocale;
};
U_NAMESPACE_END
#endif // U_SHOW_CPLUSPLUS_API
#endif // __LOCALEMATCHER_H__
icu::LocaleMatcher::Builder
LocaleMatcher builder.
Definition localematcher.h:301
icu::LocaleMatcher::Builder::setMaxDistance
Builder & setMaxDistance(const Locale &desired, const Locale &supported)
Sets the maximum distance for an acceptable match.
icu::LocaleMatcher::Builder::Builder
Builder(Builder &&src) noexcept
Move constructor; might modify the source.
icu::LocaleMatcher::Builder::setSupportedLocalesViaConverter
Builder & setSupportedLocalesViaConverter(Iter begin, Iter end, Conv converter)
Copies the supported locales from the begin/end range, preserving iteration order.
Definition localematcher.h:399
icu::LocaleMatcher::Builder::operator=
Builder & operator=(Builder &&src) noexcept
Move assignment; might modify the source.
icu::LocaleMatcher::Builder::~Builder
~Builder()
Destructor.
icu::LocaleMatcher::Builder::build
LocaleMatcher build(UErrorCode &errorCode) const
Builds and returns a new locale matcher.
icu::LocaleMatcher::Builder::setSupportedLocales
Builder & setSupportedLocales(Locale::Iterator &locales)
Copies the supported locales, preserving iteration order.
icu::LocaleMatcher::Builder::setDefaultLocale
Builder & setDefaultLocale(const Locale *defaultLocale)
Sets the default locale; if nullptr, or if it is not set explicitly, then the first supported locale ...
icu::LocaleMatcher::Builder::setDemotionPerDesiredLocale
Builder & setDemotionPerDesiredLocale(ULocMatchDemotion demotion)
Option for whether all desired locales are treated equally or earlier ones are preferred (this is the...
icu::LocaleMatcher::Builder::setDirection
Builder & setDirection(ULocMatchDirection matchDirection)
Option for whether to include or ignore one-way (fallback) match data.
Definition localematcher.h:470
icu::LocaleMatcher::Builder::addSupportedLocale
Builder & addSupportedLocale(const Locale &locale)
Adds another supported locale.
icu::LocaleMatcher::Builder::setFavorSubtag
Builder & setFavorSubtag(ULocMatchFavorSubtag subtag)
If ULOCMATCH_FAVOR_SCRIPT, then the language differences are smaller than script differences.
icu::LocaleMatcher::Builder::setSupportedLocalesFromListString
Builder & setSupportedLocalesFromListString(StringPiece locales)
Parses an Accept-Language string (RFC 2616 Section 14.4), such as "af, en, fr;q=0....
icu::LocaleMatcher::Builder::setNoDefaultLocale
Builder & setNoDefaultLocale()
Sets no default locale.
icu::LocaleMatcher::Builder::setSupportedLocales
Builder & setSupportedLocales(Iter begin, Iter end)
Copies the supported locales from the begin/end range, preserving iteration order.
Definition localematcher.h:374
icu::LocaleMatcher::Builder::copyErrorTo
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred while setting parameters.
icu::LocaleMatcher::Builder::Builder
Builder()
Constructs a builder used in chaining parameters for building a LocaleMatcher.
Definition localematcher.h:309
icu::LocaleMatcher::Result
Data for the best-matching pair of a desired and a supported locale.
Definition localematcher.h:194
icu::LocaleMatcher::Result::getDesiredLocale
const Locale * getDesiredLocale() const
Returns the best-matching desired locale.
Definition localematcher.h:228
icu::LocaleMatcher::Result::getSupportedIndex
int32_t getSupportedIndex() const
Returns the index of the best-matching supported locale in the constructor’s or builder’s input order...
Definition localematcher.h:260
icu::LocaleMatcher::Result::getSupportedLocale
const Locale * getSupportedLocale() const
Returns the best-matching supported locale.
Definition localematcher.h:239
icu::LocaleMatcher::Result::getDesiredIndex
int32_t getDesiredIndex() const
Returns the index of the best-matching desired locale in the input Iterable order.
Definition localematcher.h:248
icu::LocaleMatcher::Result::makeResolvedLocale
Locale makeResolvedLocale(UErrorCode &errorCode) const
Takes the best-matching supported locale and adds relevant fields of the best-matching desired locale...
icu::LocaleMatcher::Result::~Result
~Result()
Destructor.
icu::LocaleMatcher::Result::Result
Result(Result &&src) noexcept
Move constructor; might modify the source.
icu::LocaleMatcher::Result::operator=
Result & operator=(Result &&src) noexcept
Move assignment; might modify the source.
icu::LocaleMatcher::getBestMatch
const Locale * getBestMatch(const Locale &desiredLocale, UErrorCode &errorCode) const
Returns the supported locale which best matches the desired locale.
icu::LocaleMatcher::LocaleMatcher
LocaleMatcher(LocaleMatcher &&src) noexcept
Move copy constructor; might modify the source.
icu::LocaleMatcher::isMatch
UBool isMatch(const Locale &desired, const Locale &supported, UErrorCode &errorCode) const
Returns true if the pair of locales matches acceptably.
icu::LocaleMatcher::getBestMatchResult
Result getBestMatchResult(const Locale &desiredLocale, UErrorCode &errorCode) const
Returns the best match between the desired locale and the supported locales.
icu::LocaleMatcher::getBestMatchResult
Result getBestMatchResult(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const
Returns the best match between the desired and supported locales.
icu::LocaleMatcher::internalMatch
double internalMatch(const Locale &desired, const Locale &supported, UErrorCode &errorCode) const
Returns a fraction between 0 and 1, where 1 means that the languages are a perfect match,...
icu::LocaleMatcher::operator=
LocaleMatcher & operator=(LocaleMatcher &&src) noexcept
Move assignment operator; might modify the source.
icu::LocaleMatcher::getBestMatchForListString
const Locale * getBestMatchForListString(StringPiece desiredLocaleList, UErrorCode &errorCode) const
Parses an Accept-Language string (RFC 2616 Section 14.4), such as "af, en, fr;q=0....
icu::LocaleMatcher::~LocaleMatcher
~LocaleMatcher()
Destructor.
icu::LocaleMatcher::getBestMatch
const Locale * getBestMatch(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const
Returns the supported locale which best matches one of the desired locales.
icu::Locale::Iterator
A Locale iterator interface similar to a Java Iterator<Locale>.
Definition locid.h:1035
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:198
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition stringpiece.h:61
icu::UMemory
UMemory is the common ICU base class.
Definition uobject.h:115
ULocMatchDemotion
ULocMatchDemotion
Builder option for whether all desired locales are treated equally or earlier ones are preferred.
Definition localematcher.h:57
ULOCMATCH_DEMOTION_REGION
@ ULOCMATCH_DEMOTION_REGION
Earlier desired locales are preferred.
Definition localematcher.h:90
ULOCMATCH_DEMOTION_NONE
@ ULOCMATCH_DEMOTION_NONE
All desired locales are treated equally.
Definition localematcher.h:63
ULocMatchDirection
ULocMatchDirection
Builder option for whether to include or ignore one-way (fallback) match data.
Definition localematcher.h:113
ULOCMATCH_DIRECTION_ONLY_TWO_WAY
@ ULOCMATCH_DIRECTION_ONLY_TWO_WAY
Locale matching limited to two-way matches including e.g.
Definition localematcher.h:126
ULOCMATCH_DIRECTION_WITH_ONE_WAY
@ ULOCMATCH_DIRECTION_WITH_ONE_WAY
Locale matching includes one-way matches such as Breton→French.
Definition localematcher.h:119
ULocMatchFavorSubtag
ULocMatchFavorSubtag
Builder option for whether the language subtag or the script subtag is most important.
Definition localematcher.h:31
ULOCMATCH_FAVOR_SCRIPT
@ ULOCMATCH_FAVOR_SCRIPT
Makes script differences matter relatively more than language differences.
Definition localematcher.h:44
ULOCMATCH_FAVOR_LANGUAGE
@ ULOCMATCH_FAVOR_LANGUAGE
Language differences are most important, then script differences, then region differences.
Definition localematcher.h:38
locid.h
C++ API: Locale ID object.
UHashtable
struct UHashtable UHashtable
Definition msgfmt.h:43
stringpiece.h
C++ API: StringPiece: Read-only byte string wrapper class.
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
uobject.h
C++ API: Common ICU base class UObject.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:509
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition utypes.h:544
U_FAILURE
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition utypes.h:827
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition utypes.h:822
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition utypes.h:315