localematcher.h 25 KB

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