00001
00002
00003
00004
00005
00006
00007 #ifndef __LOCALEMATCHER_H__
00008 #define __LOCALEMATCHER_H__
00009
00010 #include "unicode/utypes.h"
00011
00012 #if U_SHOW_CPLUSPLUS_API
00013
00014 #include "unicode/locid.h"
00015 #include "unicode/stringpiece.h"
00016 #include "unicode/uobject.h"
00017
00023 #ifndef U_FORCE_HIDE_DRAFT_API
00024
00031 enum ULocMatchFavorSubtag {
00038 ULOCMATCH_FAVOR_LANGUAGE,
00044 ULOCMATCH_FAVOR_SCRIPT
00045 };
00046 #ifndef U_IN_DOXYGEN
00047 typedef enum ULocMatchFavorSubtag ULocMatchFavorSubtag;
00048 #endif
00049
00057 enum ULocMatchDemotion {
00063 ULOCMATCH_DEMOTION_NONE,
00090 ULOCMATCH_DEMOTION_REGION
00091 };
00092 #ifndef U_IN_DOXYGEN
00093 typedef enum ULocMatchDemotion ULocMatchDemotion;
00094 #endif
00095
00113 enum ULocMatchDirection {
00119 ULOCMATCH_DIRECTION_WITH_ONE_WAY,
00126 ULOCMATCH_DIRECTION_ONLY_TWO_WAY
00127 };
00128 #ifndef U_IN_DOXYGEN
00129 typedef enum ULocMatchDirection ULocMatchDirection;
00130 #endif
00131
00132 struct UHashtable;
00133
00134 U_NAMESPACE_BEGIN
00135
00136 struct LSR;
00137
00138 class LocaleDistance;
00139 class LocaleLsrIterator;
00140 class UVector;
00141 class XLikelySubtags;
00142
00186 class U_COMMON_API LocaleMatcher : public UMemory {
00187 public:
00194 class U_COMMON_API Result : public UMemory {
00195 public:
00203 Result(Result &&src) U_NOEXCEPT;
00204
00210 ~Result();
00211
00219 Result &operator=(Result &&src) U_NOEXCEPT;
00220
00221 #ifndef U_HIDE_DRAFT_API
00222
00229 inline const Locale *getDesiredLocale() const { return desiredLocale; }
00230
00240 inline const Locale *getSupportedLocale() const { return supportedLocale; }
00241
00249 inline int32_t getDesiredIndex() const { return desiredIndex; }
00250
00261 inline int32_t getSupportedIndex() const { return supportedIndex; }
00262
00275 Locale makeResolvedLocale(UErrorCode &errorCode) const;
00276 #endif // U_HIDE_DRAFT_API
00277
00278 private:
00279 Result(const Locale *desired, const Locale *supported,
00280 int32_t desIndex, int32_t suppIndex, UBool owned) :
00281 desiredLocale(desired), supportedLocale(supported),
00282 desiredIndex(desIndex), supportedIndex(suppIndex),
00283 desiredIsOwned(owned) {}
00284
00285 Result(const Result &other) = delete;
00286 Result &operator=(const Result &other) = delete;
00287
00288 const Locale *desiredLocale;
00289 const Locale *supportedLocale;
00290 int32_t desiredIndex;
00291 int32_t supportedIndex;
00292 UBool desiredIsOwned;
00293
00294 friend class LocaleMatcher;
00295 };
00296
00304 class U_COMMON_API Builder : public UMemory {
00305 public:
00312 Builder() {}
00313
00321 Builder(Builder &&src) U_NOEXCEPT;
00322
00328 ~Builder();
00329
00337 Builder &operator=(Builder &&src) U_NOEXCEPT;
00338
00339 #ifndef U_HIDE_DRAFT_API
00340
00351 Builder &setSupportedLocalesFromListString(StringPiece locales);
00352
00362 Builder &setSupportedLocales(Locale::Iterator &locales);
00363
00377 template<typename Iter>
00378 Builder &setSupportedLocales(Iter begin, Iter end) {
00379 if (U_FAILURE(errorCode_)) { return *this; }
00380 clearSupportedLocales();
00381 while (begin != end) {
00382 addSupportedLocale(*begin++);
00383 }
00384 return *this;
00385 }
00386
00402 template<typename Iter, typename Conv>
00403 Builder &setSupportedLocalesViaConverter(Iter begin, Iter end, Conv converter) {
00404 if (U_FAILURE(errorCode_)) { return *this; }
00405 clearSupportedLocales();
00406 while (begin != end) {
00407 addSupportedLocale(converter(*begin++));
00408 }
00409 return *this;
00410 }
00411
00420 Builder &addSupportedLocale(const Locale &locale);
00421
00430 Builder &setDefaultLocale(const Locale *defaultLocale);
00431
00442 Builder &setFavorSubtag(ULocMatchFavorSubtag subtag);
00443
00452 Builder &setDemotionPerDesiredLocale(ULocMatchDemotion demotion);
00453
00462 Builder &setDirection(ULocMatchDirection direction) {
00463 if (U_SUCCESS(errorCode_)) {
00464 direction_ = direction;
00465 }
00466 return *this;
00467 }
00468
00479 UBool copyErrorTo(UErrorCode &outErrorCode) const;
00480
00491 LocaleMatcher build(UErrorCode &errorCode) const;
00492 #endif // U_HIDE_DRAFT_API
00493
00494 private:
00495 friend class LocaleMatcher;
00496
00497 Builder(const Builder &other) = delete;
00498 Builder &operator=(const Builder &other) = delete;
00499
00500 void clearSupportedLocales();
00501 bool ensureSupportedLocaleVector();
00502
00503 UErrorCode errorCode_ = U_ZERO_ERROR;
00504 UVector *supportedLocales_ = nullptr;
00505 int32_t thresholdDistance_ = -1;
00506 ULocMatchDemotion demotion_ = ULOCMATCH_DEMOTION_REGION;
00507 Locale *defaultLocale_ = nullptr;
00508 ULocMatchFavorSubtag favor_ = ULOCMATCH_FAVOR_LANGUAGE;
00509 ULocMatchDirection direction_ = ULOCMATCH_DIRECTION_WITH_ONE_WAY;
00510 };
00511
00512
00513
00520 LocaleMatcher(LocaleMatcher &&src) U_NOEXCEPT;
00521
00526 ~LocaleMatcher();
00527
00536 LocaleMatcher &operator=(LocaleMatcher &&src) U_NOEXCEPT;
00537
00538 #ifndef U_HIDE_DRAFT_API
00539
00549 const Locale *getBestMatch(const Locale &desiredLocale, UErrorCode &errorCode) const;
00550
00561 const Locale *getBestMatch(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const;
00562
00577 const Locale *getBestMatchForListString(StringPiece desiredLocaleList, UErrorCode &errorCode) const;
00578
00591 Result getBestMatchResult(const Locale &desiredLocale, UErrorCode &errorCode) const;
00592
00605 Result getBestMatchResult(Locale::Iterator &desiredLocales, UErrorCode &errorCode) const;
00606 #endif // U_HIDE_DRAFT_API
00607
00608 #ifndef U_HIDE_INTERNAL_API
00609
00627 double internalMatch(const Locale &desired, const Locale &supported, UErrorCode &errorCode) const;
00628 #endif // U_HIDE_INTERNAL_API
00629
00630 private:
00631 LocaleMatcher(const Builder &builder, UErrorCode &errorCode);
00632 LocaleMatcher(const LocaleMatcher &other) = delete;
00633 LocaleMatcher &operator=(const LocaleMatcher &other) = delete;
00634
00635 int32_t putIfAbsent(const LSR &lsr, int32_t i, int32_t suppLength, UErrorCode &errorCode);
00636
00637 int32_t getBestSuppIndex(LSR desiredLSR, LocaleLsrIterator *remainingIter, UErrorCode &errorCode) const;
00638
00639 const XLikelySubtags &likelySubtags;
00640 const LocaleDistance &localeDistance;
00641 int32_t thresholdDistance;
00642 int32_t demotionPerDesiredLocale;
00643 ULocMatchFavorSubtag favorSubtag;
00644 ULocMatchDirection direction;
00645
00646
00647 const Locale ** supportedLocales;
00648 LSR *lsrs;
00649 int32_t supportedLocalesLength;
00650
00651 UHashtable *supportedLsrToIndex;
00652
00653
00654 const LSR **supportedLSRs;
00655 int32_t *supportedIndexes;
00656 int32_t supportedLSRsLength;
00657 Locale *ownedDefaultLocale;
00658 const Locale *defaultLocale;
00659 };
00660
00661 U_NAMESPACE_END
00662
00663 #endif // U_FORCE_HIDE_DRAFT_API
00664 #endif // U_SHOW_CPLUSPLUS_API
00665 #endif // __LOCALEMATCHER_H__