Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:08

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