Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-04 10:00:25

0001 // © 2022 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 
0004 #ifndef __SIMPLENUMBERFORMATTERH__
0005 #define __SIMPLENUMBERFORMATTERH__
0006 
0007 #include "unicode/utypes.h"
0008 
0009 #if U_SHOW_CPLUSPLUS_API
0010 
0011 #if !UCONFIG_NO_FORMATTING
0012 
0013 #include "unicode/dcfmtsym.h"
0014 #include "unicode/usimplenumberformatter.h"
0015 #include "unicode/formattednumber.h"
0016 
0017 /**
0018  * \file
0019  * \brief C++ API: Simple number formatting focused on low memory and code size.
0020  *
0021  * These functions render locale-aware number strings but without the bells and whistles found in
0022  * other number formatting APIs such as those in numberformatter.h, like units and currencies.
0023  *
0024  * <pre>
0025  * SimpleNumberFormatter snf = SimpleNumberFormatter::forLocale("de-CH", status);
0026  * FormattedNumber result = snf.formatInt64(-1000007, status);
0027  * assertEquals("", u"-1’000’007", result.toString(status));
0028  * </pre>
0029  */
0030 
0031 U_NAMESPACE_BEGIN
0032 
0033 /* forward declaration */
0034 class SimpleDateFormat;
0035 
0036 namespace number {  // icu::number
0037 
0038 
0039 namespace impl {
0040 class UFormattedNumberData;
0041 struct SimpleMicroProps;
0042 class AdoptingSignumModifierStore;
0043 }  // icu::number::impl
0044 
0045 
0046 /**
0047  * An input type for SimpleNumberFormatter.
0048  *
0049  * This class is mutable and not intended for public subclassing. This class is movable but not copyable.
0050  *
0051  * @stable ICU 73
0052  */
0053 class U_I18N_API SimpleNumber : public UMemory {
0054   public:
0055     /**
0056      * Creates a SimpleNumber for an integer.
0057      *
0058      * @stable ICU 73
0059      */
0060     static SimpleNumber forInt64(int64_t value, UErrorCode& status);
0061 
0062     /**
0063      * Changes the value of the SimpleNumber by a power of 10.
0064      *
0065      * This function immediately mutates the inner value.
0066      *
0067      * @stable ICU 73
0068      */
0069     void multiplyByPowerOfTen(int32_t power, UErrorCode& status);
0070 
0071     /**
0072      * Rounds the value currently stored in the SimpleNumber to the given power of 10,
0073      * which can be before or after the decimal separator.
0074      *
0075      * This function does not change minimum integer digits.
0076      *
0077      * @stable ICU 73
0078      */
0079     void roundTo(int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode& status);
0080 
0081 #ifndef U_HIDE_DRAFT_API
0082     /**
0083      * Sets the number of integer digits to the given amount, truncating if necessary.
0084      *
0085      * @draft ICU 75
0086      */
0087     void setMaximumIntegerDigits(uint32_t maximumIntegerDigits, UErrorCode& status);
0088 #endif // U_HIDE_DRAFT_API
0089 
0090     /**
0091      * Pads the beginning of the number with zeros up to the given minimum number of integer digits.
0092      *
0093      * @stable ICU 73
0094      */
0095     void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode& status);
0096 
0097     /**
0098      * Pads the end of the number with zeros up to the given minimum number of fraction digits.
0099      *
0100      * @stable ICU 73
0101      */
0102     void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
0103 
0104     /**
0105      * Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign.
0106      *
0107      * This setting is applied upon formatting the number.
0108      *
0109      * NOTE: This does not support accounting sign notation.
0110      *
0111      * @stable ICU 73
0112      */
0113     void setSign(USimpleNumberSign sign, UErrorCode& status);
0114 
0115     /**
0116      * Creates a new, empty SimpleNumber that does not contain a value.
0117      * 
0118      * NOTE: This number will fail to format; use forInt64() to create a SimpleNumber with a value.
0119      *
0120      * @stable ICU 73
0121      */
0122     SimpleNumber() = default;
0123 
0124     /**
0125      * Destruct this SimpleNumber, cleaning up any memory it might own.
0126      *
0127      * @stable ICU 73
0128      */
0129     ~SimpleNumber() {
0130         cleanup();
0131     }
0132 
0133     /**
0134      * SimpleNumber move constructor.
0135      *
0136      * @stable ICU 73
0137      */
0138     SimpleNumber(SimpleNumber&& other) noexcept {
0139         fData = other.fData;
0140         fSign = other.fSign;
0141         other.fData = nullptr;
0142     }
0143 
0144     /**
0145      * SimpleNumber move assignment.
0146      *
0147      * @stable ICU 73
0148      */
0149     SimpleNumber& operator=(SimpleNumber&& other) noexcept {
0150         cleanup();
0151         fData = other.fData;
0152         fSign = other.fSign;
0153         other.fData = nullptr;
0154         return *this;
0155     }
0156 
0157   private:
0158     SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
0159     SimpleNumber(const SimpleNumber&) = delete;
0160     SimpleNumber& operator=(const SimpleNumber&) = delete;
0161 
0162     void cleanup();
0163 
0164     impl::UFormattedNumberData* fData = nullptr;
0165     USimpleNumberSign fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
0166 
0167     friend class SimpleNumberFormatter;
0168 
0169     // Uses the private constructor to avoid a heap allocation
0170     friend class icu::SimpleDateFormat;
0171 };
0172 
0173 
0174 /**
0175  * A special NumberFormatter focused on smaller binary size and memory use.
0176  * 
0177  * SimpleNumberFormatter is capable of basic number formatting, including grouping separators,
0178  * sign display, and rounding. It is not capable of currencies, compact notation, or units.
0179  *
0180  * This class is immutable and not intended for public subclassing. This class is movable but not copyable.
0181  *
0182  * @stable ICU 73
0183  */
0184 class U_I18N_API SimpleNumberFormatter : public UMemory {
0185   public:
0186     /**
0187      * Creates a new SimpleNumberFormatter with all locale defaults.
0188      *
0189      * @stable ICU 73
0190      */
0191     static SimpleNumberFormatter forLocale(
0192         const icu::Locale &locale,
0193         UErrorCode &status);
0194 
0195     /**
0196      * Creates a new SimpleNumberFormatter, overriding the grouping strategy.
0197      *
0198      * @stable ICU 73
0199      */
0200     static SimpleNumberFormatter forLocaleAndGroupingStrategy(
0201         const icu::Locale &locale,
0202         UNumberGroupingStrategy groupingStrategy,
0203         UErrorCode &status);
0204 
0205     /**
0206      * Creates a new SimpleNumberFormatter, overriding the grouping strategy and symbols.
0207      *
0208      * IMPORTANT: For efficiency, this function borrows the symbols. The symbols MUST remain valid
0209      * for the lifetime of the SimpleNumberFormatter.
0210      *
0211      * @stable ICU 73
0212      */
0213     static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(
0214         const icu::Locale &locale,
0215         const DecimalFormatSymbols &symbols,
0216         UNumberGroupingStrategy groupingStrategy,
0217         UErrorCode &status);
0218 
0219     /**
0220      * Formats a value using this SimpleNumberFormatter.
0221      *
0222      * The SimpleNumber argument is "consumed". A new SimpleNumber object should be created for
0223      * every formatting operation.
0224      *
0225      * @stable ICU 73
0226      */
0227     FormattedNumber format(SimpleNumber value, UErrorCode &status) const;
0228 
0229     /**
0230      * Formats an integer using this SimpleNumberFormatter.
0231      *
0232      * For more control over the formatting, use SimpleNumber.
0233      *
0234      * @stable ICU 73
0235      */
0236     FormattedNumber formatInt64(int64_t value, UErrorCode &status) const {
0237         return format(SimpleNumber::forInt64(value, status), status);
0238     }
0239 
0240 #ifndef U_HIDE_INTERNAL_API
0241     /**
0242      * Run the formatter with the internal types.
0243      * @internal
0244      */
0245     void formatImpl(impl::UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode& status) const;
0246 #endif // U_HIDE_INTERNAL_API
0247 
0248     /**
0249      * Destruct this SimpleNumberFormatter, cleaning up any memory it might own.
0250      *
0251      * @stable ICU 73
0252      */
0253     ~SimpleNumberFormatter() {
0254         cleanup();
0255     }
0256 
0257     /**
0258      * Creates a shell, initialized but non-functional SimpleNumberFormatter.
0259      *
0260      * @stable ICU 73
0261      */
0262     SimpleNumberFormatter() = default;
0263 
0264     /**
0265      * SimpleNumberFormatter: Move constructor.
0266      *
0267      * @stable ICU 73
0268      */
0269     SimpleNumberFormatter(SimpleNumberFormatter&& other) noexcept {
0270         fGroupingStrategy = other.fGroupingStrategy;
0271         fOwnedSymbols = other.fOwnedSymbols;
0272         fMicros = other.fMicros;
0273         fPatternModifier = other.fPatternModifier;
0274         other.fOwnedSymbols = nullptr;
0275         other.fMicros = nullptr;
0276         other.fPatternModifier = nullptr;
0277     }
0278 
0279     /**
0280      * SimpleNumberFormatter: Move assignment.
0281      *
0282      * @stable ICU 73
0283      */
0284     SimpleNumberFormatter& operator=(SimpleNumberFormatter&& other) noexcept {
0285         cleanup();
0286         fGroupingStrategy = other.fGroupingStrategy;
0287         fOwnedSymbols = other.fOwnedSymbols;
0288         fMicros = other.fMicros;
0289         fPatternModifier = other.fPatternModifier;
0290         other.fOwnedSymbols = nullptr;
0291         other.fMicros = nullptr;
0292         other.fPatternModifier = nullptr;
0293         return *this;
0294     }
0295 
0296   private:
0297     void initialize(
0298         const icu::Locale &locale,
0299         const DecimalFormatSymbols &symbols,
0300         UNumberGroupingStrategy groupingStrategy,
0301         UErrorCode &status);
0302 
0303     void cleanup();
0304 
0305     SimpleNumberFormatter(const SimpleNumberFormatter&) = delete;
0306 
0307     SimpleNumberFormatter& operator=(const SimpleNumberFormatter&) = delete;
0308 
0309     UNumberGroupingStrategy fGroupingStrategy = UNUM_GROUPING_AUTO;
0310 
0311     // Owned Pointers:
0312     DecimalFormatSymbols* fOwnedSymbols = nullptr; // can be empty
0313     impl::SimpleMicroProps* fMicros = nullptr;
0314     impl::AdoptingSignumModifierStore* fPatternModifier = nullptr;
0315 };
0316 
0317 
0318 }  // namespace number
0319 U_NAMESPACE_END
0320 
0321 #endif /* #if !UCONFIG_NO_FORMATTING */
0322 
0323 #endif /* U_SHOW_CPLUSPLUS_API */
0324 
0325 #endif // __SIMPLENUMBERFORMATTERH__
0326