number_utils.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef __NUMBER_UTILS_H__
  6. #define __NUMBER_UTILS_H__
  7. #include "unicode/numberformatter.h"
  8. #include "number_types.h"
  9. #include "number_decimalquantity.h"
  10. #include "number_scientific.h"
  11. #include "number_patternstring.h"
  12. #include "number_modifiers.h"
  13. #include "number_multiplier.h"
  14. #include "number_roundingutils.h"
  15. #include "decNumber.h"
  16. #include "charstr.h"
  17. #include "formatted_string_builder.h"
  18. U_NAMESPACE_BEGIN
  19. namespace number {
  20. namespace impl {
  21. enum CldrPatternStyle {
  22. CLDR_PATTERN_STYLE_DECIMAL,
  23. CLDR_PATTERN_STYLE_CURRENCY,
  24. CLDR_PATTERN_STYLE_ACCOUNTING,
  25. CLDR_PATTERN_STYLE_PERCENT,
  26. CLDR_PATTERN_STYLE_SCIENTIFIC,
  27. CLDR_PATTERN_STYLE_COUNT,
  28. };
  29. // Namespace for naked functions
  30. namespace utils {
  31. inline int32_t insertDigitFromSymbols(FormattedStringBuilder& output, int32_t index, int8_t digit,
  32. const DecimalFormatSymbols& symbols, Field field,
  33. UErrorCode& status) {
  34. if (symbols.getCodePointZero() != -1) {
  35. return output.insertCodePoint(index, symbols.getCodePointZero() + digit, field, status);
  36. }
  37. return output.insert(index, symbols.getConstDigitSymbol(digit), field, status);
  38. }
  39. inline bool unitIsCurrency(const MeasureUnit& unit) {
  40. return uprv_strcmp("currency", unit.getType()) == 0;
  41. }
  42. inline bool unitIsNoUnit(const MeasureUnit& unit) {
  43. return uprv_strcmp("none", unit.getType()) == 0;
  44. }
  45. inline bool unitIsPercent(const MeasureUnit& unit) {
  46. return uprv_strcmp("percent", unit.getSubtype()) == 0;
  47. }
  48. inline bool unitIsPermille(const MeasureUnit& unit) {
  49. return uprv_strcmp("permille", unit.getSubtype()) == 0;
  50. }
  51. // NOTE: In Java, this method is in NumberFormat.java
  52. const char16_t*
  53. getPatternForStyle(const Locale& locale, const char* nsName, CldrPatternStyle style, UErrorCode& status);
  54. /**
  55. * Computes the plural form for this number based on the specified set of rules.
  56. *
  57. * @param rules A {@link PluralRules} object representing the set of rules.
  58. * @return The {@link StandardPlural} according to the PluralRules. If the plural form is not in
  59. * the set of standard plurals, {@link StandardPlural#OTHER} is returned instead.
  60. */
  61. inline StandardPlural::Form getStandardPlural(const PluralRules *rules,
  62. const IFixedDecimal &fdec) {
  63. if (rules == nullptr) {
  64. // Fail gracefully if the user didn't provide a PluralRules
  65. return StandardPlural::Form::OTHER;
  66. } else {
  67. UnicodeString ruleString = rules->select(fdec);
  68. return StandardPlural::orOtherFromString(ruleString);
  69. }
  70. }
  71. /**
  72. * Computes the plural form after copying the number and applying rounding rules.
  73. */
  74. inline StandardPlural::Form getPluralSafe(
  75. const RoundingImpl& rounder,
  76. const PluralRules* rules,
  77. const DecimalQuantity& dq,
  78. UErrorCode& status) {
  79. // TODO(ICU-20500): Avoid the copy?
  80. DecimalQuantity copy(dq);
  81. rounder.apply(copy, status);
  82. if (U_FAILURE(status)) {
  83. return StandardPlural::Form::OTHER;
  84. }
  85. return getStandardPlural(rules, copy);
  86. }
  87. } // namespace utils
  88. } // namespace impl
  89. } // namespace number
  90. U_NAMESPACE_END
  91. #endif //__NUMBER_UTILS_H__
  92. #endif /* #if !UCONFIG_NO_FORMATTING */