simplenumberformatter.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #ifndef U_HIDE_DEPRECATED_API
  74. /**
  75. * Alias for setMaximumIntegerDigits.
  76. * Will be removed after ICU 75.
  77. *
  78. * @deprecated ICU 75
  79. */
  80. void truncateStart(uint32_t maximumIntegerDigits, UErrorCode& status);
  81. #endif // U_HIDE_DEPRECATED_API
  82. /**
  83. * Pads the beginning of the number with zeros up to the given minimum number of integer digits.
  84. *
  85. * @stable ICU 73
  86. */
  87. void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode& status);
  88. /**
  89. * Pads the end of the number with zeros up to the given minimum number of fraction digits.
  90. *
  91. * @stable ICU 73
  92. */
  93. void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
  94. /**
  95. * Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign.
  96. *
  97. * This setting is applied upon formatting the number.
  98. *
  99. * NOTE: This does not support accounting sign notation.
  100. *
  101. * @stable ICU 73
  102. */
  103. void setSign(USimpleNumberSign sign, UErrorCode& status);
  104. /**
  105. * Creates a new, empty SimpleNumber that does not contain a value.
  106. *
  107. * NOTE: This number will fail to format; use forInt64() to create a SimpleNumber with a value.
  108. *
  109. * @stable ICU 73
  110. */
  111. SimpleNumber() = default;
  112. /**
  113. * Destruct this SimpleNumber, cleaning up any memory it might own.
  114. *
  115. * @stable ICU 73
  116. */
  117. ~SimpleNumber() {
  118. cleanup();
  119. }
  120. /**
  121. * SimpleNumber move constructor.
  122. *
  123. * @stable ICU 73
  124. */
  125. SimpleNumber(SimpleNumber&& other) noexcept {
  126. fData = other.fData;
  127. fSign = other.fSign;
  128. other.fData = nullptr;
  129. }
  130. /**
  131. * SimpleNumber move assignment.
  132. *
  133. * @stable ICU 73
  134. */
  135. SimpleNumber& operator=(SimpleNumber&& other) noexcept {
  136. cleanup();
  137. fData = other.fData;
  138. fSign = other.fSign;
  139. other.fData = nullptr;
  140. return *this;
  141. }
  142. private:
  143. SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
  144. SimpleNumber(const SimpleNumber&) = delete;
  145. SimpleNumber& operator=(const SimpleNumber&) = delete;
  146. void cleanup();
  147. impl::UFormattedNumberData* fData = nullptr;
  148. USimpleNumberSign fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
  149. friend class SimpleNumberFormatter;
  150. // Uses the private constructor to avoid a heap allocation
  151. friend class icu::SimpleDateFormat;
  152. };
  153. /**
  154. * A special NumberFormatter focused on smaller binary size and memory use.
  155. *
  156. * SimpleNumberFormatter is capable of basic number formatting, including grouping separators,
  157. * sign display, and rounding. It is not capable of currencies, compact notation, or units.
  158. *
  159. * This class is immutable and not intended for public subclassing. This class is movable but not copyable.
  160. *
  161. * @stable ICU 73
  162. */
  163. class U_I18N_API SimpleNumberFormatter : public UMemory {
  164. public:
  165. /**
  166. * Creates a new SimpleNumberFormatter with all locale defaults.
  167. *
  168. * @stable ICU 73
  169. */
  170. static SimpleNumberFormatter forLocale(
  171. const icu::Locale &locale,
  172. UErrorCode &status);
  173. /**
  174. * Creates a new SimpleNumberFormatter, overriding the grouping strategy.
  175. *
  176. * @stable ICU 73
  177. */
  178. static SimpleNumberFormatter forLocaleAndGroupingStrategy(
  179. const icu::Locale &locale,
  180. UNumberGroupingStrategy groupingStrategy,
  181. UErrorCode &status);
  182. /**
  183. * Creates a new SimpleNumberFormatter, overriding the grouping strategy and symbols.
  184. *
  185. * IMPORTANT: For efficiency, this function borrows the symbols. The symbols MUST remain valid
  186. * for the lifetime of the SimpleNumberFormatter.
  187. *
  188. * @stable ICU 73
  189. */
  190. static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(
  191. const icu::Locale &locale,
  192. const DecimalFormatSymbols &symbols,
  193. UNumberGroupingStrategy groupingStrategy,
  194. UErrorCode &status);
  195. /**
  196. * Formats a value using this SimpleNumberFormatter.
  197. *
  198. * The SimpleNumber argument is "consumed". A new SimpleNumber object should be created for
  199. * every formatting operation.
  200. *
  201. * @stable ICU 73
  202. */
  203. FormattedNumber format(SimpleNumber value, UErrorCode &status) const;
  204. /**
  205. * Formats an integer using this SimpleNumberFormatter.
  206. *
  207. * For more control over the formatting, use SimpleNumber.
  208. *
  209. * @stable ICU 73
  210. */
  211. FormattedNumber formatInt64(int64_t value, UErrorCode &status) const {
  212. return format(SimpleNumber::forInt64(value, status), status);
  213. }
  214. #ifndef U_HIDE_INTERNAL_API
  215. /**
  216. * Run the formatter with the internal types.
  217. * @internal
  218. */
  219. void formatImpl(impl::UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode& status) const;
  220. #endif // U_HIDE_INTERNAL_API
  221. /**
  222. * Destruct this SimpleNumberFormatter, cleaning up any memory it might own.
  223. *
  224. * @stable ICU 73
  225. */
  226. ~SimpleNumberFormatter() {
  227. cleanup();
  228. }
  229. /**
  230. * Creates a shell, initialized but non-functional SimpleNumberFormatter.
  231. *
  232. * @stable ICU 73
  233. */
  234. SimpleNumberFormatter() = default;
  235. /**
  236. * SimpleNumberFormatter: Move constructor.
  237. *
  238. * @stable ICU 73
  239. */
  240. SimpleNumberFormatter(SimpleNumberFormatter&& other) noexcept {
  241. fGroupingStrategy = other.fGroupingStrategy;
  242. fOwnedSymbols = other.fOwnedSymbols;
  243. fMicros = other.fMicros;
  244. fPatternModifier = other.fPatternModifier;
  245. other.fOwnedSymbols = nullptr;
  246. other.fMicros = nullptr;
  247. other.fPatternModifier = nullptr;
  248. }
  249. /**
  250. * SimpleNumberFormatter: Move assignment.
  251. *
  252. * @stable ICU 73
  253. */
  254. SimpleNumberFormatter& operator=(SimpleNumberFormatter&& other) noexcept {
  255. cleanup();
  256. fGroupingStrategy = other.fGroupingStrategy;
  257. fOwnedSymbols = other.fOwnedSymbols;
  258. fMicros = other.fMicros;
  259. fPatternModifier = other.fPatternModifier;
  260. other.fOwnedSymbols = nullptr;
  261. other.fMicros = nullptr;
  262. other.fPatternModifier = nullptr;
  263. return *this;
  264. }
  265. private:
  266. void initialize(
  267. const icu::Locale &locale,
  268. const DecimalFormatSymbols &symbols,
  269. UNumberGroupingStrategy groupingStrategy,
  270. UErrorCode &status);
  271. void cleanup();
  272. SimpleNumberFormatter(const SimpleNumberFormatter&) = delete;
  273. SimpleNumberFormatter& operator=(const SimpleNumberFormatter&) = delete;
  274. UNumberGroupingStrategy fGroupingStrategy = UNUM_GROUPING_AUTO;
  275. // Owned Pointers:
  276. DecimalFormatSymbols* fOwnedSymbols = nullptr; // can be empty
  277. impl::SimpleMicroProps* fMicros = nullptr;
  278. impl::AdoptingSignumModifierStore* fPatternModifier = nullptr;
  279. };
  280. } // namespace number
  281. U_NAMESPACE_END
  282. #endif /* #if !UCONFIG_NO_FORMATTING */
  283. #endif /* U_SHOW_CPLUSPLUS_API */
  284. #endif // __SIMPLENUMBERFORMATTERH__