simplenumberformatter.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. namespace number { // icu::number
  26. namespace impl {
  27. class UFormattedNumberData;
  28. struct SimpleMicroProps;
  29. class AdoptingSignumModifierStore;
  30. } // icu::number::impl
  31. #ifndef U_HIDE_DRAFT_API
  32. /**
  33. * An input type for SimpleNumberFormatter.
  34. *
  35. * This class is mutable and not intended for public subclassing. This class is movable but not copyable.
  36. *
  37. * @draft ICU 73
  38. */
  39. class U_I18N_API SimpleNumber : public UMemory {
  40. public:
  41. /**
  42. * Creates a SimpleNumber for an integer.
  43. *
  44. * @draft ICU 73
  45. */
  46. static SimpleNumber forInt64(int64_t value, UErrorCode& status);
  47. /**
  48. * Changes the value of the SimpleNumber by a power of 10.
  49. *
  50. * This function immediately mutates the inner value.
  51. *
  52. * @draft ICU 73
  53. */
  54. void multiplyByPowerOfTen(int32_t power, UErrorCode& status);
  55. /**
  56. * Rounds the value currently stored in the SimpleNumber to the given power of 10.
  57. *
  58. * This function immediately mutates the inner value.
  59. *
  60. * @draft ICU 73
  61. */
  62. void roundTo(int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode& status);
  63. /**
  64. * Truncates the most significant digits to the given maximum number of integer digits.
  65. *
  66. * This function immediately mutates the inner value.
  67. *
  68. * @draft ICU 73
  69. */
  70. void truncateStart(uint32_t maximumIntegerDigits, UErrorCode& status);
  71. /**
  72. * Pads the beginning of the number with zeros up to the given minimum number of integer digits.
  73. *
  74. * This setting is applied upon formatting the number.
  75. *
  76. * @draft 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. * This setting is applied upon formatting the number.
  83. *
  84. * @draft ICU 73
  85. */
  86. void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
  87. /**
  88. * Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign.
  89. *
  90. * This setting is applied upon formatting the number.
  91. *
  92. * NOTE: This does not support accounting sign notation.
  93. *
  94. * @draft ICU 73
  95. */
  96. void setSign(USimpleNumberSign sign, UErrorCode& status);
  97. /**
  98. * Creates a new, empty SimpleNumber that does not contain a value.
  99. *
  100. * NOTE: This number will fail to format; use forInt64() to create a SimpleNumber with a value.
  101. *
  102. * @draft ICU 73
  103. */
  104. SimpleNumber() = default;
  105. /**
  106. * Destruct this SimpleNumber, cleaning up any memory it might own.
  107. *
  108. * @draft ICU 73
  109. */
  110. ~SimpleNumber() {
  111. cleanup();
  112. }
  113. /**
  114. * SimpleNumber move constructor.
  115. *
  116. * @draft ICU 73
  117. */
  118. SimpleNumber(SimpleNumber&& other) noexcept {
  119. fData = other.fData;
  120. fSign = other.fSign;
  121. other.fData = nullptr;
  122. }
  123. /**
  124. * SimpleNumber move assignment.
  125. *
  126. * @draft ICU 73
  127. */
  128. SimpleNumber& operator=(SimpleNumber&& other) noexcept {
  129. cleanup();
  130. fData = other.fData;
  131. fSign = other.fSign;
  132. other.fData = nullptr;
  133. return *this;
  134. }
  135. private:
  136. SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
  137. SimpleNumber(const SimpleNumber&) = delete;
  138. SimpleNumber& operator=(const SimpleNumber&) = delete;
  139. void cleanup();
  140. impl::UFormattedNumberData* fData = nullptr;
  141. USimpleNumberSign fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
  142. friend class SimpleNumberFormatter;
  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. * @draft 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. * @draft 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. * @draft 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. * @draft 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. * @draft 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. * @draft 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. * @draft ICU 73
  216. */
  217. ~SimpleNumberFormatter() {
  218. cleanup();
  219. }
  220. /**
  221. * Creates a shell, initialized but non-functional SimpleNumberFormatter.
  222. *
  223. * @draft ICU 73
  224. */
  225. SimpleNumberFormatter() = default;
  226. /**
  227. * SimpleNumberFormatter: Move constructor.
  228. *
  229. * @draft 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. * @draft 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. #endif // U_HIDE_DRAFT_API
  272. } // namespace number
  273. U_NAMESPACE_END
  274. #endif /* #if !UCONFIG_NO_FORMATTING */
  275. #endif /* U_SHOW_CPLUSPLUS_API */
  276. #endif // __SIMPLENUMBERFORMATTERH__