numrange_impl.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // © 2018 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 __SOURCE_NUMRANGE_TYPES_H__
  6. #define __SOURCE_NUMRANGE_TYPES_H__
  7. #include "unicode/numberformatter.h"
  8. #include "unicode/numberrangeformatter.h"
  9. #include "unicode/simpleformatter.h"
  10. #include "number_types.h"
  11. #include "number_decimalquantity.h"
  12. #include "number_formatimpl.h"
  13. #include "formatted_string_builder.h"
  14. #include "formattedval_impl.h"
  15. U_NAMESPACE_BEGIN namespace number {
  16. namespace impl {
  17. /**
  18. * Class similar to UFormattedNumberData.
  19. *
  20. * Has incomplete magic number logic that will need to be finished
  21. * if this is to be exposed as C API in the future.
  22. *
  23. * Possible magic number: 0x46445200
  24. * Reads in ASCII as "FDR" (FormatteDnumberRange with room at the end)
  25. */
  26. class UFormattedNumberRangeData : public FormattedValueStringBuilderImpl {
  27. public:
  28. UFormattedNumberRangeData() : FormattedValueStringBuilderImpl(kUndefinedField) {}
  29. virtual ~UFormattedNumberRangeData();
  30. DecimalQuantity quantity1;
  31. DecimalQuantity quantity2;
  32. UNumberRangeIdentityResult identityResult = UNUM_IDENTITY_RESULT_COUNT;
  33. };
  34. class StandardPluralRanges : public UMemory {
  35. public:
  36. void initialize(const Locale& locale, UErrorCode& status);
  37. StandardPlural::Form resolve(StandardPlural::Form first, StandardPlural::Form second) const;
  38. /** Used for data loading. */
  39. void addPluralRange(
  40. StandardPlural::Form first,
  41. StandardPlural::Form second,
  42. StandardPlural::Form result);
  43. /** Used for data loading. */
  44. void setCapacity(int32_t length);
  45. private:
  46. struct StandardPluralRangeTriple {
  47. StandardPlural::Form first;
  48. StandardPlural::Form second;
  49. StandardPlural::Form result;
  50. };
  51. // TODO: An array is simple here, but it results in linear lookup time.
  52. // Certain locales have 20-30 entries in this list.
  53. // Consider changing to a smarter data structure.
  54. typedef MaybeStackArray<StandardPluralRangeTriple, 3> PluralRangeTriples;
  55. PluralRangeTriples fTriples;
  56. int32_t fTriplesLen = 0;
  57. };
  58. class NumberRangeFormatterImpl : public UMemory {
  59. public:
  60. NumberRangeFormatterImpl(const RangeMacroProps& macros, UErrorCode& status);
  61. void format(UFormattedNumberRangeData& data, bool equalBeforeRounding, UErrorCode& status) const;
  62. private:
  63. NumberFormatterImpl formatterImpl1;
  64. NumberFormatterImpl formatterImpl2;
  65. bool fSameFormatters;
  66. UNumberRangeCollapse fCollapse;
  67. UNumberRangeIdentityFallback fIdentityFallback;
  68. SimpleFormatter fRangeFormatter;
  69. SimpleModifier fApproximatelyModifier;
  70. StandardPluralRanges fPluralRanges;
  71. void formatSingleValue(UFormattedNumberRangeData& data,
  72. MicroProps& micros1, MicroProps& micros2,
  73. UErrorCode& status) const;
  74. void formatApproximately(UFormattedNumberRangeData& data,
  75. MicroProps& micros1, MicroProps& micros2,
  76. UErrorCode& status) const;
  77. void formatRange(UFormattedNumberRangeData& data,
  78. MicroProps& micros1, MicroProps& micros2,
  79. UErrorCode& status) const;
  80. const Modifier& resolveModifierPlurals(const Modifier& first, const Modifier& second) const;
  81. };
  82. } // namespace impl
  83. } // namespace number
  84. U_NAMESPACE_END
  85. #endif //__SOURCE_NUMRANGE_TYPES_H__
  86. #endif /* #if !UCONFIG_NO_FORMATTING */