localeprioritylist.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // © 2019 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // localeprioritylist.cpp
  4. // created: 2019jul11 Markus W. Scherer
  5. #include "unicode/utypes.h"
  6. #include "unicode/localpointer.h"
  7. #include "unicode/locid.h"
  8. #include "unicode/stringpiece.h"
  9. #include "unicode/uobject.h"
  10. #include "charstr.h"
  11. #include "cmemory.h"
  12. #include "localeprioritylist.h"
  13. #include "uarrsort.h"
  14. #include "uassert.h"
  15. #include "uhash.h"
  16. U_NAMESPACE_BEGIN
  17. namespace {
  18. int32_t hashLocale(const UHashTok token) {
  19. auto *locale = static_cast<const Locale *>(token.pointer);
  20. return locale->hashCode();
  21. }
  22. UBool compareLocales(const UHashTok t1, const UHashTok t2) {
  23. auto *l1 = static_cast<const Locale *>(t1.pointer);
  24. auto *l2 = static_cast<const Locale *>(t2.pointer);
  25. return *l1 == *l2;
  26. }
  27. constexpr int32_t WEIGHT_ONE = 1000;
  28. struct LocaleAndWeight {
  29. Locale *locale;
  30. int32_t weight; // 0..1000 = 0.0..1.0
  31. int32_t index; // force stable sort
  32. int32_t compare(const LocaleAndWeight &other) const {
  33. int32_t diff = other.weight - weight; // descending: other-this
  34. if (diff != 0) { return diff; }
  35. return index - other.index;
  36. }
  37. };
  38. int32_t U_CALLCONV
  39. compareLocaleAndWeight(const void * /*context*/, const void *left, const void *right) {
  40. return static_cast<const LocaleAndWeight *>(left)->
  41. compare(*static_cast<const LocaleAndWeight *>(right));
  42. }
  43. const char *skipSpaces(const char *p, const char *limit) {
  44. while (p < limit && *p == ' ') { ++p; }
  45. return p;
  46. }
  47. int32_t findTagLength(const char *p, const char *limit) {
  48. // Look for accept-language delimiters.
  49. // Leave other validation up to the Locale constructor.
  50. const char *q;
  51. for (q = p; q < limit; ++q) {
  52. char c = *q;
  53. if (c == ' ' || c == ',' || c == ';') { break; }
  54. }
  55. return static_cast<int32_t>(q - p);
  56. }
  57. /**
  58. * Parses and returns a qvalue weight in millis.
  59. * Advances p to after the parsed substring.
  60. * Returns a negative value if parsing fails.
  61. */
  62. int32_t parseWeight(const char *&p, const char *limit) {
  63. p = skipSpaces(p, limit);
  64. char c;
  65. if (p == limit || ((c = *p) != '0' && c != '1')) { return -1; }
  66. int32_t weight = (c - '0') * 1000;
  67. if (++p == limit || *p != '.') { return weight; }
  68. int32_t multiplier = 100;
  69. while (++p != limit && '0' <= (c = *p) && c <= '9') {
  70. c -= '0';
  71. if (multiplier > 0) {
  72. weight += c * multiplier;
  73. multiplier /= 10;
  74. } else if (multiplier == 0) {
  75. // round up
  76. if (c >= 5) { ++weight; }
  77. multiplier = -1;
  78. } // else ignore further fraction digits
  79. }
  80. return weight <= WEIGHT_ONE ? weight : -1; // bad if > 1.0
  81. }
  82. } // namespace
  83. /**
  84. * Nothing but a wrapper over a MaybeStackArray of LocaleAndWeight.
  85. *
  86. * This wrapper exists (and is not in an anonymous namespace)
  87. * so that we can forward-declare it in the header file and
  88. * don't have to expose the MaybeStackArray specialization and
  89. * the LocaleAndWeight to code (like the test) that #includes localeprioritylist.h.
  90. * Also, otherwise we would have to do a platform-specific
  91. * template export declaration of some kind for the MaybeStackArray specialization
  92. * to be properly exported from the common DLL.
  93. */
  94. struct LocaleAndWeightArray : public UMemory {
  95. MaybeStackArray<LocaleAndWeight, 20> array;
  96. };
  97. LocalePriorityList::LocalePriorityList(StringPiece s, UErrorCode &errorCode) {
  98. if (U_FAILURE(errorCode)) { return; }
  99. list = new LocaleAndWeightArray();
  100. if (list == nullptr) {
  101. errorCode = U_MEMORY_ALLOCATION_ERROR;
  102. return;
  103. }
  104. const char *p = s.data();
  105. const char *limit = p + s.length();
  106. while ((p = skipSpaces(p, limit)) != limit) {
  107. if (*p == ',') { // empty range field
  108. ++p;
  109. continue;
  110. }
  111. int32_t tagLength = findTagLength(p, limit);
  112. if (tagLength == 0) {
  113. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  114. return;
  115. }
  116. CharString tag(p, tagLength, errorCode);
  117. if (U_FAILURE(errorCode)) { return; }
  118. Locale locale = Locale(tag.data());
  119. if (locale.isBogus()) {
  120. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  121. return;
  122. }
  123. int32_t weight = WEIGHT_ONE;
  124. if ((p = skipSpaces(p + tagLength, limit)) != limit && *p == ';') {
  125. if ((p = skipSpaces(p + 1, limit)) == limit || *p != 'q' ||
  126. (p = skipSpaces(p + 1, limit)) == limit || *p != '=' ||
  127. (++p, (weight = parseWeight(p, limit)) < 0)) {
  128. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  129. return;
  130. }
  131. p = skipSpaces(p, limit);
  132. }
  133. if (p != limit && *p != ',') { // trailing junk
  134. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  135. return;
  136. }
  137. add(locale, weight, errorCode);
  138. if (p == limit) { break; }
  139. ++p;
  140. }
  141. sort(errorCode);
  142. }
  143. LocalePriorityList::~LocalePriorityList() {
  144. if (list != nullptr) {
  145. for (int32_t i = 0; i < listLength; ++i) {
  146. delete list->array[i].locale;
  147. }
  148. delete list;
  149. }
  150. uhash_close(map);
  151. }
  152. const Locale *LocalePriorityList::localeAt(int32_t i) const {
  153. return list->array[i].locale;
  154. }
  155. Locale *LocalePriorityList::orphanLocaleAt(int32_t i) {
  156. if (list == nullptr) { return nullptr; }
  157. LocaleAndWeight &lw = list->array[i];
  158. Locale *l = lw.locale;
  159. lw.locale = nullptr;
  160. return l;
  161. }
  162. bool LocalePriorityList::add(const Locale &locale, int32_t weight, UErrorCode &errorCode) {
  163. if (U_FAILURE(errorCode)) { return false; }
  164. if (map == nullptr) {
  165. if (weight <= 0) { return true; } // do not add q=0
  166. map = uhash_open(hashLocale, compareLocales, uhash_compareLong, &errorCode);
  167. if (U_FAILURE(errorCode)) { return false; }
  168. }
  169. LocalPointer<Locale> clone;
  170. UBool found = false;
  171. int32_t index = uhash_getiAndFound(map, &locale, &found);
  172. if (found) {
  173. // Duplicate: Remove the old item and append it anew.
  174. LocaleAndWeight &lw = list->array[index];
  175. clone.adoptInstead(lw.locale);
  176. lw.locale = nullptr;
  177. lw.weight = 0;
  178. ++numRemoved;
  179. }
  180. if (weight <= 0) { // do not add q=0
  181. if (found) {
  182. // Not strictly necessary but cleaner.
  183. uhash_removei(map, &locale);
  184. }
  185. return true;
  186. }
  187. if (clone.isNull()) {
  188. clone.adoptInstead(locale.clone());
  189. if (clone.isNull() || (clone->isBogus() && !locale.isBogus())) {
  190. errorCode = U_MEMORY_ALLOCATION_ERROR;
  191. return false;
  192. }
  193. }
  194. if (listLength == list->array.getCapacity()) {
  195. int32_t newCapacity = listLength < 50 ? 100 : 4 * listLength;
  196. if (list->array.resize(newCapacity, listLength) == nullptr) {
  197. errorCode = U_MEMORY_ALLOCATION_ERROR;
  198. return false;
  199. }
  200. }
  201. uhash_putiAllowZero(map, clone.getAlias(), listLength, &errorCode);
  202. if (U_FAILURE(errorCode)) { return false; }
  203. LocaleAndWeight &lw = list->array[listLength];
  204. lw.locale = clone.orphan();
  205. lw.weight = weight;
  206. lw.index = listLength++;
  207. if (weight < WEIGHT_ONE) { hasWeights = true; }
  208. U_ASSERT(uhash_count(map) == getLength());
  209. return true;
  210. }
  211. void LocalePriorityList::sort(UErrorCode &errorCode) {
  212. // Sort by descending weights if there is a mix of weights.
  213. // The comparator forces a stable sort via the item index.
  214. if (U_FAILURE(errorCode) || getLength() <= 1 || !hasWeights) { return; }
  215. uprv_sortArray(list->array.getAlias(), listLength, sizeof(LocaleAndWeight),
  216. compareLocaleAndWeight, nullptr, false, &errorCode);
  217. }
  218. U_NAMESPACE_END