Coverage Report

Created: 2026-06-23 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/unicode/localematcher.h
Line
Count
Source
1
// © 2019 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
4
// localematcher.h
5
// created: 2019may08 Markus W. Scherer
6
7
#ifndef __LOCALEMATCHER_H__
8
#define __LOCALEMATCHER_H__
9
10
#include "unicode/utypes.h"
11
12
#if U_SHOW_CPLUSPLUS_API
13
14
#include <optional>
15
16
#include "unicode/locid.h"
17
#include "unicode/stringpiece.h"
18
#include "unicode/uobject.h"
19
20
/**
21
 * \file
22
 * \brief C++ API: Locale matcher: User's desired locales vs. application's supported locales.
23
 */
24
25
/**
26
 * Builder option for whether the language subtag or the script subtag is most important.
27
 *
28
 * @see LocaleMatcher::Builder#setFavorSubtag(ULocMatchFavorSubtag)
29
 * @stable ICU 65
30
 */
31
enum ULocMatchFavorSubtag {
32
    /**
33
     * Language differences are most important, then script differences, then region differences.
34
     * (This is the default behavior.)
35
     *
36
     * @stable ICU 65
37
     */
38
    ULOCMATCH_FAVOR_LANGUAGE,
39
    /**
40
     * Makes script differences matter relatively more than language differences.
41
     *
42
     * @stable ICU 65
43
     */
44
    ULOCMATCH_FAVOR_SCRIPT
45
};
46
#ifndef U_IN_DOXYGEN
47
typedef enum ULocMatchFavorSubtag ULocMatchFavorSubtag;
48
#endif
49
50
/**
51
 * Builder option for whether all desired locales are treated equally or
52
 * earlier ones are preferred.
53
 *
54
 * @see LocaleMatcher::Builder#setDemotionPerDesiredLocale(ULocMatchDemotion)
55
 * @stable ICU 65
56
 */
57
enum ULocMatchDemotion {
58
    /**
59
     * All desired locales are treated equally.
60
     *
61
     * @stable ICU 65
62
     */
63
    ULOCMATCH_DEMOTION_NONE,
64
    /**
65
     * Earlier desired locales are preferred.
66
     *
67
     * <p>From each desired locale to the next,
68
     * the distance to any supported locale is increased by an additional amount
69
     * which is at least as large as most region mismatches.
70
     * A later desired locale has to have a better match with some supported locale
71
     * due to more than merely having the same region subtag.
72
     *
73
     * <p>For example: <code>Supported={en, sv}  desired=[en-GB, sv]</code>
74
     * yields <code>Result(en-GB, en)</code> because
75
     * with the demotion of sv its perfect match is no better than
76
     * the region distance between the earlier desired locale en-GB and en=en-US.
77
     *
78
     * <p>Notes:
79
     * <ul>
80
     *   <li>In some cases, language and/or script differences can be as small as
81
     *       the typical region difference. (Example: sr-Latn vs. sr-Cyrl)
82
     *   <li>It is possible for certain region differences to be larger than usual,
83
     *       and larger than the demotion.
84
     *       (As of CLDR 35 there is no such case, but
85
     *        this is possible in future versions of the data.)
86
     * </ul>
87
     *
88
     * @stable ICU 65
89
     */
90
    ULOCMATCH_DEMOTION_REGION
91
};
92
#ifndef U_IN_DOXYGEN
93
typedef enum ULocMatchDemotion ULocMatchDemotion;
94
#endif
95
96
/**
97
 * Builder option for whether to include or ignore one-way (fallback) match data.
98
 * The LocaleMatcher uses CLDR languageMatch data which includes fallback (oneway=true) entries.
99
 * Sometimes it is desirable to ignore those.
100
 *
101
 * <p>For example, consider a web application with the UI in a given language,
102
 * with a link to another, related web app.
103
 * The link should include the UI language, and the target server may also use
104
 * the client’s Accept-Language header data.
105
 * The target server has its own list of supported languages.
106
 * One may want to favor UI language consistency, that is,
107
 * if there is a decent match for the original UI language, we want to use it,
108
 * but not if it is merely a fallback.
109
 *
110
 * @see LocaleMatcher::Builder#setDirection(ULocMatchDirection)
111
 * @stable ICU 67
112
 */
