number_simple.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "unicode/simplenumberformatter.h"
  7. #include "number_formatimpl.h"
  8. #include "number_utils.h"
  9. #include "number_patternmodifier.h"
  10. #include "number_utypes.h"
  11. using namespace icu;
  12. using namespace icu::number;
  13. using namespace icu::number::impl;
  14. SimpleNumber
  15. SimpleNumber::forInt64(int64_t value, UErrorCode& status) {
  16. if (U_FAILURE(status)) {
  17. return SimpleNumber();
  18. }
  19. auto results = new UFormattedNumberData();
  20. if (results == nullptr) {
  21. status = U_MEMORY_ALLOCATION_ERROR;
  22. return SimpleNumber();
  23. }
  24. results->quantity.setToLong(value);
  25. return SimpleNumber(results, status);
  26. }
  27. SimpleNumber::SimpleNumber(UFormattedNumberData* data, UErrorCode& status) : fData(data) {
  28. if (U_FAILURE(status)) {
  29. return;
  30. }
  31. if (fData == nullptr) {
  32. status = U_ILLEGAL_ARGUMENT_ERROR;
  33. return;
  34. }
  35. if (fData->quantity.isNegative()) {
  36. fSign = UNUM_SIMPLE_NUMBER_MINUS_SIGN;
  37. } else {
  38. fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
  39. }
  40. }
  41. void SimpleNumber::cleanup() {
  42. delete fData;
  43. fData = nullptr;
  44. }
  45. void SimpleNumber::multiplyByPowerOfTen(int32_t power, UErrorCode& status) {
  46. if (U_FAILURE(status)) {
  47. return;
  48. }
  49. if (fData == nullptr) {
  50. status = U_INVALID_STATE_ERROR;
  51. return;
  52. }
  53. fData->quantity.adjustMagnitude(power);
  54. }
  55. void SimpleNumber::roundTo(int32_t position, UNumberFormatRoundingMode roundingMode, UErrorCode& status) {
  56. if (U_FAILURE(status)) {
  57. return;
  58. }
  59. if (fData == nullptr) {
  60. status = U_INVALID_STATE_ERROR;
  61. return;
  62. }
  63. fData->quantity.roundToMagnitude(position, roundingMode, status);
  64. }
  65. void SimpleNumber::setMinimumIntegerDigits(uint32_t position, UErrorCode& status) {
  66. if (U_FAILURE(status)) {
  67. return;
  68. }
  69. if (fData == nullptr) {
  70. status = U_INVALID_STATE_ERROR;
  71. return;
  72. }
  73. fData->quantity.setMinInteger(position);
  74. }
  75. void SimpleNumber::setMinimumFractionDigits(uint32_t position, UErrorCode& status) {
  76. if (U_FAILURE(status)) {
  77. return;
  78. }
  79. if (fData == nullptr) {
  80. status = U_INVALID_STATE_ERROR;
  81. return;
  82. }
  83. fData->quantity.setMinFraction(position);
  84. }
  85. void SimpleNumber::truncateStart(uint32_t position, UErrorCode& status) {
  86. if (U_FAILURE(status)) {
  87. return;
  88. }
  89. if (fData == nullptr) {
  90. status = U_INVALID_STATE_ERROR;
  91. return;
  92. }
  93. fData->quantity.applyMaxInteger(position);
  94. }
  95. void SimpleNumber::setSign(USimpleNumberSign sign, UErrorCode& status) {
  96. if (U_FAILURE(status)) {
  97. return;
  98. }
  99. if (fData == nullptr) {
  100. status = U_INVALID_STATE_ERROR;
  101. return;
  102. }
  103. fSign = sign;
  104. }
  105. void SimpleNumberFormatter::cleanup() {
  106. delete fOwnedSymbols;
  107. delete fMicros;
  108. delete fPatternModifier;
  109. fOwnedSymbols = nullptr;
  110. fMicros = nullptr;
  111. fPatternModifier = nullptr;
  112. }
  113. SimpleNumberFormatter SimpleNumberFormatter::forLocale(const icu::Locale &locale, UErrorCode &status) {
  114. return SimpleNumberFormatter::forLocaleAndGroupingStrategy(locale, UNUM_GROUPING_AUTO, status);
  115. }
  116. SimpleNumberFormatter SimpleNumberFormatter::forLocaleAndGroupingStrategy(
  117. const icu::Locale &locale,
  118. UNumberGroupingStrategy groupingStrategy,
  119. UErrorCode &status) {
  120. SimpleNumberFormatter retval;
  121. retval.fOwnedSymbols = new DecimalFormatSymbols(locale, status);
  122. if (U_FAILURE(status)) {
  123. return retval;
  124. }
  125. if (retval.fOwnedSymbols == nullptr) {
  126. status = U_MEMORY_ALLOCATION_ERROR;
  127. return retval;
  128. }
  129. retval.initialize(locale, *retval.fOwnedSymbols, groupingStrategy, status);
  130. return retval;
  131. }
  132. SimpleNumberFormatter SimpleNumberFormatter::forLocaleAndSymbolsAndGroupingStrategy(
  133. const icu::Locale &locale,
  134. const DecimalFormatSymbols &symbols,
  135. UNumberGroupingStrategy groupingStrategy,
  136. UErrorCode &status) {
  137. SimpleNumberFormatter retval;
  138. retval.initialize(locale, symbols, groupingStrategy, status);
  139. return retval;
  140. }
  141. void SimpleNumberFormatter::initialize(
  142. const icu::Locale &locale,
  143. const DecimalFormatSymbols &symbols,
  144. UNumberGroupingStrategy groupingStrategy,
  145. UErrorCode &status) {
  146. if (U_FAILURE(status)) {
  147. return;
  148. }
  149. fMicros = new SimpleMicroProps();
  150. if (fMicros == nullptr) {
  151. status = U_MEMORY_ALLOCATION_ERROR;
  152. return;
  153. }
  154. fMicros->symbols = &symbols;
  155. auto pattern = utils::getPatternForStyle(
  156. locale,
  157. symbols.getNumberingSystemName(),
  158. CLDR_PATTERN_STYLE_DECIMAL,
  159. status);
  160. if (U_FAILURE(status)) {
  161. return;
  162. }
  163. ParsedPatternInfo patternInfo;
  164. PatternParser::parseToPatternInfo(UnicodeString(pattern), patternInfo, status);
  165. if (U_FAILURE(status)) {
  166. return;
  167. }
  168. auto grouper = Grouper::forStrategy(groupingStrategy);
  169. grouper.setLocaleData(patternInfo, locale);
  170. fMicros->grouping = grouper;
  171. MutablePatternModifier patternModifier(false);
  172. patternModifier.setPatternInfo(&patternInfo, kUndefinedField);
  173. patternModifier.setPatternAttributes(UNUM_SIGN_EXCEPT_ZERO, false, false);
  174. patternModifier.setSymbols(fMicros->symbols, {}, UNUM_UNIT_WIDTH_SHORT, nullptr, status);
  175. fPatternModifier = new AdoptingSignumModifierStore(patternModifier.createImmutableForPlural(StandardPlural::COUNT, status));
  176. fGroupingStrategy = groupingStrategy;
  177. return;
  178. }
  179. FormattedNumber SimpleNumberFormatter::format(SimpleNumber value, UErrorCode &status) const {
  180. formatImpl(value.fData, value.fSign, status);
  181. // Do not save the results object if we encountered a failure.
  182. if (U_SUCCESS(status)) {
  183. auto temp = value.fData;
  184. value.fData = nullptr;
  185. return FormattedNumber(temp);
  186. } else {
  187. return FormattedNumber(status);
  188. }
  189. }
  190. void SimpleNumberFormatter::formatImpl(UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode &status) const {
  191. if (U_FAILURE(status)) {
  192. return;
  193. }
  194. if (data == nullptr) {
  195. status = U_ILLEGAL_ARGUMENT_ERROR;
  196. return;
  197. }
  198. if (fPatternModifier == nullptr || fMicros == nullptr) {
  199. status = U_INVALID_STATE_ERROR;
  200. return;
  201. }
  202. Signum signum;
  203. if (sign == UNUM_SIMPLE_NUMBER_MINUS_SIGN) {
  204. signum = SIGNUM_NEG;
  205. } else if (sign == UNUM_SIMPLE_NUMBER_PLUS_SIGN) {
  206. signum = SIGNUM_POS;
  207. } else {
  208. signum = SIGNUM_POS_ZERO;
  209. }
  210. const Modifier* modifier = (*fPatternModifier)[signum];
  211. auto length = NumberFormatterImpl::writeNumber(
  212. *fMicros,
  213. data->quantity,
  214. data->getStringRef(),
  215. 0,
  216. status);
  217. length += modifier->apply(data->getStringRef(), 0, length, status);
  218. data->getStringRef().writeTerminator(status);
  219. }
  220. #endif /* #if !UCONFIG_NO_FORMATTING */