simplenumberformatter.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // © 2022 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #ifndef __SIMPLENUMBERFORMATTERH__
  4. #define __SIMPLENUMBERFORMATTERH__
  5. #include "unicode/utypes.h"
  6. #if U_SHOW_CPLUSPLUS_API
  7. #if !UCONFIG_NO_FORMATTING
  8. #include "unicode/dcfmtsym.h"
  9. #include "unicode/usimplenumberformatter.h"
  10. #include "unicode/formattednumber.h"
  11. /**
  12. * \file
  13. * \brief C++ API: Simple number formatting focused on low memory and code size.
  14. *
  15. * These functions render locale-aware number strings but without the bells and whistles found in
  16. * other number formatting APIs such as those in numberformatter.h, like units and currencies.
  17. *
  18. * <pre>
  19. * SimpleNumberFormatter snf = SimpleNumberFormatter::forLocale("de-CH", status);
  20. * FormattedNumber result = snf.formatInt64(-1000007, status);
  21. * assertEquals("", u"-1’000’007", result.toString(status));
  22. * </pre>
  23. */
  24. U_NAMESPACE_BEGIN
  25. /* forward declaration */
  26. class SimpleDateFormat;
  27. namespace number { // icu::number
  28. namespace impl {
  29. class UFormattedNumberData;
  30. struct SimpleMicroProps;
  31. class AdoptingSignumModifierStore;
  32. } // icu::number::impl
  33. /**
  34. * An input type for SimpleNumberFormatter.
  35. *
  36. * This class is mutable and not intended for public subclassing. This class is movable but not copyable.
  37. *
  38. * @stable ICU 73
  39. */
  40. class U_I18N_API SimpleNumber : public UMemory {
  41. public:
  42. /**
  43. * Creates a SimpleNumber for an integer.
  44. *
  45. * @stable ICU 73
  46. */
  47. static SimpleNumber forInt64(int64_t value, UErrorCode& status);
  48. /**
  49. * Changes the value of the SimpleNumber by a power of 10.
  50. *
  51. * This function immediately mutates the inner value.
  52. *
  53. * @stable ICU 73
  54. */
  55. void multiplyByPowerOfTen(int32_t power, UErrorCode& status);
  56. /**
  57. * Rounds the value currently stored in the SimpleNumber to the given power of 10,
  58. * which can be before or after the decimal separator.
  59. *
  60. * This function does not change minimum integer digits.
  61. *
  62. * @stable ICU 73
  63. */
  64. void roundTo(int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode& status);
  65. #ifndef U_HIDE_DRAFT_API
  66. /**
  67. * Sets the number of integer digits to the given amount, truncating if necessary.
  68. *
  69. * @draft ICU 75
  70. */
  71. void setMaximumIntegerDigits(uint32_t maximumIntegerDigits, UErrorCode& status);
  72. #endif // U_HIDE_DRAFT_API
  73. /**
  74. * Pads the beginning of the number with zeros up to the given minimum number of integer digits.
  75. *
  76. * @stable ICU 73
  77. */
  78. void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode& status);
  79. /**
  80. * Pads the end of the number with zeros up to the given minimum number of fraction digits.
  81. *
  82. * @stable ICU 73
  83. */
  84. void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
  85. /**
  86. * Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign.
  87. *
  88. * This setting is applied upon formatting the number.
  89. *
  90. * NOTE: This does not support accounting sign notation.
  91. *
  92. * @stable ICU 73
  93. */
  94. void setSign(USimpleNumberSign sign, UErrorCode& status);
  95. /**
  96. * Creates a new, empty SimpleNumber that does not contain a value.
  97. *
  98. * NOTE: This number will fail to format; use forInt64() to create a SimpleNumber with a value.
  99. *
  100. * @stable ICU 73
  101. */
  102. SimpleNumber() = default;
  103. /**
  104. * Destruct this SimpleNumber, cleaning up any memory it might own.
  105. *
  106. * @stable ICU 73
  107. */
  108. ~SimpleNumber() {
  109. cleanup();
  110. }
  111. /**
  112. * SimpleNumber move constructor.
  113. *
  114. * @stable ICU 73
  115. */
  116. SimpleNumber(SimpleNumber&& other) noexcept {
  117. fData = other.fData;
  118. fSign = other.fSign;
  119. other.fData = nullptr;
  120. }
  121. /**
  122. * SimpleNumber move assignment.
  123. *
  124. * @stable ICU 73
  125. */
  126. SimpleNumber& operator=(SimpleNumber&& other) noexcept {
  127. cleanup();
  128. fData = other.fData;
  129. fSign = other.fSign;
  130. other.fData = nullptr;
  131. return *this;
  132. }
  133. private:
  134. SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
  135. SimpleNumber(const SimpleNumber&) = delete;
  136. SimpleNumber& operator=(const SimpleNumber&) = delete;
  137. void cleanup();
  138. impl::UFormattedNumberData* fData = nullptr;
  139. USimpleNumberSign fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
  140. friend class SimpleNumberFormatter;
  141. // Uses the private constructor to avoid a heap allocation
  142. friend class icu::SimpleDateFormat;
  143. };
  144. /**
  145. * A special NumberFormatter focused on smaller binary size and memory use.
  146. *
  147. * SimpleNumberFormatter is capable of basic number formatting, including grouping separators,
  148. * sign display, and rounding. It is not capable of currencies, compact notation, or units.
  149. *
  150. * This class is immutable and not intended for public subclassing. This class is movable but not copyable.
  151. *
  152. * @stable ICU 73
  153. */
  154. class U_I18N_API SimpleNumberFormatter : public UMemory {
  155. public:
  156. /**
  157. * Creates a new SimpleNumberFormatter with all locale defaults.
  158. *
  159. * @stable ICU 73
  160. */
  161. static SimpleNumberFormatter forLocale(
  162. const icu::Locale &locale,
  163. UErrorCode &status);
  164. /**
  165. * Creates a new SimpleNumberFormatter, overriding the grouping strategy.
  166. *
  167. * @stable ICU 73
  168. */
  169. static SimpleNumberFormatter forLocaleAndGroupingStrategy(
  170. const icu::Locale &locale,
  171. UNumberGroupingStrategy groupingStrategy,
  172. UErrorCode &status);
  173. /**
  174. * Creates a new SimpleNumberFormatter, overriding the grouping strategy and symbols.
  175. *
  176. * IMPORTANT: For efficiency, this function borrows the symbols. The symbols MUST remain valid
  177. * for the lifetime of the SimpleNumberFormatter.
  178. *
  179. * @stable ICU 73
  180. */
  181. static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(
  182. const icu::Locale &locale,
  183. const DecimalFormatSymbols &symbols,
  184. UNumberGroupingStrategy groupingStrategy,
  185. UErrorCode &status);
  186. /**
  187. * Formats a value using this SimpleNumberFormatter.
  188. *
  189. * The SimpleNumber argument is "consumed". A new SimpleNumber object should be created for
  190. * every formatting operation.
  191. *
  192. * @stable ICU 73
  193. */
  194. FormattedNumber format(SimpleNumber value, UErrorCode &status) const;
  195. /**
  196. * Formats an integer using this SimpleNumberFormatter.
  197. *
  198. * For more control over the formatting, use SimpleNumber.
  199. *
  200. * @stable ICU 73
  201. */
  202. FormattedNumber formatInt64(int64_t value, UErrorCode &status) const {
  203. return format(SimpleNumber::forInt64(value, status), status);
  204. }
  205. #ifndef U_HIDE_INTERNAL_API
  206. /**
  207. * Run the formatter with the internal types.
  208. * @internal
  209. */
  210. void formatImpl(impl::UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode& status) const;
  211. #endif // U_HIDE_INTERNAL_API
  212. /**
  213. * Destruct this SimpleNumberFormatter, cleaning up any memory it might own.
  214. *
  215. * @stable ICU 73
  216. */
  217. ~SimpleNumberFormatter() {
  218. cleanup();
  219. }
  220. /**
  221. * Creates a shell, initialized but non-functional SimpleNumberFormatter.
  222. *
  223. * @stable ICU 73
  224. */
  225. SimpleNumberFormatter() = default;
  226. /**
  227. * SimpleNumberFormatter: Move constructor.
  228. *
  229. * @stable ICU 73
  230. */
  231. SimpleNumberFormatter(SimpleNumberFormatter&& other) noexcept {
  232. fGroupingStrategy = other.fGroupingStrategy;
  233. fOwnedSymbols = other.fOwnedSymbols;
  234. fMicros = other.fMicros;
  235. fPatternModifier = other.fPatternModifier;
  236. other.fOwnedSymbols = nullptr;
  237. other.fMicros = nullptr;
  238. other.fPatternModifier = nullptr;
  239. }
  240. /**
  241. * SimpleNumberFormatter: Move assignment.
  242. *
  243. * @stable ICU 73
  244. */
  245. SimpleNumberFormatter& operator=(SimpleNumberFormatter&& other) noexcept {
  246. cleanup();
  247. fGroupingStrategy = other.fGroupingStrategy;
  248. fOwnedSymbols = other.fOwnedSymbols;
  249. fMicros = other.fMicros;
  250. fPatternModifier = other.fPatternModifier;
  251. other.fOwnedSymbols = nullptr;
  252. other.fMicros = nullptr;
  253. other.fPatternModifier = nullptr;
  254. return *this;
  255. }
  256. private:
  257. void initialize(
  258. const icu::Locale &locale,
  259. const DecimalFormatSymbols &symbols,
  260. UNumberGroupingStrategy groupingStrategy,
  261. UErrorCode &status);
  262. void cleanup();
  263. SimpleNumberFormatter(const SimpleNumberFormatter&) = delete;
  264. SimpleNumberFormatter& operator=(const SimpleNumberFormatter&) = delete;
  265. UNumberGroupingStrategy fGroupingStrategy = UNUM_GROUPING_AUTO;
  266. // Owned Pointers:
  267. DecimalFormatSymbols* fOwnedSymbols = nullptr; // can be empty
  268. impl::SimpleMicroProps* fMicros = nullptr;
  269. impl::AdoptingSignumModifierStore* fPatternModifier = nullptr;
  270. };
  271. } // namespace number
  272. U_NAMESPACE_END
  273. #endif /* #if !UCONFIG_NO_FORMATTING */
  274. #endif /* U_SHOW_CPLUSPLUS_API */
  275. #endif // __SIMPLENUMBERFORMATTERH__