113
enum ULocMatchDirection {
114
    /**
115
     * Locale matching includes one-way matches such as Breton→French. (default)
116
     *
117
     * @stable ICU 67
118
     */
119
    ULOCMATCH_DIRECTION_WITH_ONE_WAY,
120
    /**
121
     * Locale matching limited to two-way matches including e.g. Danish↔Norwegian
122
     * but ignoring one-way matches.
123
     *
124
     * @stable ICU 67
125
     */
126
    ULOCMATCH_DIRECTION_ONLY_TWO_WAY
127
};
128
#ifndef U_IN_DOXYGEN
129
typedef enum ULocMatchDirection ULocMatchDirection;
130
#endif
131
132
struct UHashtable;
133
134
U_NAMESPACE_BEGIN
135
136
struct LSR;
137
138
class LikelySubtags;
139
class LocaleDistance;
140
class LocaleLsrIterator;
141
class UVector;
142
143
/**
144
 * Immutable class that picks the best match between a user's desired locales and
145
 * an application's supported locales.
146
 * Movable but not copyable.
147
 *
148
 * <p>Example:
149
 * <pre>
150
 * UErrorCode errorCode = U_ZERO_ERROR;
151
 * LocaleMatcher matcher = LocaleMatcher::Builder().setSupportedLocales("fr, en-GB, en").build(errorCode);
152
 * Locale *bestSupported = matcher.getBestLocale(Locale.US, errorCode);  // "en"
153
 * </pre>
154
 *
155
 * <p>A matcher takes into account when languages are close to one another,
156
 * such as Danish and Norwegian,
157
 * and when regional variants are close, like en-GB and en-AU as opposed to en-US.
158
 *
159
 * <p>If there are multiple supported locales with the same (language, script, region)
160
 * likely subtags, then the current implementation returns the first of those locales.
161
 * It ignores variant subtags (except for pseudolocale variants) and extensions.
162
 * This may change in future versions.
163
 *
164
 * <p>For example, the current implementation does not distinguish between
165
 * de, de-DE, de-Latn, de-1901, de-u-co-phonebk.
166
 *
167
 * <p>If you prefer one equivalent locale over another, then provide only the preferred one,
168
 * or place it earlier in the list of supported locales.
169
 *
170
 * <p>Otherwise, the order of supported locales may have no effect on the best-match results.
171
 * The current implementation compares each desired locale with supported locales
172
 * in the following order:
173
 * 1. Default locale, if supported;
174
 * 2. CLDR "paradigm locales" like en-GB and es-419;
175
 * 3. other supported locales.
176
 * This may change in future versions.
177
 *
178
 * <p>Often a product will just need one matcher instance, built with the languages
179
 * that it supports. However, it may want multiple instances with different
180
 * default languages based on additional information, such as the domain.
181
 *
182
 * <p>This class is not intended for public subclassing.
183
 *
184
 * @stable ICU 65
185
 */
