number_grouping.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // © 2017 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #include "unicode/utypes.h"
  4. #if !UCONFIG_NO_FORMATTING
  5. #include "unicode/numberformatter.h"
  6. #include "number_patternstring.h"
  7. #include "uresimp.h"
  8. using namespace icu;
  9. using namespace icu::number;
  10. using namespace icu::number::impl;
  11. namespace {
  12. int16_t getMinGroupingForLocale(const Locale& locale) {
  13. // TODO: Cache this?
  14. UErrorCode localStatus = U_ZERO_ERROR;
  15. LocalUResourceBundlePointer bundle(ures_open(nullptr, locale.getName(), &localStatus));
  16. int32_t resultLen = 0;
  17. const char16_t* result = ures_getStringByKeyWithFallback(
  18. bundle.getAlias(),
  19. "NumberElements/minimumGroupingDigits",
  20. &resultLen,
  21. &localStatus);
  22. // TODO: Is it safe to assume resultLen == 1? Would locales set minGrouping >= 10?
  23. if (U_FAILURE(localStatus) || resultLen != 1) {
  24. return 1;
  25. }
  26. return result[0] - u'0';
  27. }
  28. }
  29. Grouper Grouper::forStrategy(UNumberGroupingStrategy grouping) {
  30. switch (grouping) {
  31. case UNUM_GROUPING_OFF:
  32. return {-1, -1, -2, grouping};
  33. case UNUM_GROUPING_AUTO:
  34. return {-2, -2, -2, grouping};
  35. case UNUM_GROUPING_MIN2:
  36. return {-2, -2, -3, grouping};
  37. case UNUM_GROUPING_ON_ALIGNED:
  38. return {-4, -4, 1, grouping};
  39. case UNUM_GROUPING_THOUSANDS:
  40. return {3, 3, 1, grouping};
  41. default:
  42. UPRV_UNREACHABLE_EXIT;
  43. }
  44. }
  45. Grouper Grouper::forProperties(const DecimalFormatProperties& properties) {
  46. if (!properties.groupingUsed) {
  47. return forStrategy(UNUM_GROUPING_OFF);
  48. }
  49. auto grouping1 = static_cast<int16_t>(properties.groupingSize);
  50. auto grouping2 = static_cast<int16_t>(properties.secondaryGroupingSize);
  51. auto minGrouping = static_cast<int16_t>(properties.minimumGroupingDigits);
  52. grouping1 = grouping1 > 0 ? grouping1 : grouping2 > 0 ? grouping2 : grouping1;
  53. grouping2 = grouping2 > 0 ? grouping2 : grouping1;
  54. return {grouping1, grouping2, minGrouping, UNUM_GROUPING_COUNT};
  55. }
  56. void Grouper::setLocaleData(const impl::ParsedPatternInfo &patternInfo, const Locale& locale) {
  57. if (fMinGrouping == -2) {
  58. fMinGrouping = getMinGroupingForLocale(locale);
  59. } else if (fMinGrouping == -3) {
  60. fMinGrouping = static_cast<int16_t>(uprv_max(2, getMinGroupingForLocale(locale)));
  61. } else {
  62. // leave fMinGrouping alone
  63. }
  64. if (fGrouping1 != -2 && fGrouping2 != -4) {
  65. return;
  66. }
  67. auto grouping1 = static_cast<int16_t> (patternInfo.positive.groupingSizes & 0xffff);
  68. auto grouping2 = static_cast<int16_t> ((patternInfo.positive.groupingSizes >> 16) & 0xffff);
  69. auto grouping3 = static_cast<int16_t> ((patternInfo.positive.groupingSizes >> 32) & 0xffff);
  70. if (grouping2 == -1) {
  71. grouping1 = fGrouping1 == -4 ? (short) 3 : (short) -1;
  72. }
  73. if (grouping3 == -1) {
  74. grouping2 = grouping1;
  75. }
  76. fGrouping1 = grouping1;
  77. fGrouping2 = grouping2;
  78. }
  79. bool Grouper::groupAtPosition(int32_t position, const impl::DecimalQuantity &value) const {
  80. U_ASSERT(fGrouping1 > -2);
  81. if (fGrouping1 == -1 || fGrouping1 == 0) {
  82. // Either -1 or 0 means "no grouping"
  83. return false;
  84. }
  85. position -= fGrouping1;
  86. return position >= 0 && (position % fGrouping2) == 0
  87. && value.getUpperDisplayMagnitude() - fGrouping1 + 1 >= fMinGrouping;
  88. }
  89. int16_t Grouper::getPrimary() const {
  90. return fGrouping1;
  91. }
  92. int16_t Grouper::getSecondary() const {
  93. return fGrouping2;
  94. }
  95. #endif /* #if !UCONFIG_NO_FORMATTING */