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