ICU 78.1 78.1

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
56 enum URBNFRuleSetTag {
61 URBNF_SPELLOUT,
66 URBNF_ORDINAL,
67 #ifndef U_HIDE_DEPRECATED_API
72 URBNF_DURATION,
73 #endif // U_HIDE_DERECATED_API
80 URBNF_NUMBERING_SYSTEM = 3,
81 #ifndef U_HIDE_DEPRECATED_API
86 URBNF_COUNT
87 #endif // U_HIDE_DEPRECATED_API
88 };
89
624 class U_I18N_API_CLASS RuleBasedNumberFormat : public NumberFormat {
625 public:
626
627 //-----------------------------------------------------------------------
628 // constructors
629 //-----------------------------------------------------------------------
630
641 U_I18N_API RuleBasedNumberFormat(const UnicodeString& rules,
642 UParseError& perror,
643 UErrorCode& status);
644
668 U_I18N_API RuleBasedNumberFormat(const UnicodeString& rules,
669 const UnicodeString& localizations,
670 UParseError& perror,
671 UErrorCode& status);
672
688 U_I18N_API RuleBasedNumberFormat(const UnicodeString& rules,
689 const Locale& locale,
690 UParseError& perror,
691 UErrorCode& status);
692
719 U_I18N_API RuleBasedNumberFormat(const UnicodeString& rules,
720 const UnicodeString& localizations,
721 const Locale& locale,
722 UParseError& perror,
723 UErrorCode& status);
724
743 U_I18N_API RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& locale, UErrorCode& status);
744
745 //-----------------------------------------------------------------------
746 // boilerplate
747 //-----------------------------------------------------------------------
748
754 U_I18N_API RuleBasedNumberFormat(const RuleBasedNumberFormat& rhs);
755
761 U_I18N_API RuleBasedNumberFormat& operator=(const RuleBasedNumberFormat& rhs);
762
767 U_I18N_API virtual ~RuleBasedNumberFormat();
768
775 U_I18N_API virtual RuleBasedNumberFormat* clone() const override;
776
784 U_I18N_API virtual bool operator==(const Format& other) const override;
785
786 //-----------------------------------------------------------------------
787 // public API functions
788 //-----------------------------------------------------------------------
789
795 U_I18N_API virtual UnicodeString getRules() const;
796
802 U_I18N_API virtual int32_t getNumberOfRuleSetNames() const;
803
811 U_I18N_API virtual UnicodeString getRuleSetName(int32_t index) const;
812
818 U_I18N_API virtual int32_t getNumberOfRuleSetDisplayNameLocales() const;
819
828 U_I18N_API virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const;
829
843 U_I18N_API virtual UnicodeString getRuleSetDisplayName(int32_t index,
844 const Locale& locale = Locale::getDefault());
845
854 U_I18N_API virtual UnicodeString getRuleSetDisplayName(const UnicodeString& ruleSetName,
855 const Locale& locale = Locale::getDefault());
856
857
858 using NumberFormat::format;
859
868 U_I18N_API virtual UnicodeString& format(int32_t number,
869 UnicodeString& toAppendTo,
870 FieldPosition& pos) const override;
871
880 U_I18N_API virtual UnicodeString& format(int64_t number,
881 UnicodeString& toAppendTo,
882 FieldPosition& pos) const override;
891 U_I18N_API virtual UnicodeString& format(double number,
892 UnicodeString& toAppendTo,
893 FieldPosition& pos) const override;
894
906 U_I18N_API virtual UnicodeString& format(int32_t number,
907 const UnicodeString& ruleSetName,
908 UnicodeString& toAppendTo,
909 FieldPosition& pos,
910 UErrorCode& status) const;
922 U_I18N_API virtual UnicodeString& format(int64_t number,
923 const UnicodeString& ruleSetName,
924 UnicodeString& toAppendTo,
925 FieldPosition& pos,
926 UErrorCode& status) const;
938 U_I18N_API virtual UnicodeString& format(double number,
939 const UnicodeString& ruleSetName,
940 UnicodeString& toAppendTo,
941 FieldPosition& pos,
942 UErrorCode& status) const;
943
944 protected:
962 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
963 UnicodeString& appendTo,
964 FieldPosition& pos,
965 UErrorCode& status) const override;
966 public:
967
968 using NumberFormat::parse;
969
984 U_I18N_API virtual void parse(const UnicodeString& text,
985 Formattable& result,
986 ParsePosition& parsePosition) const override;
987
988 #if !UCONFIG_NO_COLLATION
989
1023 U_I18N_API virtual void setLenient(UBool enabled) override;
1024
1032 U_I18N_API virtual inline UBool isLenient() const override;
1033
1034 #endif
1035
1044 U_I18N_API virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
1045
1052 U_I18N_API virtual UnicodeString getDefaultRuleSetName() const;
1053
1064 U_I18N_API virtual void setContext(UDisplayContext value, UErrorCode& status) override;
1065
1071 U_I18N_API virtual ERoundingMode getRoundingMode() const override;
1072
1078 U_I18N_API virtual void setRoundingMode(ERoundingMode roundingMode) override;
1079
1080 public:
1086 U_I18N_API static UClassID getStaticClassID();
1087
1093 U_I18N_API virtual UClassID getDynamicClassID() const override;
1094
1103 U_I18N_API virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
1104
1114 U_I18N_API virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1115
1116 private:
1117 RuleBasedNumberFormat() = delete; // default constructor not implemented
1118
1119 // this will ref the localizations if they are not nullptr
1120 // caller must deref to get adoption
1121 RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
1122 const Locale& locale, UParseError& perror, UErrorCode& status);
1123
1124 void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status);
1125 void initCapitalizationContextInfo(const Locale& thelocale);
1126 void dispose();
1127 void stripWhitespace(UnicodeString& src);
1128 void initDefaultRuleSet();
1129 NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
1130
1131 /* friend access */
1132 friend class NFSubstitution;
1133 friend class NFRule;
1134 friend class NFRuleSet;
1135 friend class FractionalPartSubstitution;
1136
1137 inline NFRuleSet * getDefaultRuleSet() const;
1138 const RuleBasedCollator * getCollator() const;
1139 DecimalFormatSymbols * initializeDecimalFormatSymbols(UErrorCode &status);
1140 const DecimalFormatSymbols * getDecimalFormatSymbols() const;
1141 NFRule * initializeDefaultInfinityRule(UErrorCode &status);
1142 const NFRule * getDefaultInfinityRule() const;
1143 NFRule * initializeDefaultNaNRule(UErrorCode &status);
1144 const NFRule * getDefaultNaNRule() const;
1145 PluralFormat *createPluralFormat(UPluralType pluralType, const UnicodeString &pattern, UErrorCode& status) const;
1146 UnicodeString& adjustForCapitalizationContext(int32_t startPos, UnicodeString& currentResult, UErrorCode& status) const;
1147 UnicodeString& format(int64_t number, NFRuleSet *ruleSet, UnicodeString& toAppendTo, UErrorCode& status) const;
1148 void format(double number, NFRuleSet& rs, UnicodeString& toAppendTo, UErrorCode& status) const;
1149
1150 private:
1151 NFRuleSet **fRuleSets;
1152 UnicodeString* ruleSetDescriptions;
1153 int32_t numRuleSets;
1154 NFRuleSet *defaultRuleSet;
1155 Locale locale;
1156 RuleBasedCollator* collator;
1157 DecimalFormatSymbols* decimalFormatSymbols;
1158 NFRule *defaultInfinityRule;
1159 NFRule *defaultNaNRule;
1160 ERoundingMode fRoundingMode;
1161 UBool lenient;
1162 UnicodeString* lenientParseRules;
1163 LocalizationInfo* localizations;
1164 UnicodeString originalDescription;
1165 UBool capitalizationInfoSet;
1166 UBool capitalizationForUIListMenu;
1167 UBool capitalizationForStandAlone;
1168 BreakIterator* capitalizationBrkIter;
1169 };
1170
1171 // ---------------
1172
1173 #if !UCONFIG_NO_COLLATION
1174
1175 inline UBool
1176 RuleBasedNumberFormat::isLenient() const {
1177 return lenient;
1178 }
1179
1180 #endif
1181
1182 inline NFRuleSet*
1183 RuleBasedNumberFormat::getDefaultRuleSet() const {
1184 return defaultRuleSet;
1185 }
1186
1187 U_NAMESPACE_END
1188
1189 /* U_HAVE_RBNF */
1190 #endif
1191
1192 #endif /* U_SHOW_CPLUSPLUS_API */
1193
1194 /* RBNF_H */
1195 #endif
brkiter.h
C++ API: Break Iterator.
icu::BreakIterator
The BreakIterator class implements methods for finding the location of boundaries in text.
Definition: brkiter.h:106
icu::DecimalFormatSymbols
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:86
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
icu::Format
Base class for all formats.
Definition: format.h:98
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:198
icu::NumberFormat
Definition: numfmt.h:175
icu::NumberFormat::format
U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
icu::NumberFormat::ERoundingMode
ERoundingMode
Rounding mode.
Definition: numfmt.h:186
icu::NumberFormat::parse
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
Return a long if possible (e.g.
icu::NumberFormat::isLenient
virtual UBool isLenient() const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1274
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
icu::PluralFormat
Definition: plurfmt.h:148
icu::RuleBasedCollator
The RuleBasedCollator class provides the implementation of Collator, using data-driven tables.
Definition: tblcoll.h:115
icu::RuleBasedNumberFormat
The RuleBasedNumberFormat class formats numbers according to a set of rules.
Definition: rbnf.h:624
icu::RuleBasedNumberFormat::setContext
virtual U_I18N_API void setContext(UDisplayContext value, UErrorCode &status) override
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
icu::RuleBasedNumberFormat::format
virtual U_I18N_API UnicodeString & format(int64_t number, UnicodeString &toAppendTo, FieldPosition &pos) const override
Formats the specified 64-bit number using the default ruleset.
icu::RuleBasedNumberFormat::getDynamicClassID
virtual U_I18N_API UClassID getDynamicClassID() const override
ICU "poor man's RTTI", returns a UClassID for the actual class.
icu::RuleBasedNumberFormat::getRuleSetName
virtual U_I18N_API UnicodeString getRuleSetName(int32_t index) const
Return the name of the index'th public ruleSet.
icu::RuleBasedNumberFormat::RuleBasedNumberFormat
U_I18N_API RuleBasedNumberFormat(const UnicodeString &rules, const UnicodeString &localizations, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the description passed in.
icu::RuleBasedNumberFormat::getDefaultRuleSetName
virtual U_I18N_API UnicodeString getDefaultRuleSetName() const
Return the name of the current default rule set.
icu::RuleBasedNumberFormat::format
virtual U_I18N_API UnicodeString & format(double number, UnicodeString &toAppendTo, FieldPosition &pos) const override
Formats the specified number using the default ruleset.
icu::RuleBasedNumberFormat::getNumberOfRuleSetDisplayNameLocales
virtual U_I18N_API int32_t getNumberOfRuleSetDisplayNameLocales() const
Return the number of locales for which we have localized rule set display names.
icu::RuleBasedNumberFormat::format
virtual U_I18N_API 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.
icu::RuleBasedNumberFormat::setRoundingMode
virtual U_I18N_API void setRoundingMode(ERoundingMode roundingMode) override
Set the rounding mode.
icu::RuleBasedNumberFormat::RuleBasedNumberFormat
U_I18N_API 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.
icu::RuleBasedNumberFormat::getNumberOfRuleSetNames
virtual U_I18N_API int32_t getNumberOfRuleSetNames() const
Return the number of public rule set names.
icu::RuleBasedNumberFormat::getRuleSetDisplayName
virtual U_I18N_API UnicodeString getRuleSetDisplayName(int32_t index, const Locale &locale=Locale::getDefault())
Return the rule set display names for the provided locale.
icu::RuleBasedNumberFormat::setDefaultRuleSet
virtual U_I18N_API void setDefaultRuleSet(const UnicodeString &ruleSetName, UErrorCode &status)
Override the default rule set to use.
icu::RuleBasedNumberFormat::operator==
virtual U_I18N_API bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
icu::RuleBasedNumberFormat::format
virtual U_I18N_API UnicodeString & format(int32_t number, UnicodeString &toAppendTo, FieldPosition &pos) const override
Formats the specified 32-bit number using the default ruleset.
icu::RuleBasedNumberFormat::~RuleBasedNumberFormat
virtual U_I18N_API ~RuleBasedNumberFormat()
Release memory allocated for a RuleBasedNumberFormat when you are finished with it.
icu::RuleBasedNumberFormat::getStaticClassID
static U_I18N_API UClassID getStaticClassID()
ICU "poor man's RTTI", returns a UClassID for this class.
icu::RuleBasedNumberFormat::clone
virtual U_I18N_API RuleBasedNumberFormat * clone() const override
Clone this object polymorphically.
icu::RuleBasedNumberFormat::format
virtual U_I18N_API UnicodeString & format(int32_t number, const UnicodeString &ruleSetName, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
Formats the specified number using the named ruleset.
icu::RuleBasedNumberFormat::getRoundingMode
virtual U_I18N_API ERoundingMode getRoundingMode() const override
Get the rounding mode.
icu::RuleBasedNumberFormat::setDecimalFormatSymbols
virtual U_I18N_API void setDecimalFormatSymbols(const DecimalFormatSymbols &symbols)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
icu::RuleBasedNumberFormat::RuleBasedNumberFormat
U_I18N_API RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale &locale, UErrorCode &status)
Creates a RuleBasedNumberFormat from a predefined ruleset.
icu::RuleBasedNumberFormat::getRuleSetDisplayName
virtual U_I18N_API UnicodeString getRuleSetDisplayName(const UnicodeString &ruleSetName, const Locale &locale=Locale::getDefault())
Return the rule set display name for the provided rule set and locale.
icu::RuleBasedNumberFormat::getRuleSetDisplayNameLocale
virtual U_I18N_API Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode &status) const
Return the index'th display name locale.
icu::RuleBasedNumberFormat::parse
virtual U_I18N_API 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...
icu::RuleBasedNumberFormat::format
virtual U_I18N_API UnicodeString & format(double number, const UnicodeString &ruleSetName, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
Formats the specified number using the named ruleset.
icu::RuleBasedNumberFormat::RuleBasedNumberFormat
U_I18N_API RuleBasedNumberFormat(const UnicodeString &rules, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the description passed in.
icu::RuleBasedNumberFormat::getRules
virtual U_I18N_API UnicodeString getRules() const
return the rules that were provided to the RuleBasedNumberFormat.
icu::RuleBasedNumberFormat::setLenient
virtual U_I18N_API void setLenient(UBool enabled) override
Turns lenient parse mode on and off.
icu::RuleBasedNumberFormat::operator=
U_I18N_API RuleBasedNumberFormat & operator=(const RuleBasedNumberFormat &rhs)
Assignment operator.
icu::RuleBasedNumberFormat::RuleBasedNumberFormat
U_I18N_API RuleBasedNumberFormat(const UnicodeString &rules, const Locale &locale, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the rules passed in.
icu::RuleBasedNumberFormat::adoptDecimalFormatSymbols
virtual U_I18N_API void adoptDecimalFormatSymbols(DecimalFormatSymbols *symbolsToAdopt)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
icu::RuleBasedNumberFormat::format
virtual UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format a decimal number.
icu::RuleBasedNumberFormat::RuleBasedNumberFormat
U_I18N_API RuleBasedNumberFormat(const RuleBasedNumberFormat &rhs)
Copy constructor.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:303
dcfmtsym.h
C++ API: Symbols for formatting numbers.
fmtable.h
C++ API: Formattable is a thin wrapper for primitive types used for formatting and parsing.
locid.h
C++ API: Locale ID object.
icu::URBNFRuleSetTag
URBNFRuleSetTag
Tags for the predefined rulesets.
Definition: rbnf.h:56
icu::URBNF_COUNT
@ URBNF_COUNT
One more than the highest normal URBNFRuleSetTag value.
Definition: rbnf.h:86
icu::URBNF_DURATION
@ URBNF_DURATION
Requests predefined ruleset for formatting a value as a duration in hours, minutes,...
Definition: rbnf.h:72
icu::URBNF_ORDINAL
@ URBNF_ORDINAL
Requests predefined ruleset for the ordinal form of a number.
Definition: rbnf.h:66
icu::URBNF_SPELLOUT
@ URBNF_SPELLOUT
Requests predefined ruleset for spelling out numeric values in words.
Definition: rbnf.h:61
numfmt.h
C++ API: Compatibility APIs for number formatting.
strenum.h
C++ API: String Enumeration.
UParseError
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
UDisplayContext
UDisplayContext
Display context settings.
Definition: udisplaycontext.h:64
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
unistr.h
C++ API: Unicode String.
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
upluralrules.h
C API: Plural rules, select plural keywords for numeric values.
UPluralType
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:59
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:509
U_I18N_API_CLASS
#define U_I18N_API_CLASS
Set to export library symbols from inside the i18n library, and to import them from outside,...
Definition: utypes.h:457
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:316