186
class U_COMMON_API LocaleMatcher : public UMemory {
187
public:
188
    /**
189
     * Data for the best-matching pair of a desired and a supported locale.
190
     * Movable but not copyable.
191
     *
192
     * @stable ICU 65
193
     */
194
    class U_COMMON_API Result : public UMemory {
195
    public:
196
        /**
197
         * Move constructor; might modify the source.
198
         * This object will have the same contents that the source object had.
199
         *
200
         * @param src Result to move contents from.
201
         * @stable ICU 65
202
         */
203
        Result(Result &&src) noexcept;
204
205
        /**
206
         * Destructor.
207
         *
208
         * @stable ICU 65
209
         */
210
        ~Result();
211
212
        /**
213
         * Move assignment; might modify the source.
214
         * This object will have the same contents that the source object had.
215
         *
216
         * @param src Result to move contents from.
217
         * @stable ICU 65
218
         */
219
        Result &operator=(Result &&src) noexcept;
220
221
        /**
222
         * Returns the best-matching desired locale.
223
         * nullptr if the list of desired locales is empty or if none matched well enough.
224
         *
225
         * @return the best-matching desired locale, or nullptr.
226
         * @stable ICU 65
227
         */
228
0
        inline const Locale *getDesiredLocale() const { return desiredLocale; }
229
230
        /**
231
         * Returns the best-matching supported locale.
232
         * If none matched well enough, this is the default locale.
233
         * The default locale is nullptr if Builder::setNoDefaultLocale() was called,
234
         * or if the list of supported locales is empty and no explicit default locale is set.
235
         *
236
         * @return the best-matching supported locale, or nullptr.
237
         * @stable ICU 65
238
         */
239
0
        inline const Locale *getSupportedLocale() const { return supportedLocale; }
240
241
        /**
242
         * Returns the index of the best-matching desired locale in the input Iterable order.
243
         * -1 if the list of desired locales is empty or if none matched well enough.
244
         *
245
         * @return the index of the best-matching desired locale, or -1.
246
         * @stable ICU 65
247
         */
248
0
        inline int32_t getDesiredIndex() const { return desiredIndex; }
249
250
        /**
251
         * Returns the index of the best-matching supported locale in the
252
         * constructor’s or builder’s input order (“set” Collection plus “added” locales).
253
         * If the matcher was built from a locale list string, then the iteration order is that
254
         * of a LocalePriorityList built from the same string.
255
         * -1 if the list of supported locales is empty or if none matched well enough.
256
         *
257
         * @return the index of the best-matching supported locale, or -1.
258
         * @stable ICU 65
259
         */
260
0
        inline int32_t getSupportedIndex() const { return supportedIndex; }
261
262
        /**
263
         * Takes the best-matching supported locale and adds relevant fields of the
264
         * best-matching desired locale, such as the -t- and -u- extensions.
265
         * May replace some fields of the supported locale.
266
         * The result is the locale that should be used for date and number formatting, collation, etc.
267
         * Returns the root locale if getSupportedLocale() returns nullptr.
268
         *
269
         * <p>Example: desired=ar-SA-u-nu-latn, supported=ar-EG, resolved locale=ar-SA-u-nu-latn
270
         *
271
         * @return a locale combining the best-matching desired and supported locales.
272
         * @stable ICU 65
273
         */
274
        Locale makeResolvedLocale(UErrorCode &errorCode) const;
275
276
    private:
277
        Result(const Locale *desired, const Locale *supported,
278
               int32_t desIndex, int32_t suppIndex, UBool owned) :
279
                desiredLocale(desired), supportedLocale(supported),
280
                desiredIndex(desIndex), supportedIndex(suppIndex),
281
0
                desiredIsOwned(owned) {}
282
283
        Result(const Result &other) = delete;
284
        Result &operator=(const Result &other) = delete;
285
286
        const Locale *desiredLocale;
287
        const Locale *supportedLocale;
288
        int32_t desiredIndex;
289
        int32_t supportedIndex;
290
        UBool desiredIsOwned;
291
292
        friend class LocaleMatcher;
293
    };
294
295
    /**
296
     * LocaleMatcher builder.
297
     * Movable but not copyable.
298
     *
299
     * @stable ICU 65
300
     */
301
    class U_COMMON_API Builder : public UMemory {
302
    public:
303
        /**
304
         * Constructs a builder used in chaining parameters for building a LocaleMatcher.
305
         *
306
         * @stable ICU 65
307
         */
308
0
        Builder() {}
309
310
        /**
311
         * Move constructor; might modify the source.
312
         * This builder will have the same contents that the source builder had.
313
         *
314
         * @param src Builder to move contents from.
315
         * @stable ICU 65
316
         */
317
        Builder(Builder &&src) noexcept;
318
319
        /**
320
         * Destructor.
321
         *
322
         * @stable ICU 65
323
         */
324
        ~Builder();
325
326
        /**
327
         * Move assignment; might modify the source.
328
         * This builder will have the same contents that the source builder had.
329
         *
330
         * @param src Builder to move contents from.
331
         * @stable ICU 65
332
         */
333
        Builder &operator=(Builder &&src) noexcept;
334
335
        /**
336
         * Parses an Accept-Language string
337
         * (<a href="https://tools.ietf.org/html/rfc2616#section-14.4">RFC 2616 Section 14.4</a>),
338
         * such as "af, en, fr;q=0.9", and sets the supported locales accordingly.
339
         * Allows whitespace in more places but does not allow "*".
340
         * Clears any previously set/added supported locales first.
341
         *
342
         * @param locales the Accept-Language string of locales to set
343
         * @return this Builder object
344
         * @stable ICU 65
345
         */
346
        Builder &setSupportedLocalesFromListString(StringPiece locales);
347
348
        /**
349
         * Copies the supported locales, preserving iteration order.
350
         * Clears any previously set/added supported locales first.
351
         * Duplicates are allowed, and are not removed.
352
         *
353
         * @param locales the list of locale
354
         * @return this Builder object
355
         * @stable ICU 65
356
         */
357
        Builder &setSupportedLocales(Locale::Iterator &locales);
358
359
        /**
360
         * Copies the supported locales from the begin/end range, preserving iteration order.
361
         * Clears any previously set/added supported locales first.
362
         * Duplicates are allowed, and are not removed.
363
         *
364
         * Each of the iterator parameter values must be an
365
         * input iterator whose value is convertible to const Locale &.
366
         *
367
         * @param begin Start of range.
368
         * @param end Exclusive end of range.
369
         * @return this Builder object
370
         * @stable ICU 65
371
         */
372
        template<typename Iter>
373
        Builder &setSupportedLocales(Iter begin, Iter end) {
374
            if (U_FAILURE(errorCode_)) { return *this; }
375
            clearSupportedLocales();
376
            while (begin != end) {
377
                addSupportedLocale(*begin++);
378
            }
379
            return *this;
380
        }
381
382
        /**
383
         * Copies the supported locales from the begin/end range, preserving iteration order.
384
         * Calls the converter to convert each *begin to a Locale or const Locale &.
385
         * Clears any previously set/added supported locales first.
386
         * Duplicates are allowed, and are not removed.
387
         *
388
         * Each of the iterator parameter values must be an
389
         * input iterator whose value is convertible to const Locale &.
390
         *
391
         * @param begin Start of range.
392
         * @param end Exclusive end of range.
393
         * @param converter Converter from *begin to const Locale & or compatible.
394
         * @return this Builder object
395
         * @stable ICU 65
396
         */
397
        template<typename Iter, typename Conv>
398
        Builder &setSupportedLocalesViaConverter(Iter begin, Iter end, Conv converter) {
399
            if (U_FAILURE(errorCode_)) { return *this; }
400
            clearSupportedLocales();
401
            while (begin != end) {
402
                addSupportedLocale(converter(*begin++));
403
            }
404
            return *this;
405
        }
406
407
        /**
408
         * Adds another supported locale.
409
         * Duplicates are allowed, and are not removed.
410
         *
411
         * @param locale another locale
412
         * @return this Builder object
413
         * @stable ICU 65
414
         */
415
        Builder &addSupportedLocale(const Locale &locale);
416
417
        /**
418
         * Sets no default locale.
419
         * There will be no explicit or implicit default locale.
420
         * If there is no good match, then the matcher will return nullptr for the
421
         * best supported locale.
422
         *
423
         * @stable ICU 68
424
         */
425
        Builder &setNoDefaultLocale();
426
427
        /**
428
         * Sets the default locale; if nullptr, or if it is not set explicitly,
429
         * then the first supported locale is used as the default locale.
430
         * There is no default locale at all (nullptr will be returned instead)
431
         * if setNoDefaultLocale() is called.
432
         *
433
         * @param defaultLocale the default locale (will be copied)
434
         * @return this Builder object
435
         * @stable ICU 65
436
         */
437
        Builder &setDefaultLocale(const Locale *defaultLocale);
438
439
        /**
440
         * If ULOCMATCH_FAVOR_SCRIPT, then the language differences are smaller than script
441
         * differences.
442
         * This is used in situations (such as maps) where
443
         * it is better to fall back to the same script than a similar language.
444
         *
445
         * @param subtag the subtag to favor
446
         * @return this Builder object
447
         * @stable ICU 65
448
         */
449
        Builder &setFavorSubtag(ULocMatchFavorSubtag subtag);
450
451
        /**
452
         * Option for whether all desired locales are treated equally or
453
         * earlier ones are preferred (this is the default).
454
         *
455
         * @param demotion the demotion per desired locale to set.
456
         * @return this Builder object
457
         * @stable ICU 65
458
         */
459
        Builder &setDemotionPerDesiredLocale(ULocMatchDemotion demotion);
460
461
        /**
462
         * Option for whether to include or ignore one-way (fallback) match data.
463
         * By default, they are included.
464
         *
465
         * @param matchDirection the match direction to set.
466
         * @return this Builder object
467
         * @stable ICU 67
468
         */
469
0
        Builder &setDirection(ULocMatchDirection matchDirection) {
470
0
            if (U_SUCCESS(errorCode_)) {
471
0
                direction_ = matchDirection;
472
0
            }
473
0
            return *this;
474
0
        }
475
476
        /**
477
         * Sets the maximum distance for an acceptable match.
478
         * The matcher will return a match for a pair of locales only if
479
         * they match at least as well as the pair given here.
480
         *
481
         * For example, setMaxDistance(en-US, en-GB) limits matches to ones where the
482
         * (desired, support) locales have a distance no greater than a region subtag difference.
483
         * This is much stricter than the CLDR default.
484
         *
485
         * The details of locale matching are subject to changes in
486
         * CLDR data and in the algorithm.
487
         * Specifying a maximum distance in relative terms via a sample pair of locales
488
         * insulates from changes that affect all distance metrics similarly,
489
         * but some changes will necessarily affect relative distances between
490
         * different pairs of locales.
491
         *
492
         * @param desired the desired locale for distance comparison.
493
         * @param supported the supported locale for distance comparison.
494
         * @return this Builder object
495
         * @stable ICU 68
496
         */
497
        Builder &setMaxDistance(const Locale &desired, const Locale &supported);
498
499
        /**
500
         * Sets the UErrorCode if an error occurred while setting parameters.
501
         * Preserves older error codes in the outErrorCode.
502
         *
503
         * @param outErrorCode Set to an error code if it does not contain one already
504
         *                  and an error occurred while setting parameters.
505
         *                  Otherwise unchanged.
506
         * @return true if U_FAILURE(outErrorCode)
507
         * @stable ICU 65
508
         */
509
        UBool copyErrorTo(UErrorCode &outErrorCode) const;
510
511
        /**
512
         * Builds and returns a new locale matcher.
513
         * This builder can continue to be used.
514
         *
515
         * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
516
         *                  or else the function returns immediately. Check for U_FAILURE()
517
         *                  on output or use with function chaining. (See User Guide for details.)
518
         * @return LocaleMatcher
519
         * @stable ICU 65
520
         */
521
        LocaleMatcher build(UErrorCode &errorCode) const;
522
523
    private:
524
        friend class LocaleMatcher;
525
526
        Builder(const Builder &other) = delete;
527
        Builder &operator=(const Builder &other) = delete;
528
529
        void clearSupportedLocales();
530
        bool ensureSupportedLocaleVector();
531
532
        UErrorCode errorCode_ = U_ZERO_ERROR;
533
        UVector *supportedLocales_ = nullptr;
534
        int32_t thresholdDistance_ = -1;
535
        ULocMatchDemotion demotion_ = ULOCMATCH_DEMOTION_REGION;
536
        Locale *defaultLocale_ = nullptr;
537
        bool withDefault_ = true;
538
        ULocMatchFavorSubtag favor_ = ULOCMATCH_FAVOR_LANGUAGE;
539
        ULocMatchDirection direction_ = ULOCMATCH_DIRECTION_WITH_ONE_WAY;
540
        Locale *maxDistanceDesired_ = nullptr;
541
        Locale *maxDistanceSupported_ = nullptr;
542
    };
543
544
    // FYI No public LocaleMatcher constructors in C++; use the Builder.
545
546
    /**
547
     * Move copy constructor; might modify the source.
548
     * This matcher will have the same settings that the source matcher had.
549
     * @param src source matcher
550
     * @stable ICU 65
551
     */
552
    LocaleMatcher(LocaleMatcher &&src) noexcept;
553
554
    /**
555
     * Destructor.
556
     * @stable ICU 65
557
     */
558
    ~LocaleMatcher();
559
560
    /**
561
     * Move assignment operator; might modify the source.
562
     * This matcher will have the same settings that the source matcher had.
563
     * The behavior is undefined if *this and src are the same object.
564
     * @param src source matcher
565
     * @return *this
566
     * @stable ICU 65
567
     */
568
    LocaleMatcher &operator=(LocaleMatcher &&src) noexcept;
569
570
    /**
571
     * Returns the supported locale which best matches the desired locale.
572
     *
573
     * @param desiredLocale Typically a user's language.
574
     * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
575
     *                  or else the function returns immediately. Check for U_FAILURE()
576
     *                  on output or use with function chaining. (See User Guide for details.)
577
     * @return the best-matching supported locale.
578
     * @stable ICU 65
579
     */
580
    const Locale *getBestMatch(const Locale &desiredLocale, UErrorCode &errorCode) const;
581
582
    /**
583
     * Returns the supported locale which best matches one of the desired locales.
584
     *
585
     * @param desiredLocales Typically a user's languages, in order of preference (descending).
586
     * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
587
     *                  or else the function returns immediately. Check for U_FAILURE()
588
     *                  on output or use with function chaining. (See User Guide for details.)
589
     * @return the best-matching supported locale.
590
     * @stable ICU 65
591
     */
592
    const Locale *getBestMatch(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const;
593
594
    /**
595
     * Parses an Accept-Language string
596
     * (<a href="https://tools.ietf.org/html/rfc2616#section-14.4">RFC 2616 Section 14.4</a>),
597
     * such as "af, en, fr;q=0.9",
598
     * and returns the supported locale which best matches one of the desired locales.
599
     * Allows whitespace in more places but does not allow "*".
600
     *
601
     * @param desiredLocaleList Typically a user's languages, as an Accept-Language string.
602
     * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
603
     *                  or else the function returns immediately. Check for U_FAILURE()
604
     *                  on output or use with function chaining. (See User Guide for details.)
605
     * @return the best-matching supported locale.
606
     * @stable ICU 65
607
     */
608
    const Locale *getBestMatchForListString(StringPiece desiredLocaleList, UErrorCode &errorCode) const;
609
610
    /**
611
     * Returns the best match between the desired locale and the supported locales.
612
     * If the result's desired locale is not nullptr, then it is the address of the input locale.
613
     * It has not been cloned.
614
     *
615
     * @param desiredLocale Typically a user's language.
616
     * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
617
     *                  or else the function returns immediately. Check for U_FAILURE()
618
     *                  on output or use with function chaining. (See User Guide for details.)
619
     * @return the best-matching pair of the desired and a supported locale.
620
     * @stable ICU 65
621
     */
622
    Result getBestMatchResult(const Locale &desiredLocale, UErrorCode &errorCode) const;
623
624
    /**
625
     * Returns the best match between the desired and supported locales.
626
     * If the result's desired locale is not nullptr, then it is a clone of
627
     * the best-matching desired locale. The Result object owns the clone.
628
     *
629
     * @param desiredLocales Typically a user's languages, in order of preference (descending).
630
     * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
631
     *                  or else the function returns immediately. Check for U_FAILURE()
632
     *                  on output or use with function chaining. (See User Guide for details.)
633
     * @return the best-matching pair of a desired and a supported locale.
634
     * @stable ICU 65
635
     */
636
    Result getBestMatchResult(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const;
637
638
    /**
639
     * Returns true if the pair of locales matches acceptably.
640
     * This is influenced by Builder options such as setDirection(), setFavorSubtag(),
641
     * and setMaxDistance().
642
     *
643
     * @param desired The desired locale.
644
     * @param supported The supported locale.
645
     * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
646
     *                  or else the function returns immediately. Check for U_FAILURE()
647
     *                  on output or use with function chaining. (See User Guide for details.)
648
     * @return true if the pair of locales matches acceptably.
649
     * @stable ICU 68
650
     */
651
    UBool isMatch(const Locale &desired, const Locale &supported, UErrorCode &errorCode) const;
652
653
#ifndef U_HIDE_INTERNAL_API
654
    /**
655
     * Returns a fraction between 0 and 1, where 1 means that the languages are a
656
     * perfect match, and 0 means that they are completely different.
657
     *
658
     * <p>This is mostly an implementation detail, and the precise values may change over time.
659
     * The implementation may use either the maximized forms or the others ones, or both.
660
     * The implementation may or may not rely on the forms to be consistent with each other.
661
     *
662
     * <p>Callers should construct and use a matcher rather than match pairs of locales directly.
663
     *
664
     * @param desired Desired locale.
665
     * @param supported Supported locale.
666
     * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
667
     *                  or else the function returns immediately. Check for U_FAILURE()
668
     *                  on output or use with function chaining. (See User Guide for details.)
669
     * @return value between 0 and 1, inclusive.
670
     * @internal (has a known user)
671
     */
672
    double internalMatch(const Locale &desired, const Locale &supported, UErrorCode &errorCode) const;
673
#endif  // U_HIDE_INTERNAL_API
674
675
private:
676
    LocaleMatcher(const Builder &builder, UErrorCode &errorCode);
677
    LocaleMatcher(const LocaleMatcher &other) = delete;
678
    LocaleMatcher &operator=(const LocaleMatcher &other) = delete;
679
680
    int32_t putIfAbsent(const LSR &lsr, int32_t i, int32_t suppLength, UErrorCode &errorCode);
681
682
    std::optional<int32_t> getBestSuppIndex(LSR desiredLSR, LocaleLsrIterator *remainingIter, UErrorCode &errorCode) const;
683
684
    const LikelySubtags &likelySubtags;
685
    const LocaleDistance &localeDistance;
686
    int32_t thresholdDistance;
687
    int32_t demotionPerDesiredLocale;
688
    ULocMatchFavorSubtag favorSubtag;
689
    ULocMatchDirection direction;
690
691
    // These are in input order.
692
    const Locale ** supportedLocales;
693
    LSR *lsrs;
694
    int32_t supportedLocalesLength;
695
    // These are in preference order: 1. Default locale 2. paradigm locales 3. others.
696
    UHashtable *supportedLsrToIndex;  // Map<LSR, Integer>
697
    // Array versions of the supportedLsrToIndex keys and values.
698
    // The distance lookup loops over the supportedLSRs and returns the index of the best match.
699
    const LSR **supportedLSRs;
700
    int32_t *supportedIndexes;
701
    int32_t supportedLSRsLength;
702
    Locale *ownedDefaultLocale;
703
    const Locale *defaultLocale;
704
};
705
706
U_NAMESPACE_END
707
708
#endif  // U_SHOW_CPLUSPLUS_API
709
#endif  // __LOCALEMATCHER_H__