ICU 63.1  63.1
numberformatter.h
Go to the documentation of this file.
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMBERFORMATTER_H__
8 #define __NUMBERFORMATTER_H__
9 
10 #include "unicode/appendable.h"
11 #include "unicode/dcfmtsym.h"
12 #include "unicode/currunit.h"
13 #include "unicode/fieldpos.h"
14 #include "unicode/fpositer.h"
15 #include "unicode/measunit.h"
16 #include "unicode/nounit.h"
17 #include "unicode/plurrule.h"
18 #include "unicode/ucurr.h"
19 #include "unicode/unum.h"
21 #include "unicode/uobject.h"
22 
23 #ifndef U_HIDE_DRAFT_API
24 
80 
81 // Forward declarations:
82 class IFixedDecimal;
83 class FieldPositionIteratorHandler;
84 
85 namespace numparse {
86 namespace impl {
87 
88 // Forward declarations:
89 class NumberParserImpl;
90 class MultiplierParseHandler;
91 
92 }
93 }
94 
95 namespace number { // icu::number
96 
97 // Forward declarations:
98 class UnlocalizedNumberFormatter;
99 class LocalizedNumberFormatter;
100 class FormattedNumber;
101 class Notation;
102 class ScientificNotation;
103 class Precision;
104 class FractionPrecision;
105 class CurrencyPrecision;
106 class IncrementPrecision;
107 class IntegerWidth;
108 
109 namespace impl {
110 
116 typedef int16_t digits_t;
117 
124 static constexpr int32_t DEFAULT_THRESHOLD = 3;
125 
126 // Forward declarations:
127 class Padder;
128 struct MacroProps;
129 struct MicroProps;
130 class DecimalQuantity;
131 struct UFormattedNumberData;
132 class NumberFormatterImpl;
133 struct ParsedPatternInfo;
134 class ScientificModifier;
135 class MultiplierProducer;
136 class RoundingImpl;
137 class ScientificHandler;
138 class Modifier;
139 class NumberStringBuilder;
140 class AffixPatternProvider;
141 class NumberPropertyMapper;
142 struct DecimalFormatProperties;
143 class MultiplierFormatHandler;
144 class CurrencySymbols;
145 class GeneratorHelpers;
146 class DecNum;
147 class NumberRangeFormatterImpl;
148 struct RangeMacroProps;
149 
157 
158 } // namespace impl
159 
166 
173 
179 class U_I18N_API Notation : public UMemory {
180  public:
205  static ScientificNotation scientific();
206 
229  static ScientificNotation engineering();
230 
272  static CompactNotation compactShort();
273 
296  static CompactNotation compactLong();
297 
322  static SimpleNotation simple();
323 
324  private:
325  enum NotationType {
326  NTN_SCIENTIFIC, NTN_COMPACT, NTN_SIMPLE, NTN_ERROR
327  } fType;
328 
329  union NotationUnion {
330  // For NTN_SCIENTIFIC
341  } scientific;
342 
343  // For NTN_COMPACT
344  UNumberCompactStyle compactStyle;
345 
346  // For NTN_ERROR
347  UErrorCode errorCode;
348  } fUnion;
349 
351 
352  Notation(const NotationType &type, const NotationUnion &union_) : fType(type), fUnion(union_) {}
353 
354  Notation(UErrorCode errorCode) : fType(NTN_ERROR) {
355  fUnion.errorCode = errorCode;
356  }
357 
358  Notation() : fType(NTN_SIMPLE), fUnion() {}
359 
360  UBool copyErrorTo(UErrorCode &status) const {
361  if (fType == NTN_ERROR) {
362  status = fUnion.errorCode;
363  return TRUE;
364  }
365  return FALSE;
366  }
367 
368  // To allow MacroProps to initialize empty instances:
369  friend struct impl::MacroProps;
370  friend class ScientificNotation;
371 
372  // To allow implementation to access internal types:
373  friend class impl::NumberFormatterImpl;
374  friend class impl::ScientificModifier;
375  friend class impl::ScientificHandler;
376 
377  // To allow access to the skeleton generation code:
378  friend class impl::GeneratorHelpers;
379 };
380 
390  public:
404  ScientificNotation withMinExponentDigits(int32_t minExponentDigits) const;
405 
419  ScientificNotation withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const;
420 
421  private:
422  // Inherit constructor
423  using Notation::Notation;
424 
425  // Raw constructor for NumberPropertyMapper
426  ScientificNotation(int8_t fEngineeringInterval, bool fRequireMinInt, impl::digits_t fMinExponentDigits,
427  UNumberSignDisplay fExponentSignDisplay);
428 
429  friend class Notation;
430 
431  // So that NumberPropertyMapper can create instances
432  friend class impl::NumberPropertyMapper;
433 };
434 
441 
442 // Typedefs for ICU 60/61 compatibility.
443 // These will be removed in ICU 64.
444 // See http://bugs.icu-project.org/trac/ticket/13746
445 
451 
457 
463 
469 
478 class U_I18N_API Precision : public UMemory {
479 
480  public:
497  static Precision unlimited();
498 
505  static FractionPrecision integer();
506 
534  static FractionPrecision fixedFraction(int32_t minMaxFractionPlaces);
535 
549  static FractionPrecision minFraction(int32_t minFractionPlaces);
550 
561  static FractionPrecision maxFraction(int32_t maxFractionPlaces);
562 
576  static FractionPrecision minMaxFraction(int32_t minFractionPlaces, int32_t maxFractionPlaces);
577 
591  static SignificantDigitsPrecision fixedSignificantDigits(int32_t minMaxSignificantDigits);
592 
605  static SignificantDigitsPrecision minSignificantDigits(int32_t minSignificantDigits);
606 
615  static SignificantDigitsPrecision maxSignificantDigits(int32_t maxSignificantDigits);
616 
628  static SignificantDigitsPrecision minMaxSignificantDigits(int32_t minSignificantDigits,
629  int32_t maxSignificantDigits);
630 
631 #ifndef U_HIDE_DEPRECATED_API
632  // Compatiblity methods that will be removed in ICU 64.
633  // See http://bugs.icu-project.org/trac/ticket/13746
634 
636  static inline SignificantDigitsPrecision fixedDigits(int32_t a) {
637  return fixedSignificantDigits(a);
638  }
639 
641  static inline SignificantDigitsPrecision minDigits(int32_t a) {
642  return minSignificantDigits(a);
643  }
644 
646  static inline SignificantDigitsPrecision maxDigits(int32_t a) {
647  return maxSignificantDigits(a);
648  }
649 
651  static inline SignificantDigitsPrecision minMaxDigits(int32_t a, int32_t b) {
652  return minMaxSignificantDigits(a, b);
653  }
654 #endif /* U_HIDE_DEPRECATED_API */
655 
675  static IncrementPrecision increment(double roundingIncrement);
676 
694  static CurrencyPrecision currency(UCurrencyUsage currencyUsage);
695 
696 #ifndef U_HIDE_DEPRECATED_API
697 
708  Precision withMode(UNumberFormatRoundingMode roundingMode) const;
709 #endif /* U_HIDE_DEPRECATED_API */
710 
711  private:
712  enum PrecisionType {
713  RND_BOGUS,
714  RND_NONE,
715  RND_FRACTION,
716  RND_SIGNIFICANT,
717  RND_FRACTION_SIGNIFICANT,
718  RND_INCREMENT,
719  RND_CURRENCY,
720  RND_ERROR
721  } fType;
722 
723  union PrecisionUnion {
726  // For RND_FRACTION, RND_SIGNIFICANT, and RND_FRACTION_SIGNIFICANT
735  } fracSig;
739  double fIncrement;
744  } increment; // For RND_INCREMENT
745  UCurrencyUsage currencyUsage; // For RND_CURRENCY
746  UErrorCode errorCode; // For RND_ERROR
747  } fUnion;
748 
751 
753  UNumberFormatRoundingMode fRoundingMode;
754 
755  Precision(const PrecisionType& type, const PrecisionUnion& union_,
756  UNumberFormatRoundingMode roundingMode)
757  : fType(type), fUnion(union_), fRoundingMode(roundingMode) {}
758 
759  Precision(UErrorCode errorCode) : fType(RND_ERROR) {
760  fUnion.errorCode = errorCode;
761  }
762 
763  Precision() : fType(RND_BOGUS) {}
764 
765  bool isBogus() const {
766  return fType == RND_BOGUS;
767  }
768 
769  UBool copyErrorTo(UErrorCode &status) const {
770  if (fType == RND_ERROR) {
771  status = fUnion.errorCode;
772  return TRUE;
773  }
774  return FALSE;
775  }
776 
777  // On the parent type so that this method can be called internally on Precision instances.
778  Precision withCurrency(const CurrencyUnit &currency, UErrorCode &status) const;
779 
780  static FractionPrecision constructFraction(int32_t minFrac, int32_t maxFrac);
781 
782  static Precision constructSignificant(int32_t minSig, int32_t maxSig);
783 
784  static Precision
785  constructFractionSignificant(const FractionPrecision &base, int32_t minSig, int32_t maxSig);
786 
787  static IncrementPrecision constructIncrement(double increment, int32_t minFrac);
788 
789  static CurrencyPrecision constructCurrency(UCurrencyUsage usage);
790 
791  static Precision constructPassThrough();
792 
793  // To allow MacroProps/MicroProps to initialize bogus instances:
794  friend struct impl::MacroProps;
795  friend struct impl::MicroProps;
796 
797  // To allow NumberFormatterImpl to access isBogus() and other internal methods:
798  friend class impl::NumberFormatterImpl;
799 
800  // To allow NumberPropertyMapper to create instances from DecimalFormatProperties:
801  friend class impl::NumberPropertyMapper;
802 
803  // To allow access to the main implementation class:
804  friend class impl::RoundingImpl;
805 
806  // To allow child classes to call private methods:
807  friend class FractionPrecision;
808  friend class CurrencyPrecision;
809  friend class IncrementPrecision;
810 
811  // To allow access to the skeleton generation code:
812  friend class impl::GeneratorHelpers;
813 };
814 
825  public:
842  Precision withMinDigits(int32_t minSignificantDigits) const;
843 
861  Precision withMaxDigits(int32_t maxSignificantDigits) const;
862 
863  private:
864  // Inherit constructor
865  using Precision::Precision;
866 
867  // To allow parent class to call this class's constructor:
868  friend class Precision;
869 };
870 
881  public:
899  Precision withCurrency(const CurrencyUnit &currency) const;
900 
901  private:
902  // Inherit constructor
903  using Precision::Precision;
904 
905  // To allow parent class to call this class's constructor:
906  friend class Precision;
907 };
908 
919  public:
935  Precision withMinFraction(int32_t minFrac) const;
936 
937  private:
938  // Inherit constructor
939  using Precision::Precision;
940 
941  // To allow parent class to call this class's constructor:
942  friend class Precision;
943 };
944 
955  public:
967  static IntegerWidth zeroFillTo(int32_t minInt);
968 
980  IntegerWidth truncateAt(int32_t maxInt);
981 
982  private:
983  union {
984  struct {
985  impl::digits_t fMinInt;
986  impl::digits_t fMaxInt;
987  bool fFormatFailIfMoreThanMaxDigits;
988  } minMaxInt;
989  UErrorCode errorCode;
990  } fUnion;
991  bool fHasError = false;
992 
993  IntegerWidth(impl::digits_t minInt, impl::digits_t maxInt, bool formatFailIfMoreThanMaxDigits);
994 
995  IntegerWidth(UErrorCode errorCode) { // NOLINT
996  fUnion.errorCode = errorCode;
997  fHasError = true;
998  }
999 
1000  IntegerWidth() { // NOLINT
1001  fUnion.minMaxInt.fMinInt = -1;
1002  }
1003 
1005  static IntegerWidth standard() {
1006  return IntegerWidth::zeroFillTo(1);
1007  }
1008 
1009  bool isBogus() const {
1010  return !fHasError && fUnion.minMaxInt.fMinInt == -1;
1011  }
1012 
1013  UBool copyErrorTo(UErrorCode &status) const {
1014  if (fHasError) {
1015  status = fUnion.errorCode;
1016  return TRUE;
1017  }
1018  return FALSE;
1019  }
1020 
1021  void apply(impl::DecimalQuantity &quantity, UErrorCode &status) const;
1022 
1023  bool operator==(const IntegerWidth& other) const;
1024 
1025  // To allow MacroProps/MicroProps to initialize empty instances:
1026  friend struct impl::MacroProps;
1027  friend struct impl::MicroProps;
1028 
1029  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1030  friend class impl::NumberFormatterImpl;
1031 
1032  // So that NumberPropertyMapper can create instances
1033  friend class impl::NumberPropertyMapper;
1034 
1035  // To allow access to the skeleton generation code:
1036  friend class impl::GeneratorHelpers;
1037 };
1038 
1047 class U_I18N_API Scale : public UMemory {
1048  public:
1055  static Scale none();
1056 
1067  static Scale powerOfTen(int32_t power);
1068 
1081  static Scale byDecimal(StringPiece multiplicand);
1082 
1091  static Scale byDouble(double multiplicand);
1092 
1099  static Scale byDoubleAndPowerOfTen(double multiplicand, int32_t power);
1100 
1101  // We need a custom destructor for the DecNum, which means we need to declare
1102  // the copy/move constructor/assignment quartet.
1103 
1105  Scale(const Scale& other);
1106 
1108  Scale& operator=(const Scale& other);
1109 
1111  Scale(Scale&& src) U_NOEXCEPT;
1112 
1114  Scale& operator=(Scale&& src) U_NOEXCEPT;
1115 
1117  ~Scale();
1118 
1119 #ifndef U_HIDE_INTERNAL_API
1120 
1121  Scale(int32_t magnitude, impl::DecNum* arbitraryToAdopt);
1122 #endif /* U_HIDE_INTERNAL_API */
1123 
1124  private:
1125  int32_t fMagnitude;
1126  impl::DecNum* fArbitrary;
1127  UErrorCode fError;
1128 
1129  Scale(UErrorCode error) : fMagnitude(0), fArbitrary(nullptr), fError(error) {}
1130 
1131  Scale() : fMagnitude(0), fArbitrary(nullptr), fError(U_ZERO_ERROR) {}
1132 
1133  bool isValid() const {
1134  return fMagnitude != 0 || fArbitrary != nullptr;
1135  }
1136 
1137  UBool copyErrorTo(UErrorCode &status) const {
1138  if (fError != U_ZERO_ERROR) {
1139  status = fError;
1140  return TRUE;
1141  }
1142  return FALSE;
1143  }
1144 
1145  void applyTo(impl::DecimalQuantity& quantity) const;
1146 
1147  void applyReciprocalTo(impl::DecimalQuantity& quantity) const;
1148 
1149  // To allow MacroProps/MicroProps to initialize empty instances:
1150  friend struct impl::MacroProps;
1151  friend struct impl::MicroProps;
1152 
1153  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1154  friend class impl::NumberFormatterImpl;
1155 
1156  // To allow the helper class MultiplierFormatHandler access to private fields:
1157  friend class impl::MultiplierFormatHandler;
1158 
1159  // To allow access to the skeleton generation code:
1160  friend class impl::GeneratorHelpers;
1161 
1162  // To allow access to parsing code:
1163  friend class ::icu::numparse::impl::NumberParserImpl;
1164  friend class ::icu::numparse::impl::MultiplierParseHandler;
1165 };
1166 
1167 namespace impl {
1168 
1169 // Do not enclose entire SymbolsWrapper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1172  public:
1174  SymbolsWrapper() : fType(SYMPTR_NONE), fPtr{nullptr} {}
1175 
1177  SymbolsWrapper(const SymbolsWrapper &other);
1178 
1180  SymbolsWrapper &operator=(const SymbolsWrapper &other);
1181 
1184 
1186  SymbolsWrapper &operator=(SymbolsWrapper&& src) U_NOEXCEPT;
1187 
1189  ~SymbolsWrapper();
1190 
1191 #ifndef U_HIDE_INTERNAL_API
1192 
1197  void setTo(const DecimalFormatSymbols &dfs);
1198 
1203  void setTo(const NumberingSystem *ns);
1204 
1209  bool isDecimalFormatSymbols() const;
1210 
1215  bool isNumberingSystem() const;
1216 
1221  const DecimalFormatSymbols *getDecimalFormatSymbols() const;
1222 
1227  const NumberingSystem *getNumberingSystem() const;
1228 
1229 #endif // U_HIDE_INTERNAL_API
1230 
1232  UBool copyErrorTo(UErrorCode &status) const {
1233  if (fType == SYMPTR_DFS && fPtr.dfs == nullptr) {
1234  status = U_MEMORY_ALLOCATION_ERROR;
1235  return TRUE;
1236  } else if (fType == SYMPTR_NS && fPtr.ns == nullptr) {
1237  status = U_MEMORY_ALLOCATION_ERROR;
1238  return TRUE;
1239  }
1240  return FALSE;
1241  }
1242 
1243  private:
1244  enum SymbolsPointerType {
1245  SYMPTR_NONE, SYMPTR_DFS, SYMPTR_NS
1246  } fType;
1247 
1248  union {
1249  const DecimalFormatSymbols *dfs;
1250  const NumberingSystem *ns;
1251  } fPtr;
1252 
1253  void doCopyFrom(const SymbolsWrapper &other);
1254 
1255  void doMoveFrom(SymbolsWrapper&& src);
1256 
1257  void doCleanup();
1258 };
1259 
1260 // Do not enclose entire Grouper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1262 class U_I18N_API Grouper : public UMemory {
1263  public:
1264 #ifndef U_HIDE_INTERNAL_API
1265 
1266  static Grouper forStrategy(UNumberGroupingStrategy grouping);
1267 
1272  static Grouper forProperties(const DecimalFormatProperties& properties);
1273 
1274  // Future: static Grouper forProperties(DecimalFormatProperties& properties);
1275 
1277  Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UNumberGroupingStrategy strategy)
1278  : fGrouping1(grouping1),
1279  fGrouping2(grouping2),
1280  fMinGrouping(minGrouping),
1281  fStrategy(strategy) {}
1282 #endif // U_HIDE_INTERNAL_API
1283 
1285  int16_t getPrimary() const;
1286 
1288  int16_t getSecondary() const;
1289 
1290  private:
1299  int16_t fGrouping1;
1300  int16_t fGrouping2;
1301 
1309  int16_t fMinGrouping;
1310 
1315  UNumberGroupingStrategy fStrategy;
1316 
1317  Grouper() : fGrouping1(-3) {};
1318 
1319  bool isBogus() const {
1320  return fGrouping1 == -3;
1321  }
1322 
1324  void setLocaleData(const impl::ParsedPatternInfo &patternInfo, const Locale& locale);
1325 
1326  bool groupAtPosition(int32_t position, const impl::DecimalQuantity &value) const;
1327 
1328  // To allow MacroProps/MicroProps to initialize empty instances:
1329  friend struct MacroProps;
1330  friend struct MicroProps;
1331 
1332  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1333  friend class NumberFormatterImpl;
1334 
1335  // To allow NumberParserImpl to perform setLocaleData():
1336  friend class ::icu::numparse::impl::NumberParserImpl;
1337 
1338  // To allow access to the skeleton generation code:
1339  friend class impl::GeneratorHelpers;
1340 };
1341 
1342 // Do not enclose entire Padder with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1344 class U_I18N_API Padder : public UMemory {
1345  public:
1346 #ifndef U_HIDE_INTERNAL_API
1347 
1348  static Padder none();
1349 
1351  static Padder codePoints(UChar32 cp, int32_t targetWidth, UNumberFormatPadPosition position);
1352 #endif // U_HIDE_INTERNAL_API
1353 
1355  static Padder forProperties(const DecimalFormatProperties& properties);
1356 
1357  private:
1358  UChar32 fWidth; // -3 = error; -2 = bogus; -1 = no padding
1359  union {
1360  struct {
1361  int32_t fCp;
1362  UNumberFormatPadPosition fPosition;
1363  } padding;
1364  UErrorCode errorCode;
1365  } fUnion;
1366 
1367  Padder(UChar32 cp, int32_t width, UNumberFormatPadPosition position);
1368 
1369  Padder(int32_t width);
1370 
1371  Padder(UErrorCode errorCode) : fWidth(-3) { // NOLINT
1372  fUnion.errorCode = errorCode;
1373  }
1374 
1375  Padder() : fWidth(-2) {} // NOLINT
1376 
1377  bool isBogus() const {
1378  return fWidth == -2;
1379  }
1380 
1381  UBool copyErrorTo(UErrorCode &status) const {
1382  if (fWidth == -3) {
1383  status = fUnion.errorCode;
1384  return TRUE;
1385  }
1386  return FALSE;
1387  }
1388 
1389  bool isValid() const {
1390  return fWidth > 0;
1391  }
1392 
1393  int32_t padAndApply(const impl::Modifier &mod1, const impl::Modifier &mod2,
1394  impl::NumberStringBuilder &string, int32_t leftIndex, int32_t rightIndex,
1395  UErrorCode &status) const;
1396 
1397  // To allow MacroProps/MicroProps to initialize empty instances:
1398  friend struct MacroProps;
1399  friend struct MicroProps;
1400 
1401  // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1402  friend class impl::NumberFormatterImpl;
1403 
1404  // To allow access to the skeleton generation code:
1405  friend class impl::GeneratorHelpers;
1406 };
1407 
1408 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1410 struct U_I18N_API MacroProps : public UMemory {
1413 
1415  MeasureUnit unit; // = NoUnit::base();
1416 
1418  MeasureUnit perUnit; // = NoUnit::base();
1419 
1421  Precision precision; // = Precision(); (bogus)
1422 
1425 
1427  Grouper grouper; // = Grouper(); (bogus)
1428 
1430  Padder padder; // = Padder(); (bogus)
1431 
1433  IntegerWidth integerWidth; // = IntegerWidth(); (bogus)
1434 
1437 
1438  // UNUM_XYZ_COUNT denotes null (bogus) values.
1439 
1442 
1445 
1448 
1450  Scale scale; // = Scale(); (benign value)
1451 
1453  const AffixPatternProvider* affixProvider = nullptr; // no ownership
1454 
1456  const PluralRules* rules = nullptr; // no ownership
1457 
1459  const CurrencySymbols* currencySymbols = nullptr; // no ownership
1460 
1462  int32_t threshold = DEFAULT_THRESHOLD;
1463 
1466 
1467  // NOTE: Uses default copy and move constructors.
1468 
1473  bool copyErrorTo(UErrorCode &status) const {
1474  return notation.copyErrorTo(status) || precision.copyErrorTo(status) ||
1475  padder.copyErrorTo(status) || integerWidth.copyErrorTo(status) ||
1476  symbols.copyErrorTo(status) || scale.copyErrorTo(status);
1477  }
1478 };
1479 
1480 } // namespace impl
1481 
1487 template<typename Derived>
1489  public:
1518  Derived notation(const Notation &notation) const &;
1519 
1529  Derived notation(const Notation &notation) &&;
1530 
1575  Derived unit(const icu::MeasureUnit &unit) const &;
1576 
1586  Derived unit(const icu::MeasureUnit &unit) &&;
1587 
1603  Derived adoptUnit(icu::MeasureUnit *unit) const &;
1604 
1614  Derived adoptUnit(icu::MeasureUnit *unit) &&;
1615 
1633  Derived perUnit(const icu::MeasureUnit &perUnit) const &;
1634 
1644  Derived perUnit(const icu::MeasureUnit &perUnit) &&;
1645 
1663  Derived adoptPerUnit(icu::MeasureUnit *perUnit) const &;
1664 
1674  Derived adoptPerUnit(icu::MeasureUnit *perUnit) &&;
1675 
1706  Derived precision(const Precision& precision) const &;
1707 
1717  Derived precision(const Precision& precision) &&;
1718 
1719 #ifndef U_HIDE_DEPRECATED_API
1720  // Compatibility method that will be removed in ICU 64.
1721  // Use precision() instead.
1722  // See http://bugs.icu-project.org/trac/ticket/13746
1724  Derived rounding(const Rounder& rounder) const & {
1725  return precision(rounder);
1726  }
1727 #endif /* U_HIDE_DEPRECATED_API */
1728 
1747  Derived roundingMode(UNumberFormatRoundingMode roundingMode) const &;
1748 
1757  Derived roundingMode(UNumberFormatRoundingMode roundingMode) &&;
1758 
1786  Derived grouping(UNumberGroupingStrategy strategy) const &;
1787 
1797  Derived grouping(UNumberGroupingStrategy strategy) &&;
1798 
1823  Derived integerWidth(const IntegerWidth &style) const &;
1824 
1834  Derived integerWidth(const IntegerWidth &style) &&;
1835 
1876  Derived symbols(const DecimalFormatSymbols &symbols) const &;
1877 
1887  Derived symbols(const DecimalFormatSymbols &symbols) &&;
1888 
1922  Derived adoptSymbols(NumberingSystem *symbols) const &;
1923 
1933  Derived adoptSymbols(NumberingSystem *symbols) &&;
1934 
1960  Derived unitWidth(UNumberUnitWidth width) const &;
1961 
1971  Derived unitWidth(UNumberUnitWidth width) &&;
1972 
1998  Derived sign(UNumberSignDisplay style) const &;
1999 
2009  Derived sign(UNumberSignDisplay style) &&;
2010 
2036  Derived decimal(UNumberDecimalSeparatorDisplay style) const &;
2037 
2047  Derived decimal(UNumberDecimalSeparatorDisplay style) &&;
2048 
2073  Derived scale(const Scale &scale) const &;
2074 
2084  Derived scale(const Scale &scale) &&;
2085 
2086 #ifndef U_HIDE_INTERNAL_API
2087 
2093  Derived padding(const impl::Padder &padder) const &;
2094 
2096  Derived padding(const impl::Padder &padder) &&;
2097 
2104  Derived threshold(int32_t threshold) const &;
2105 
2107  Derived threshold(int32_t threshold) &&;
2108 
2114  Derived macros(const impl::MacroProps& macros) const &;
2115 
2117  Derived macros(const impl::MacroProps& macros) &&;
2118 
2120  Derived macros(impl::MacroProps&& macros) const &;
2121 
2123  Derived macros(impl::MacroProps&& macros) &&;
2124 
2125 #endif /* U_HIDE_INTERNAL_API */
2126 
2141  UnicodeString toSkeleton(UErrorCode& status) const;
2142 
2149  UBool copyErrorTo(UErrorCode &outErrorCode) const {
2150  if (U_FAILURE(outErrorCode)) {
2151  // Do not overwrite the older error code
2152  return TRUE;
2153  }
2154  fMacros.copyErrorTo(outErrorCode);
2155  return U_FAILURE(outErrorCode);
2156  };
2157 
2158  // NOTE: Uses default copy and move constructors.
2159 
2160  private:
2161  impl::MacroProps fMacros;
2162 
2163  // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
2164  NumberFormatterSettings() = default;
2165 
2166  friend class LocalizedNumberFormatter;
2167  friend class UnlocalizedNumberFormatter;
2168 
2169  // Give NumberRangeFormatter access to the MacroProps
2170  friend void impl::touchRangeLocales(impl::RangeMacroProps& macros);
2171  friend class impl::NumberRangeFormatterImpl;
2172 };
2173 
2181  : public NumberFormatterSettings<UnlocalizedNumberFormatter>, public UMemory {
2182 
2183  public:
2193  LocalizedNumberFormatter locale(const icu::Locale &locale) const &;
2194 
2204  LocalizedNumberFormatter locale(const icu::Locale &locale) &&;
2205 
2211  UnlocalizedNumberFormatter() = default;
2212 
2218 
2225 
2230  UnlocalizedNumberFormatter& operator=(const UnlocalizedNumberFormatter& other);
2231 
2238 
2239  private:
2241 
2242  explicit UnlocalizedNumberFormatter(
2244 
2245  // To give the fluent setters access to this class's constructor:
2246  friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2247 
2248  // To give NumberFormatter::with() access to this class's constructor:
2249  friend class NumberFormatter;
2250 };
2251 
2259  : public NumberFormatterSettings<LocalizedNumberFormatter>, public UMemory {
2260  public:
2272  FormattedNumber formatInt(int64_t value, UErrorCode &status) const;
2273 
2285  FormattedNumber formatDouble(double value, UErrorCode &status) const;
2286 
2301  FormattedNumber formatDecimal(StringPiece value, UErrorCode& status) const;
2302 
2303 #ifndef U_HIDE_INTERNAL_API
2304 
2308  FormattedNumber formatDecimalQuantity(const impl::DecimalQuantity& dq, UErrorCode& status) const;
2309 
2313  void getAffixImpl(bool isPrefix, bool isNegative, UnicodeString& result, UErrorCode& status) const;
2314 
2319  const impl::NumberFormatterImpl* getCompiled() const;
2320 
2325  int32_t getCallCount() const;
2326 
2327 #endif /* U_HIDE_INTERNAL_API */
2328 
2342  Format* toFormat(UErrorCode& status) const;
2343 
2349  LocalizedNumberFormatter() = default;
2350 
2356 
2363 
2368  LocalizedNumberFormatter& operator=(const LocalizedNumberFormatter& other);
2369 
2376 
2377 #ifndef U_HIDE_INTERNAL_API
2378 
2391  void formatImpl(impl::UFormattedNumberData *results, UErrorCode &status) const;
2392 
2393 #endif /* U_HIDE_INTERNAL_API */
2394 
2400 
2401  private:
2402  // Note: fCompiled can't be a LocalPointer because impl::NumberFormatterImpl is defined in an internal
2403  // header, and LocalPointer needs the full class definition in order to delete the instance.
2404  const impl::NumberFormatterImpl* fCompiled {nullptr};
2405  char fUnsafeCallCount[8] {}; // internally cast to u_atomic_int32_t
2406 
2408 
2410 
2411  LocalizedNumberFormatter(const impl::MacroProps &macros, const Locale &locale);
2412 
2413  LocalizedNumberFormatter(impl::MacroProps &&macros, const Locale &locale);
2414 
2415  void clear();
2416 
2417  void lnfMoveHelper(LocalizedNumberFormatter&& src);
2418 
2422  bool computeCompiled(UErrorCode& status) const;
2423 
2424  // To give the fluent setters access to this class's constructor:
2425  friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2426  friend class NumberFormatterSettings<LocalizedNumberFormatter>;
2427 
2428  // To give UnlocalizedNumberFormatter::locale() access to this class's constructor:
2429  friend class UnlocalizedNumberFormatter;
2430 };
2431 
2439  public:
2440 #ifndef U_HIDE_DEPRECATED_API
2441 
2449  UnicodeString toString() const;
2450 #endif /* U_HIDE_DEPRECATED_API */
2451 
2460  UnicodeString toString(UErrorCode& status) const;
2461 
2462 #ifndef U_HIDE_DEPRECATED_API
2463 
2474  Appendable &appendTo(Appendable &appendable);
2475 #endif /* U_HIDE_DEPRECATED_API */
2476 
2488  Appendable &appendTo(Appendable &appendable, UErrorCode& status) const;
2489 
2490 #ifndef U_HIDE_DEPRECATED_API
2491 
2511  void populateFieldPosition(FieldPosition &fieldPosition, UErrorCode &status);
2512 #endif /* U_HIDE_DEPRECATED_API */
2513 
2545  UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
2546 
2547 #ifndef U_HIDE_DEPRECATED_API
2548 
2563  void populateFieldPositionIterator(FieldPositionIterator &iterator, UErrorCode &status);
2564 #endif /* U_HIDE_DEPRECATED_API */
2565 
2579  void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
2580 
2581 #ifndef U_HIDE_INTERNAL_API
2582 
2587  void getDecimalQuantity(impl::DecimalQuantity& output, UErrorCode& status) const;
2588 
2593  void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
2594 
2595 #endif /* U_HIDE_INTERNAL_API */
2596 
2600  FormattedNumber(const FormattedNumber&) = delete;
2601 
2605  FormattedNumber& operator=(const FormattedNumber&) = delete;
2606 
2613 
2619  FormattedNumber& operator=(FormattedNumber&& src) U_NOEXCEPT;
2620 
2625  ~FormattedNumber();
2626 
2627  private:
2628  // Can't use LocalPointer because UFormattedNumberData is forward-declared
2629  const impl::UFormattedNumberData *fResults;
2630 
2631  // Error code for the terminal methods
2632  UErrorCode fErrorCode;
2633 
2638  explicit FormattedNumber(impl::UFormattedNumberData *results)
2639  : fResults(results), fErrorCode(U_ZERO_ERROR) {};
2640 
2641  explicit FormattedNumber(UErrorCode errorCode)
2642  : fResults(nullptr), fErrorCode(errorCode) {};
2643 
2644  // To give LocalizedNumberFormatter format methods access to this class's constructor:
2645  friend class LocalizedNumberFormatter;
2646 };
2647 
2654  public:
2662  static UnlocalizedNumberFormatter with();
2663 
2673  static LocalizedNumberFormatter withLocale(const Locale &locale);
2674 
2686  static UnlocalizedNumberFormatter forSkeleton(const UnicodeString& skeleton, UErrorCode& status);
2687 
2691  NumberFormatter() = delete;
2692 };
2693 
2694 } // namespace number
2696 
2697 #endif // U_HIDE_DRAFT_API
2698 
2699 #endif // __NUMBERFORMATTER_H__
2700 
2701 #endif /* #if !UCONFIG_NO_FORMATTING */
One more than the highest UNumberSignDisplay value.
Base class for all formats.
Definition: format.h:96
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:84
UNumberFormatRoundingMode
The possible number format rounding modes.
Definition: unum.h:279
static SignificantDigitsPrecision minDigits(int32_t a)
A unit such as length, mass, volume, currency, etc.
Definition: measunit.h:38
C++ API: Currency Unit Information.
C-compatible API for localized number formatting; not recommended for C++.
static SignificantDigitsPrecision fixedDigits(int32_t a)
See the main description in numberformatter.h for documentation and examples.
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:699
A class that defines the strategy for padding and truncating integers before the decimal separator...
C++ API: FieldPosition Iterator.
static SignificantDigitsPrecision minMaxDigits(int32_t a, int32_t b)
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
Defines numbering systems.
Definition: numsys.h:60
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
C++ API: units for percent and permille.
static SignificantDigitsPrecision maxDigits(int32_t a)
No error, no warning.
Definition: utypes.h:435
C++ API: PluralRules object.
A class that defines a rounding precision parameterized by a rounding increment to be used when forma...
UNumberDecimalSeparatorDisplay
An enum declaring how to render the decimal separator.
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:194
Memory allocation error.
Definition: utypes.h:443
An abstract base class for specifying settings related to number formatting.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
Half-even rounding.
Definition: unum.h:288
C++ API: FieldPosition identifies the fields in a formatted output.
A class that defines the notation style to be used when formatting numbers in NumberFormatter.
void touchRangeLocales(impl::RangeMacroProps &macros)
Used for NumberRangeFormatter and implemented in numrange_fluent.cpp.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
C++ API: A unit for measuring a quantity.
C API: Encapsulates information about a currency.
UNumberSignDisplay
An enum declaring how to denote positive and negative numbers.
A class that defines a rounding precision based on a number of fraction places and optionally signifi...
UNumberGroupingStrategy
An enum declaring the strategy for when and how to display grouping separators (i.e., the separator, often a comma or period, after every 2-3 powers of ten).
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition: ucurr.h:41
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:389
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
A class that defines a rounding precision parameterized by a currency to be used when formatting numb...
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:229
A unit of currency, such as USD (U.S.
Definition: currunit.h:37
UNumberFormatPadPosition
The possible number format pad positions.
Definition: unum.h:308
C++ API: Common ICU base class UObject.
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:503
A NumberFormatter that does not yet have a locale.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UNumberCompactStyle
Constants for specifying short or long format.
Definition: unum.h:319
A NumberFormatter that has a locale associated with it; this means .format() methods are available...
Derived rounding(const Rounder &rounder) const &
A class that defines a quantity by which a number should be multiplied when formatting.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
C++ API: Symbols for formatting numbers.
A class that defines the scientific notation style to be used when formatting numbers in NumberFormat...
UNumberUnitWidth
An enum declaring how to render units, including currencies.
Basic definitions for ICU, for both C and C++ APIs.
#define FALSE
The FALSE value of a UBool.
Definition: umachine.h:233
int16_t digits_t
Datatype for minimum/maximum fraction digits.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:289
The result of a number formatting operation.
Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UNumberGroupingStrategy strategy)
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
UMemory is the common ICU base class.
Definition: uobject.h:109
A class that defines the rounding precision to be used when formatting numbers in NumberFormatter...
One more than the highest UNumberDecimalSeparatorDisplay value.
One more than the highest UNumberUnitWidth value.
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:51
C API: Compatibility APIs for number formatting.
UBool copyErrorTo(UErrorCode &status) const
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192