ICU 73.2  73.2
rbnf.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 1997-2015, International Business Machines Corporation and others.
6 * All Rights Reserved.
7 *******************************************************************************
8 */
9 
10 #ifndef RBNF_H
11 #define RBNF_H
12 
13 #include "unicode/utypes.h"
14 
15 #if U_SHOW_CPLUSPLUS_API
16 
29 #if UCONFIG_NO_FORMATTING
30 #define U_HAVE_RBNF 0
31 #else
32 #define U_HAVE_RBNF 1
33 
34 #include "unicode/dcfmtsym.h"
35 #include "unicode/fmtable.h"
36 #include "unicode/locid.h"
37 #include "unicode/numfmt.h"
38 #include "unicode/unistr.h"
39 #include "unicode/strenum.h"
40 #include "unicode/brkiter.h"
41 #include "unicode/upluralrules.h"
42 
43 U_NAMESPACE_BEGIN
44 
45 class NFRule;
46 class NFRuleSet;
47 class LocalizationInfo;
48 class PluralFormat;
49 class RuleBasedCollator;
50 
79 #ifndef U_HIDE_DEPRECATED_API
85 #endif // U_HIDE_DEPRECATED_API
86 };
87 
581 public:
582 
583  //-----------------------------------------------------------------------
584  // constructors
585  //-----------------------------------------------------------------------
586 
598 
622  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
623  UParseError& perror, UErrorCode& status);
624 
640  RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale,
641  UParseError& perror, UErrorCode& status);
642 
669  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
670  const Locale& locale, UParseError& perror, UErrorCode& status);
671 
691 
692  //-----------------------------------------------------------------------
693  // boilerplate
694  //-----------------------------------------------------------------------
695 
702 
709 
715 
722  virtual RuleBasedNumberFormat* clone() const override;
723 
731  virtual bool operator==(const Format& other) const override;
732 
733 //-----------------------------------------------------------------------
734 // public API functions
735 //-----------------------------------------------------------------------
736 
742  virtual UnicodeString getRules() const;
743 
749  virtual int32_t getNumberOfRuleSetNames() const;
750 
758  virtual UnicodeString getRuleSetName(int32_t index) const;
759 
765  virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const;
766 
775  virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const;
776 
790  virtual UnicodeString getRuleSetDisplayName(int32_t index,
791  const Locale& locale = Locale::getDefault());
792 
802  const Locale& locale = Locale::getDefault());
803 
804 
805  using NumberFormat::format;
806 
815  virtual UnicodeString& format(int32_t number,
816  UnicodeString& toAppendTo,
817  FieldPosition& pos) const override;
818 
827  virtual UnicodeString& format(int64_t number,
828  UnicodeString& toAppendTo,
829  FieldPosition& pos) const override;
838  virtual UnicodeString& format(double number,
839  UnicodeString& toAppendTo,
840  FieldPosition& pos) const override;
841 
853  virtual UnicodeString& format(int32_t number,
854  const UnicodeString& ruleSetName,
855  UnicodeString& toAppendTo,
856  FieldPosition& pos,
857  UErrorCode& status) const;
869  virtual UnicodeString& format(int64_t number,
870  const UnicodeString& ruleSetName,
871  UnicodeString& toAppendTo,
872  FieldPosition& pos,
873  UErrorCode& status) const;
885  virtual UnicodeString& format(double number,
886  const UnicodeString& ruleSetName,
887  UnicodeString& toAppendTo,
888  FieldPosition& pos,
889  UErrorCode& status) const;
890 
891 protected:
909  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
910  UnicodeString& appendTo,
911  FieldPosition& pos,
912  UErrorCode& status) const override;
913 public:
914 
915  using NumberFormat::parse;
916 
931  virtual void parse(const UnicodeString& text,
932  Formattable& result,
933  ParsePosition& parsePosition) const override;
934 
935 #if !UCONFIG_NO_COLLATION
936 
970  virtual void setLenient(UBool enabled) override;
971 
979  virtual inline UBool isLenient(void) const override;
980 
981 #endif
982 
991  virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
992 
1000 
1011  virtual void setContext(UDisplayContext value, UErrorCode& status) override;
1012 
1018  virtual ERoundingMode getRoundingMode(void) const override;
1019 
1025  virtual void setRoundingMode(ERoundingMode roundingMode) override;
1026 
1027 public:
1033  static UClassID U_EXPORT2 getStaticClassID(void);
1034 
1040  virtual UClassID getDynamicClassID(void) const override;
1041 
1050  virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
1051 
1061  virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1062 
1063 private:
1064  RuleBasedNumberFormat() = delete; // default constructor not implemented
1065 
1066  // this will ref the localizations if they are not nullptr
1067  // caller must deref to get adoption
1068  RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
1069  const Locale& locale, UParseError& perror, UErrorCode& status);
1070 
1071  void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status);
1072  void initCapitalizationContextInfo(const Locale& thelocale);
1073  void dispose();
1074  void stripWhitespace(UnicodeString& src);
1075  void initDefaultRuleSet();
1076  NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
1077 
1078  /* friend access */
1079  friend class NFSubstitution;
1080  friend class NFRule;
1081  friend class NFRuleSet;
1082  friend class FractionalPartSubstitution;
1083 
1084  inline NFRuleSet * getDefaultRuleSet() const;
1085  const RuleBasedCollator * getCollator() const;
1086  DecimalFormatSymbols * initializeDecimalFormatSymbols(UErrorCode &status);
1087  const DecimalFormatSymbols * getDecimalFormatSymbols() const;
1088  NFRule * initializeDefaultInfinityRule(UErrorCode &status);
1089  const NFRule * getDefaultInfinityRule() const;
1090  NFRule * initializeDefaultNaNRule(UErrorCode &status);
1091  const NFRule * getDefaultNaNRule() const;
1092  PluralFormat *createPluralFormat(UPluralType pluralType, const UnicodeString &pattern, UErrorCode& status) const;
1093  UnicodeString& adjustForCapitalizationContext(int32_t startPos, UnicodeString& currentResult, UErrorCode& status) const;
1094  UnicodeString& format(int64_t number, NFRuleSet *ruleSet, UnicodeString& toAppendTo, UErrorCode& status) const;
1095  void format(double number, NFRuleSet& rs, UnicodeString& toAppendTo, UErrorCode& status) const;
1096 
1097 private:
1098  NFRuleSet **fRuleSets;
1099  UnicodeString* ruleSetDescriptions;
1100  int32_t numRuleSets;
1101  NFRuleSet *defaultRuleSet;
1102  Locale locale;
1103  RuleBasedCollator* collator;
1104  DecimalFormatSymbols* decimalFormatSymbols;
1105  NFRule *defaultInfinityRule;
1106  NFRule *defaultNaNRule;
1107  ERoundingMode fRoundingMode;
1108  UBool lenient;
1109  UnicodeString* lenientParseRules;
1110  LocalizationInfo* localizations;
1111  UnicodeString originalDescription;
1112  UBool capitalizationInfoSet;
1113  UBool capitalizationForUIListMenu;
1114  UBool capitalizationForStandAlone;
1115  BreakIterator* capitalizationBrkIter;
1116 };
1117 
1118 // ---------------
1119 
1120 #if !UCONFIG_NO_COLLATION
1121 
1122 inline UBool
1123 RuleBasedNumberFormat::isLenient(void) const {
1124  return lenient;
1125 }
1126 
1127 #endif
1128 
1129 inline NFRuleSet*
1130 RuleBasedNumberFormat::getDefaultRuleSet() const {
1131  return defaultRuleSet;
1132 }
1133 
1134 U_NAMESPACE_END
1135 
1136 /* U_HAVE_RBNF */
1137 #endif
1138 
1139 #endif /* U_SHOW_CPLUSPLUS_API */
1140 
1141 /* RBNF_H */
1142 #endif
C++ API: Break Iterator.
The BreakIterator class implements methods for finding the location of boundaries in text.
Definition: brkiter.h:106
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:86
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
Base class for all formats.
Definition: format.h:98
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1276
ERoundingMode
Rounding mode.
Definition: numfmt.h:186
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
Return a long if possible (e.g.
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
The RuleBasedCollator class provides the implementation of Collator, using data-driven tables.
Definition: tblcoll.h:115
The RuleBasedNumberFormat class formats numbers according to a set of rules.
Definition: rbnf.h:580
virtual void setLenient(UBool enabled) override
Turns lenient parse mode on and off.
RuleBasedNumberFormat & operator=(const RuleBasedNumberFormat &rhs)
Assignment operator.
virtual UnicodeString & format(double number, UnicodeString &toAppendTo, FieldPosition &pos) const override
Formats the specified number using the default ruleset.
virtual UnicodeString getRuleSetName(int32_t index) const
Return the name of the index'th public ruleSet.
RuleBasedNumberFormat(const UnicodeString &rules, const UnicodeString &localizations, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the description passed in.
virtual UnicodeString & format(int32_t number, UnicodeString &toAppendTo, FieldPosition &pos) const override
Formats the specified 32-bit number using the default ruleset.
virtual UnicodeString getRules() const
return the rules that were provided to the RuleBasedNumberFormat.
RuleBasedNumberFormat(const UnicodeString &rules, const Locale &locale, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the rules passed in.
virtual int32_t getNumberOfRuleSetNames() const
Return the number of public rule set names.
virtual ERoundingMode getRoundingMode(void) const override
Get the rounding mode.
virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const
Return the number of locales for which we have localized rule set display names.
virtual UClassID getDynamicClassID(void) const override
ICU "poor man's RTTI", returns a UClassID for the actual class.
virtual UnicodeString & format(int64_t number, UnicodeString &toAppendTo, FieldPosition &pos) const override
Formats the specified 64-bit number using the default ruleset.
virtual void setDecimalFormatSymbols(const DecimalFormatSymbols &symbols)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
virtual void setContext(UDisplayContext value, UErrorCode &status) override
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
virtual UnicodeString & format(int64_t number, const UnicodeString &ruleSetName, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
Formats the specified 64-bit number using the named ruleset.
virtual ~RuleBasedNumberFormat()
Release memory allocated for a RuleBasedNumberFormat when you are finished with it.
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const override
Parses the specified string, beginning at the specified position, according to this formatter's rules...
virtual void setRoundingMode(ERoundingMode roundingMode) override
Set the rounding mode.
virtual void setDefaultRuleSet(const UnicodeString &ruleSetName, UErrorCode &status)
Override the default rule set to use.
virtual RuleBasedNumberFormat * clone() const override
Clone this object polymorphically.
virtual UnicodeString & format(double number, const UnicodeString &ruleSetName, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
Formats the specified number using the named ruleset.
RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale &locale, UErrorCode &status)
Creates a RuleBasedNumberFormat from a predefined ruleset.
virtual UnicodeString getRuleSetDisplayName(int32_t index, const Locale &locale=Locale::getDefault())
Return the rule set display names for the provided locale.
RuleBasedNumberFormat(const RuleBasedNumberFormat &rhs)
Copy constructor.
virtual UnicodeString getDefaultRuleSetName() const
Return the name of the current default rule set.
RuleBasedNumberFormat(const UnicodeString &rules, const UnicodeString &localizations, const Locale &locale, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the description passed in.
virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode &status) const
Return the index'th display name locale.
virtual UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format a decimal number.
virtual UnicodeString & format(int32_t number, const UnicodeString &ruleSetName, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
Formats the specified number using the named ruleset.
virtual bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
static UClassID getStaticClassID(void)
ICU "poor man's RTTI", returns a UClassID for this class.
virtual UnicodeString getRuleSetDisplayName(const UnicodeString &ruleSetName, const Locale &locale=Locale::getDefault())
Return the rule set display name for the provided rule set and locale.
virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols *symbolsToAdopt)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
RuleBasedNumberFormat(const UnicodeString &rules, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the description passed in.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
C++ API: Symbols for formatting numbers.
C++ API: Formattable is a thin wrapper for primitive types used for formatting and parsing.
C++ API: Locale ID object.
URBNFRuleSetTag
Tags for the predefined rulesets.
Definition: rbnf.h:56
@ URBNF_COUNT
One more than the highest normal URBNFRuleSetTag value.
Definition: rbnf.h:84
@ URBNF_DURATION
Requests predefined ruleset for formatting a value as a duration in hours, minutes,...
Definition: rbnf.h:71
@ URBNF_NUMBERING_SYSTEM
Requests predefined ruleset for various non-place-value numbering systems.
Definition: rbnf.h:78
@ URBNF_ORDINAL
Requests predefined ruleset for the ordinal form of a number.
Definition: rbnf.h:66
@ URBNF_SPELLOUT
Requests predefined ruleset for spelling out numeric values in words.
Definition: rbnf.h:61
C++ API: Compatibility APIs for number formatting.
C++ API: String Enumeration.
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
UDisplayContext
Display context settings.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
C++ API: Unicode String.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
C API: Plural rules, select plural keywords for numeric values.
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:59
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301