ICU4C 79
Loading...
Searching...
No Matches
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
21
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"
42
43U_NAMESPACE_BEGIN
44
45class NFRule;
46class NFRuleSet;
47class LocalizationInfo;
48class PluralFormat;
49class RuleBasedCollator;
50
67#ifndef U_HIDE_DEPRECATED_API
73#endif // U_HIDE_DERECATED_API
81#ifndef U_HIDE_DEPRECATED_API
87#endif // U_HIDE_DEPRECATED_API
88};
89
625public:
626
627 //-----------------------------------------------------------------------
628 // constructors
629 //-----------------------------------------------------------------------
630
642 UParseError& perror,
643 UErrorCode& status);
644
669 const UnicodeString& localizations,
670 UParseError& perror,
671 UErrorCode& status);
672
689 const Locale& locale,
690 UParseError& perror,
691 UErrorCode& status);
692
720 const UnicodeString& localizations,
721 const Locale& locale,
722 UParseError& perror,
723 UErrorCode& status);
724
744
745 //-----------------------------------------------------------------------
746 // boilerplate
747 //-----------------------------------------------------------------------
748
755
762
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
796
802 U_I18N_API virtual int32_t getNumberOfRuleSetNames() const;
803
811 U_I18N_API virtual UnicodeString getRuleSetName(int32_t index) const;
812
819
828 U_I18N_API virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const;
829
844 const Locale& locale = Locale::getDefault());
845
855 const Locale& locale = Locale::getDefault());
856
857
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
944protected:
962 virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
963 UnicodeString& appendTo,
964 FieldPosition& pos,
965 UErrorCode& status) const override;
966public:
967
969
984 U_I18N_API virtual void parse(const UnicodeString& text,
985 Formattable& result,
986 ParsePosition& parsePosition) const override;
987
1021 U_I18N_API virtual void setLenient(UBool enabled) override;
1022
1030 U_I18N_API virtual inline UBool isLenient() const override;
1031
1040 U_I18N_API virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
1041
1049
1060 U_I18N_API virtual void setContext(UDisplayContext value, UErrorCode& status) override;
1061
1067 U_I18N_API virtual ERoundingMode getRoundingMode() const override;
1068
1074 U_I18N_API virtual void setRoundingMode(ERoundingMode roundingMode) override;
1075
1076public:
1083
1089 U_I18N_API virtual UClassID getDynamicClassID() const override;
1090
1100
1111
1112private:
1113 RuleBasedNumberFormat() = delete; // default constructor not implemented
1114
1115 // this will ref the localizations if they are not nullptr
1116 // caller must deref to get adoption
1117 RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
1118 const Locale& locale, UParseError& perror, UErrorCode& status);
1119
1120 void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status);
1121 void initCapitalizationContextInfo(const Locale& thelocale);
1122 void dispose();
1123 void stripWhitespace(UnicodeString& src);
1124 void initDefaultRuleSet();
1125 NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
1126
1127 /* friend access */
1128 friend class NFSubstitution;
1129 friend class NFRule;
1130 friend class NFRuleSet;
1131 friend class FractionalPartSubstitution;
1132
1133 inline NFRuleSet * getDefaultRuleSet() const;
1134 void initializeCollator(UErrorCode &status);
1135 const RuleBasedCollator * getCollator() const;
1136 DecimalFormatSymbols * initializeDecimalFormatSymbols(UErrorCode &status);
1137 const DecimalFormatSymbols * getDecimalFormatSymbols() const;
1138 NFRule * initializeDefaultInfinityRule(UErrorCode &status);
1139 const NFRule * getDefaultInfinityRule() const;
1140 NFRule * initializeDefaultNaNRule(UErrorCode &status);
1141 const NFRule * getDefaultNaNRule() const;
1142 PluralFormat *createPluralFormat(UPluralType pluralType, const UnicodeString &pattern, UErrorCode& status) const;
1143 UnicodeString& adjustForCapitalizationContext(int32_t startPos, UnicodeString& currentResult, UErrorCode& status) const;
1144 UnicodeString& format(int64_t number, NFRuleSet *ruleSet, UnicodeString& toAppendTo, UErrorCode& status) const;
1145 void format(double number, NFRuleSet& rs, UnicodeString& toAppendTo, UErrorCode& status) const;
1146
1147private:
1148 NFRuleSet **fRuleSets { };
1149 UnicodeString* ruleSetDescriptions { };
1150 int32_t numRuleSets { };
1151 NFRuleSet *defaultRuleSet { };
1152 Locale locale;
1153 RuleBasedCollator* collator { };
1154 DecimalFormatSymbols* decimalFormatSymbols { };
1155 NFRule *defaultInfinityRule { };
1156 NFRule *defaultNaNRule { };
1157 ERoundingMode fRoundingMode { ERoundingMode::kRoundUnnecessary };
1158 UBool lenient { };
1159 UBool unparseable { };
1160 uint8_t caseFoldOption { };
1161 UnicodeString* lenientParseRules { };
1162 LocalizationInfo* localizations { };
1163 UnicodeString originalDescription;
1164 UBool capitalizationInfoSet { };
1165 UBool capitalizationForUIListMenu { };
1166 UBool capitalizationForStandAlone { };
1167 BreakIterator* capitalizationBrkIter { };
1168};
1169
1170// ---------------
1171
1172inline UBool
1174 return lenient;
1175}
1176
1177inline NFRuleSet*
1178RuleBasedNumberFormat::getDefaultRuleSet() const {
1179 return defaultRuleSet;
1180}
1181
1182U_NAMESPACE_END
1183
1184/* U_HAVE_RBNF */
1185#endif
1186
1187#endif /* U_SHOW_CPLUSPLUS_API */
1188
1189/* RBNF_H */
1190#endif
C++ API: Break Iterator.
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:63
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:198
static U_COMMON_API const Locale & getDefault()
Common methods of getting the current default Locale.
NumberFormat()
Default constructor for subclass use only.
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.
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format an object to produce a string.
virtual UBool isLenient() const
Returns whether lenient parsing is enabled (it is off by default).
Definition numfmt.h:1272
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
virtual U_I18N_API UBool isLenient() const override
Returns true if lenient-parse mode is turned on.
Definition rbnf.h:1173
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...
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.
virtual U_I18N_API UClassID getDynamicClassID() const override
ICU "poor man's RTTI", returns a UClassID for the actual class.
virtual U_I18N_API UnicodeString getRuleSetName(int32_t index) const
Return the name of the index'th public ruleSet.
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.
virtual U_I18N_API UnicodeString getDefaultRuleSetName() const
Return the name of the current default rule set.
virtual U_I18N_API int32_t getNumberOfRuleSetDisplayNameLocales() const
Return the number of locales for which we have localized rule set display names.
virtual U_I18N_API void setRoundingMode(ERoundingMode roundingMode) override
Set the rounding mode.
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.
virtual U_I18N_API int32_t getNumberOfRuleSetNames() const
Return the number of public rule set names.
virtual U_I18N_API UnicodeString getRuleSetDisplayName(int32_t index, const Locale &locale=Locale::getDefault())
Return the rule set display names for the provided locale.
virtual U_I18N_API void setDefaultRuleSet(const UnicodeString &ruleSetName, UErrorCode &status)
Override the default rule set to use.
virtual U_I18N_API bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
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.
virtual U_I18N_API RuleBasedNumberFormat * clone() const override
Clone this object polymorphically.
virtual U_I18N_API ~RuleBasedNumberFormat()
Release memory allocated for a RuleBasedNumberFormat when you are finished with it.
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.
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.
static U_I18N_API UClassID getStaticClassID()
ICU "poor man's RTTI", returns a UClassID for this class.
virtual U_I18N_API ERoundingMode getRoundingMode() const override
Get the rounding mode.
virtual U_I18N_API void setDecimalFormatSymbols(const DecimalFormatSymbols &symbols)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
U_I18N_API RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale &locale, UErrorCode &status)
Creates a RuleBasedNumberFormat from a predefined ruleset.
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.
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.
virtual U_I18N_API Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode &status) const
Return the index'th display name locale.
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...
U_I18N_API RuleBasedNumberFormat(const UnicodeString &rules, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the description passed in.
virtual U_I18N_API UnicodeString getRules() const
return the rules that were provided to the RuleBasedNumberFormat.
virtual U_I18N_API void setLenient(UBool enabled) override
Turns lenient parse mode on and off.
U_I18N_API RuleBasedNumberFormat & operator=(const RuleBasedNumberFormat &rhs)
Assignment operator.
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.
virtual UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format a decimal number.
virtual U_I18N_API void adoptDecimalFormatSymbols(DecimalFormatSymbols *symbolsToAdopt)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
virtual U_I18N_API UnicodeString & format(double number, UnicodeString &toAppendTo, FieldPosition &pos) const override
Formats the specified number using the default ruleset.
U_I18N_API RuleBasedNumberFormat(const RuleBasedNumberFormat &rhs)
Copy constructor.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
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:86
@ URBNF_DURATION
Requests predefined ruleset for formatting a value as a duration in hours, minutes,...
Definition rbnf.h:72
@ URBNF_NUMBERING_SYSTEM
Requests predefined ruleset for various non-place-value numbering systems.
Definition rbnf.h:80
@ 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:269
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.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:509
#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
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:316