Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-17 09:32:17

0001 // © 2019 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 
0004 // localematcher.h
0005 // created: 2019may08 Markus W. Scherer
0006 
0007 #ifndef __LOCALEMATCHER_H__
0008 #define __LOCALEMATCHER_H__
0009 
0010 #include "unicode/utypes.h"
0011 
0012 #if U_SHOW_CPLUSPLUS_API
0013 
0014 #include <optional>
0015 
0016 #include "unicode/locid.h"
0017 #include "unicode/stringpiece.h"
0018 #include "unicode/uobject.h"
0019 
0020 /**
0021  * \file
0022  * \brief C++ API: Locale matcher: User's desired locales vs. application's supported locales.
0023  */
0024 
0025 /**
0026  * Builder option for whether the language subtag or the script subtag is most important.
0027  *
0028  * @see LocaleMatcher::Builder#setFavorSubtag(ULocMatchFavorSubtag)
0029  * @stable ICU 65
0030  */
0031 enum ULocMatchFavorSubtag {
0032     /**
0033      * Language differences are most important, then script differences, then region differences.
0034      * (This is the default behavior.)
0035      *
0036      * @stable ICU 65
0037      */
0038     ULOCMATCH_FAVOR_LANGUAGE,
0039     /**
0040      * Makes script differences matter relatively more than language differences.
0041      *
0042      * @stable ICU 65
0043      */
0044     ULOCMATCH_FAVOR_SCRIPT
0045 };
0046 #ifndef U_IN_DOXYGEN
0047 typedef enum ULocMatchFavorSubtag ULocMatchFavorSubtag;
0048 #endif
0049 
0050 /**
0051  * Builder option for whether all desired locales are treated equally or
0052  * earlier ones are preferred.
0053  *
0054  * @see LocaleMatcher::Builder#setDemotionPerDesiredLocale(ULocMatchDemotion)
0055  * @stable ICU 65
0056  */
0057 enum ULocMatchDemotion {
0058     /**
0059      * All desired locales are treated equally.
0060      *
0061      * @stable ICU 65
0062      */
0063     ULOCMATCH_DEMOTION_NONE,
0064     /**
0065      * Earlier desired locales are preferred.
0066      *
0067      * <p>From each desired locale to the next,
0068      * the distance to any supported locale is increased by an additional amount
0069      * which is at least as large as most region mismatches.
0070      * A later desired locale has to have a better match with some supported locale
0071      * due to more than merely having the same region subtag.
0072      *
0073      * <p>For example: <code>Supported={en, sv}  desired=[en-GB, sv]</code>
0074      * yields <code>Result(en-GB, en)</code> because
0075      * with the demotion of sv its perfect match is no better than
0076      * the region distance between the earlier desired locale en-GB and en=en-US.
0077      *
0078      * <p>Notes:
0079      * <ul>
0080      *   <li>In some cases, language and/or script differences can be as small as
0081      *       the typical region difference. (Example: sr-Latn vs. sr-Cyrl)
0082      *   <li>It is possible for certain region differences to be larger than usual,
0083      *       and larger than the demotion.
0084      *       (As of CLDR 35 there is no such case, but
0085      *        this is possible in future versions of the data.)
0086      * </ul>
0087      *
0088      * @stable ICU 65
0089      */
0090     ULOCMATCH_DEMOTION_REGION
0091 };
0092 #ifndef U_IN_DOXYGEN
0093 typedef enum ULocMatchDemotion ULocMatchDemotion;
0094 #endif
0095 
0096 /**
0097  * Builder option for whether to include or ignore one-way (fallback) match data.
0098  * The LocaleMatcher uses CLDR languageMatch data which includes fallback (oneway=true) entries.
0099  * Sometimes it is desirable to ignore those.
0100  *
0101  * <p>For example, consider a web application with the UI in a given language,
0102  * with a link to another, related web app.
0103  * The link should include the UI language, and the target server may also use
0104  * the client’s Accept-Language header data.
0105  * The target server has its own list of supported languages.
0106  * One may want to favor UI language consistency, that is,
0107  * if there is a decent match for the original UI language, we want to use it,
0108  * but not if it is merely a fallback.
0109  *
0110  * @see LocaleMatcher::Builder#setDirection(ULocMatchDirection)
0111  * @stable ICU 67
0112  */
0113 enum ULocMatchDirection {
0114     /**
0115      * Locale matching includes one-way matches such as Breton→French. (default)
0116      *
0117      * @stable ICU 67
0118      */
0119     ULOCMATCH_DIRECTION_WITH_ONE_WAY,
0120     /**
0121      * Locale matching limited to two-way matches including e.g. Danish↔Norwegian
0122      * but ignoring one-way matches.
0123      *
0124      * @stable ICU 67
0125      */
0126     ULOCMATCH_DIRECTION_ONLY_TWO_WAY
0127 };
0128 #ifndef U_IN_DOXYGEN
0129 typedef enum ULocMatchDirection ULocMatchDirection;
0130 #endif
0131 
0132 struct UHashtable;
0133 
0134 U_NAMESPACE_BEGIN
0135 
0136 struct LSR;
0137 
0138 class LikelySubtags;
0139 class LocaleDistance;
0140 class LocaleLsrIterator;
0141 class UVector;
0142 
0143 /**
0144  * Immutable class that picks the best match between a user's desired locales and
0145  * an application's supported locales.
0146  * Movable but not copyable.
0147  *
0148  * <p>Example:
0149  * <pre>
0150  * UErrorCode errorCode = U_ZERO_ERROR;
0151  * LocaleMatcher matcher = LocaleMatcher::Builder().setSupportedLocales("fr, en-GB, en").build(errorCode);
0152  * Locale *bestSupported = matcher.getBestLocale(Locale.US, errorCode);  // "en"
0153  * </pre>
0154  *
0155  * <p>A matcher takes into account when languages are close to one another,
0156  * such as Danish and Norwegian,
0157  * and when regional variants are close, like en-GB and en-AU as opposed to en-US.
0158  *
0159  * <p>If there are multiple supported locales with the same (language, script, region)
0160  * likely subtags, then the current implementation returns the first of those locales.
0161  * It ignores variant subtags (except for pseudolocale variants) and extensions.
0162  * This may change in future versions.
0163  *
0164  * <p>For example, the current implementation does not distinguish between
0165  * de, de-DE, de-Latn, de-1901, de-u-co-phonebk.
0166  *
0167  * <p>If you prefer one equivalent locale over another, then provide only the preferred one,
0168  * or place it earlier in the list of supported locales.
0169  *
0170  * <p>Otherwise, the order of supported locales may have no effect on the best-match results.
0171  * The current implementation compares each desired locale with supported locales
0172  * in the following order:
0173  * 1. Default locale, if supported;
0174  * 2. CLDR "paradigm locales" like en-GB and es-419;
0175  * 3. other supported locales.
0176  * This may change in future versions.
0177  *
0178  * <p>Often a product will just need one matcher instance, built with the languages
0179  * that it supports. However, it may want multiple instances with different
0180  * default languages based on additional information, such as the domain.
0181  *
0182  * <p>This class is not intended for public subclassing.
0183  *
0184  * @stable ICU 65
0185  */
0186 class U_COMMON_API LocaleMatcher : public UMemory {
0187 public:
0188     /**
0189      * Data for the best-matching pair of a desired and a supported locale.
0190      * Movable but not copyable.
0191      *
0192      * @stable ICU 65
0193      */
0194     class U_COMMON_API Result : public UMemory {
0195     public:
0196         /**
0197          * Move constructor; might modify the source.
0198          * This object will have the same contents that the source object had.
0199          *
0200          * @param src Result to move contents from.
0201          * @stable ICU 65
0202          */
0203         Result(Result &&src) noexcept;
0204 
0205         /**
0206          * Destructor.
0207          *
0208          * @stable ICU 65
0209          */
0210         ~Result();
0211 
0212         /**
0213          * Move assignment; might modify the source.
0214          * This object will have the same contents that the source object had.
0215          *
0216          * @param src Result to move contents from.
0217          * @stable ICU 65
0218          */
0219         Result &operator=(Result &&src) noexcept;
0220 
0221         /**
0222          * Returns the best-matching desired locale.
0223          * nullptr if the list of desired locales is empty or if none matched well enough.
0224          *
0225          * @return the best-matching desired locale, or nullptr.
0226          * @stable ICU 65
0227          */
0228         inline const Locale *getDesiredLocale() const { return desiredLocale; }
0229 
0230         /**
0231          * Returns the best-matching supported locale.
0232          * If none matched well enough, this is the default locale.
0233          * The default locale is nullptr if Builder::setNoDefaultLocale() was called,
0234          * or if the list of supported locales is empty and no explicit default locale is set.
0235          *
0236          * @return the best-matching supported locale, or nullptr.
0237          * @stable ICU 65
0238          */
0239         inline const Locale *getSupportedLocale() const { return supportedLocale; }
0240 
0241         /**
0242          * Returns the index of the best-matching desired locale in the input Iterable order.
0243          * -1 if the list of desired locales is empty or if none matched well enough.
0244          *
0245          * @return the index of the best-matching desired locale, or -1.
0246          * @stable ICU 65
0247          */
0248         inline int32_t getDesiredIndex() const { return desiredIndex; }
0249 
0250         /**
0251          * Returns the index of the best-matching supported locale in the
0252          * constructor’s or builder’s input order (“set” Collection plus “added” locales).
0253          * If the matcher was built from a locale list string, then the iteration order is that
0254          * of a LocalePriorityList built from the same string.
0255          * -1 if the list of supported locales is empty or if none matched well enough.
0256          *
0257          * @return the index of the best-matching supported locale, or -1.
0258          * @stable ICU 65
0259          */
0260         inline int32_t getSupportedIndex() const { return supportedIndex; }
0261 
0262         /**
0263          * Takes the best-matching supported locale and adds relevant fields of the
0264          * best-matching desired locale, such as the -t- and -u- extensions.
0265          * May replace some fields of the supported locale.
0266          * The result is the locale that should be used for date and number formatting, collation, etc.
0267          * Returns the root locale if getSupportedLocale() returns nullptr.
0268          *
0269          * <p>Example: desired=ar-SA-u-nu-latn, supported=ar-EG, resolved locale=ar-SA-u-nu-latn
0270          *
0271          * @return a locale combining the best-matching desired and supported locales.
0272          * @stable ICU 65
0273          */
0274         Locale makeResolvedLocale(UErrorCode &errorCode) const;
0275 
0276     private:
0277         Result(const Locale *desired, const Locale *supported,
0278                int32_t desIndex, int32_t suppIndex, UBool owned) :
0279                 desiredLocale(desired), supportedLocale(supported),
0280                 desiredIndex(desIndex), supportedIndex(suppIndex),
0281                 desiredIsOwned(owned) {}
0282 
0283         Result(const Result &other) = delete;
0284         Result &operator=(const Result &other) = delete;
0285 
0286         const Locale *desiredLocale;
0287         const Locale *supportedLocale;
0288         int32_t desiredIndex;
0289         int32_t supportedIndex;
0290         UBool desiredIsOwned;
0291 
0292         friend class LocaleMatcher;
0293     };
0294 
0295     /**
0296      * LocaleMatcher builder.
0297      * Movable but not copyable.
0298      *
0299      * @stable ICU 65
0300      */
0301     class U_COMMON_API Builder : public UMemory {
0302     public:
0303         /**
0304          * Constructs a builder used in chaining parameters for building a LocaleMatcher.
0305          *
0306          * @return a new Builder object
0307          * @stable ICU 65
0308          */
0309         Builder() {}
0310 
0311         /**
0312          * Move constructor; might modify the source.
0313          * This builder will have the same contents that the source builder had.
0314          *
0315          * @param src Builder to move contents from.
0316          * @stable ICU 65
0317          */
0318         Builder(Builder &&src) noexcept;
0319 
0320         /**
0321          * Destructor.
0322          *
0323          * @stable ICU 65
0324          */
0325         ~Builder();
0326 
0327         /**
0328          * Move assignment; might modify the source.
0329          * This builder will have the same contents that the source builder had.
0330          *
0331          * @param src Builder to move contents from.
0332          * @stable ICU 65
0333          */
0334         Builder &operator=(Builder &&src) noexcept;
0335 
0336         /**
0337          * Parses an Accept-Language string
0338          * (<a href="https://tools.ietf.org/html/rfc2616#section-14.4">RFC 2616 Section 14.4</a>),
0339          * such as "af, en, fr;q=0.9", and sets the supported locales accordingly.
0340          * Allows whitespace in more places but does not allow "*".
0341          * Clears any previously set/added supported locales first.
0342          *
0343          * @param locales the Accept-Language string of locales to set
0344          * @return this Builder object
0345          * @stable ICU 65
0346          */
0347         Builder &setSupportedLocalesFromListString(StringPiece locales);
0348 
0349         /**
0350          * Copies the supported locales, preserving iteration order.
0351          * Clears any previously set/added supported locales first.
0352          * Duplicates are allowed, and are not removed.
0353          *
0354          * @param locales the list of locale
0355          * @return this Builder object
0356          * @stable ICU 65
0357          */
0358         Builder &setSupportedLocales(Locale::Iterator &locales);
0359 
0360         /**
0361          * Copies the supported locales from the begin/end range, preserving iteration order.
0362          * Clears any previously set/added supported locales first.
0363          * Duplicates are allowed, and are not removed.
0364          *
0365          * Each of the iterator parameter values must be an
0366          * input iterator whose value is convertible to const Locale &.
0367          *
0368          * @param begin Start of range.
0369          * @param end Exclusive end of range.
0370          * @return this Builder object
0371          * @stable ICU 65
0372          */
0373         template<typename Iter>
0374         Builder &setSupportedLocales(Iter begin, Iter end) {
0375             if (U_FAILURE(errorCode_)) { return *this; }
0376             clearSupportedLocales();
0377             while (begin != end) {
0378                 addSupportedLocale(*begin++);
0379             }
0380             return *this;
0381         }
0382 
0383         /**
0384          * Copies the supported locales from the begin/end range, preserving iteration order.
0385          * Calls the converter to convert each *begin to a Locale or const Locale &.
0386          * Clears any previously set/added supported locales first.
0387          * Duplicates are allowed, and are not removed.
0388          *
0389          * Each of the iterator parameter values must be an
0390          * input iterator whose value is convertible to const Locale &.
0391          *
0392          * @param begin Start of range.
0393          * @param end Exclusive end of range.
0394          * @param converter Converter from *begin to const Locale & or compatible.
0395          * @return this Builder object
0396          * @stable ICU 65
0397          */
0398         template<typename Iter, typename Conv>
0399         Builder &setSupportedLocalesViaConverter(Iter begin, Iter end, Conv converter) {
0400             if (U_FAILURE(errorCode_)) { return *this; }
0401             clearSupportedLocales();
0402             while (begin != end) {
0403                 addSupportedLocale(converter(*begin++));
0404             }
0405             return *this;
0406         }
0407 
0408         /**
0409          * Adds another supported locale.
0410          * Duplicates are allowed, and are not removed.
0411          *
0412          * @param locale another locale
0413          * @return this Builder object
0414          * @stable ICU 65
0415          */
0416         Builder &addSupportedLocale(const Locale &locale);
0417 
0418         /**
0419          * Sets no default locale.
0420          * There will be no explicit or implicit default locale.
0421          * If there is no good match, then the matcher will return nullptr for the
0422          * best supported locale.
0423          *
0424          * @stable ICU 68
0425          */
0426         Builder &setNoDefaultLocale();
0427 
0428         /**
0429          * Sets the default locale; if nullptr, or if it is not set explicitly,
0430          * then the first supported locale is used as the default locale.
0431          * There is no default locale at all (nullptr will be returned instead)
0432          * if setNoDefaultLocale() is called.
0433          *
0434          * @param defaultLocale the default locale (will be copied)
0435          * @return this Builder object
0436          * @stable ICU 65
0437          */
0438         Builder &setDefaultLocale(const Locale *defaultLocale);
0439 
0440         /**
0441          * If ULOCMATCH_FAVOR_SCRIPT, then the language differences are smaller than script
0442          * differences.
0443          * This is used in situations (such as maps) where
0444          * it is better to fall back to the same script than a similar language.
0445          *
0446          * @param subtag the subtag to favor
0447          * @return this Builder object
0448          * @stable ICU 65
0449          */
0450         Builder &setFavorSubtag(ULocMatchFavorSubtag subtag);
0451 
0452         /**
0453          * Option for whether all desired locales are treated equally or
0454          * earlier ones are preferred (this is the default).
0455          *
0456          * @param demotion the demotion per desired locale to set.
0457          * @return this Builder object
0458          * @stable ICU 65
0459          */
0460         Builder &setDemotionPerDesiredLocale(ULocMatchDemotion demotion);
0461 
0462         /**
0463          * Option for whether to include or ignore one-way (fallback) match data.
0464          * By default, they are included.
0465          *
0466          * @param matchDirection the match direction to set.
0467          * @return this Builder object
0468          * @stable ICU 67
0469          */
0470         Builder &setDirection(ULocMatchDirection matchDirection) {
0471             if (U_SUCCESS(errorCode_)) {
0472                 direction_ = matchDirection;
0473             }
0474             return *this;
0475         }
0476 
0477         /**
0478          * Sets the maximum distance for an acceptable match.
0479          * The matcher will return a match for a pair of locales only if
0480          * they match at least as well as the pair given here.
0481          *
0482          * For example, setMaxDistance(en-US, en-GB) limits matches to ones where the
0483          * (desired, support) locales have a distance no greater than a region subtag difference.
0484          * This is much stricter than the CLDR default.
0485          *
0486          * The details of locale matching are subject to changes in
0487          * CLDR data and in the algorithm.
0488          * Specifying a maximum distance in relative terms via a sample pair of locales
0489          * insulates from changes that affect all distance metrics similarly,
0490          * but some changes will necessarily affect relative distances between
0491          * different pairs of locales.
0492          *
0493          * @param desired the desired locale for distance comparison.
0494          * @param supported the supported locale for distance comparison.
0495          * @return this Builder object
0496          * @stable ICU 68
0497          */
0498         Builder &setMaxDistance(const Locale &desired, const Locale &supported);
0499 
0500         /**
0501          * Sets the UErrorCode if an error occurred while setting parameters.
0502          * Preserves older error codes in the outErrorCode.
0503          *
0504          * @param outErrorCode Set to an error code if it does not contain one already
0505          *                  and an error occurred while setting parameters.
0506          *                  Otherwise unchanged.
0507          * @return true if U_FAILURE(outErrorCode)
0508          * @stable ICU 65
0509          */
0510         UBool copyErrorTo(UErrorCode &outErrorCode) const;
0511 
0512         /**
0513          * Builds and returns a new locale matcher.
0514          * This builder can continue to be used.
0515          *
0516          * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
0517          *                  or else the function returns immediately. Check for U_FAILURE()
0518          *                  on output or use with function chaining. (See User Guide for details.)
0519          * @return LocaleMatcher
0520          * @stable ICU 65
0521          */
0522         LocaleMatcher build(UErrorCode &errorCode) const;
0523 
0524     private:
0525         friend class LocaleMatcher;
0526 
0527         Builder(const Builder &other) = delete;
0528         Builder &operator=(const Builder &other) = delete;
0529 
0530         void clearSupportedLocales();
0531         bool ensureSupportedLocaleVector();
0532 
0533         UErrorCode errorCode_ = U_ZERO_ERROR;
0534         UVector *supportedLocales_ = nullptr;
0535         int32_t thresholdDistance_ = -1;
0536         ULocMatchDemotion demotion_ = ULOCMATCH_DEMOTION_REGION;
0537         Locale *defaultLocale_ = nullptr;
0538         bool withDefault_ = true;
0539         ULocMatchFavorSubtag favor_ = ULOCMATCH_FAVOR_LANGUAGE;
0540         ULocMatchDirection direction_ = ULOCMATCH_DIRECTION_WITH_ONE_WAY;
0541         Locale *maxDistanceDesired_ = nullptr;
0542         Locale *maxDistanceSupported_ = nullptr;
0543     };
0544 
0545     // FYI No public LocaleMatcher constructors in C++; use the Builder.
0546 
0547     /**
0548      * Move copy constructor; might modify the source.
0549      * This matcher will have the same settings that the source matcher had.
0550      * @param src source matcher
0551      * @stable ICU 65
0552      */
0553     LocaleMatcher(LocaleMatcher &&src) noexcept;
0554 
0555     /**
0556      * Destructor.
0557      * @stable ICU 65
0558      */
0559     ~LocaleMatcher();
0560 
0561     /**
0562      * Move assignment operator; might modify the source.
0563      * This matcher will have the same settings that the source matcher had.
0564      * The behavior is undefined if *this and src are the same object.
0565      * @param src source matcher
0566      * @return *this
0567      * @stable ICU 65
0568      */
0569     LocaleMatcher &operator=(LocaleMatcher &&src) noexcept;
0570 
0571     /**
0572      * Returns the supported locale which best matches the desired locale.
0573      *
0574      * @param desiredLocale Typically a user's language.
0575      * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
0576      *                  or else the function returns immediately. Check for U_FAILURE()
0577      *                  on output or use with function chaining. (See User Guide for details.)
0578      * @return the best-matching supported locale.
0579      * @stable ICU 65
0580      */
0581     const Locale *getBestMatch(const Locale &desiredLocale, UErrorCode &errorCode) const;
0582 
0583     /**
0584      * Returns the supported locale which best matches one of the desired locales.
0585      *
0586      * @param desiredLocales Typically a user's languages, in order of preference (descending).
0587      * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
0588      *                  or else the function returns immediately. Check for U_FAILURE()
0589      *                  on output or use with function chaining. (See User Guide for details.)
0590      * @return the best-matching supported locale.
0591      * @stable ICU 65
0592      */
0593     const Locale *getBestMatch(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const;
0594 
0595     /**
0596      * Parses an Accept-Language string
0597      * (<a href="https://tools.ietf.org/html/rfc2616#section-14.4">RFC 2616 Section 14.4</a>),
0598      * such as "af, en, fr;q=0.9",
0599      * and returns the supported locale which best matches one of the desired locales.
0600      * Allows whitespace in more places but does not allow "*".
0601      *
0602      * @param desiredLocaleList Typically a user's languages, as an Accept-Language string.
0603      * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
0604      *                  or else the function returns immediately. Check for U_FAILURE()
0605      *                  on output or use with function chaining. (See User Guide for details.)
0606      * @return the best-matching supported locale.
0607      * @stable ICU 65
0608      */
0609     const Locale *getBestMatchForListString(StringPiece desiredLocaleList, UErrorCode &errorCode) const;
0610 
0611     /**
0612      * Returns the best match between the desired locale and the supported locales.
0613      * If the result's desired locale is not nullptr, then it is the address of the input locale.
0614      * It has not been cloned.
0615      *
0616      * @param desiredLocale Typically a user's language.
0617      * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
0618      *                  or else the function returns immediately. Check for U_FAILURE()
0619      *                  on output or use with function chaining. (See User Guide for details.)
0620      * @return the best-matching pair of the desired and a supported locale.
0621      * @stable ICU 65
0622      */
0623     Result getBestMatchResult(const Locale &desiredLocale, UErrorCode &errorCode) const;
0624 
0625     /**
0626      * Returns the best match between the desired and supported locales.
0627      * If the result's desired locale is not nullptr, then it is a clone of
0628      * the best-matching desired locale. The Result object owns the clone.
0629      *
0630      * @param desiredLocales Typically a user's languages, in order of preference (descending).
0631      * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
0632      *                  or else the function returns immediately. Check for U_FAILURE()
0633      *                  on output or use with function chaining. (See User Guide for details.)
0634      * @return the best-matching pair of a desired and a supported locale.
0635      * @stable ICU 65
0636      */
0637     Result getBestMatchResult(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const;
0638 
0639     /**
0640      * Returns true if the pair of locales matches acceptably.
0641      * This is influenced by Builder options such as setDirection(), setFavorSubtag(),
0642      * and setMaxDistance().
0643      *
0644      * @param desired The desired locale.
0645      * @param supported The supported locale.
0646      * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
0647      *                  or else the function returns immediately. Check for U_FAILURE()
0648      *                  on output or use with function chaining. (See User Guide for details.)
0649      * @return true if the pair of locales matches acceptably.
0650      * @stable ICU 68
0651      */
0652     UBool isMatch(const Locale &desired, const Locale &supported, UErrorCode &errorCode) const;
0653 
0654 #ifndef U_HIDE_INTERNAL_API
0655     /**
0656      * Returns a fraction between 0 and 1, where 1 means that the languages are a
0657      * perfect match, and 0 means that they are completely different.
0658      *
0659      * <p>This is mostly an implementation detail, and the precise values may change over time.
0660      * The implementation may use either the maximized forms or the others ones, or both.
0661      * The implementation may or may not rely on the forms to be consistent with each other.
0662      *
0663      * <p>Callers should construct and use a matcher rather than match pairs of locales directly.
0664      *
0665      * @param desired Desired locale.
0666      * @param supported Supported locale.
0667      * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
0668      *                  or else the function returns immediately. Check for U_FAILURE()
0669      *                  on output or use with function chaining. (See User Guide for details.)
0670      * @return value between 0 and 1, inclusive.
0671      * @internal (has a known user)
0672      */
0673     double internalMatch(const Locale &desired, const Locale &supported, UErrorCode &errorCode) const;
0674 #endif  // U_HIDE_INTERNAL_API
0675 
0676 private:
0677     LocaleMatcher(const Builder &builder, UErrorCode &errorCode);
0678     LocaleMatcher(const LocaleMatcher &other) = delete;
0679     LocaleMatcher &operator=(const LocaleMatcher &other) = delete;
0680 
0681     int32_t putIfAbsent(const LSR &lsr, int32_t i, int32_t suppLength, UErrorCode &errorCode);
0682 
0683     std::optional<int32_t> getBestSuppIndex(LSR desiredLSR, LocaleLsrIterator *remainingIter, UErrorCode &errorCode) const;
0684 
0685     const LikelySubtags &likelySubtags;
0686     const LocaleDistance &localeDistance;
0687     int32_t thresholdDistance;
0688     int32_t demotionPerDesiredLocale;
0689     ULocMatchFavorSubtag favorSubtag;
0690     ULocMatchDirection direction;
0691 
0692     // These are in input order.
0693     const Locale ** supportedLocales;
0694     LSR *lsrs;
0695     int32_t supportedLocalesLength;
0696     // These are in preference order: 1. Default locale 2. paradigm locales 3. others.
0697     UHashtable *supportedLsrToIndex;  // Map<LSR, Integer>
0698     // Array versions of the supportedLsrToIndex keys and values.
0699     // The distance lookup loops over the supportedLSRs and returns the index of the best match.
0700     const LSR **supportedLSRs;
0701     int32_t *supportedIndexes;
0702     int32_t supportedLSRsLength;
0703     Locale *ownedDefaultLocale;
0704     const Locale *defaultLocale;
0705 };
0706 
0707 U_NAMESPACE_END
0708 
0709 #endif  // U_SHOW_CPLUSPLUS_API
0710 #endif  // __LOCALEMATCHER_H__