ICU 69.1  69.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 
57  URBNF_SPELLOUT,
58  URBNF_ORDINAL,
59  URBNF_DURATION,
60  URBNF_NUMBERING_SYSTEM,
61 #ifndef U_HIDE_DEPRECATED_API
67 #endif // U_HIDE_DEPRECATED_API
68 };
69 
563 public:
564 
565  //-----------------------------------------------------------------------
566  // constructors
567  //-----------------------------------------------------------------------
568 
580 
604  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
605  UParseError& perror, UErrorCode& status);
606 
622  RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale,
623  UParseError& perror, UErrorCode& status);
624 
651  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
652  const Locale& locale, UParseError& perror, UErrorCode& status);
653 
670 
671  //-----------------------------------------------------------------------
672  // boilerplate
673  //-----------------------------------------------------------------------
674 
681 
688 
694 
701  virtual RuleBasedNumberFormat* clone() const;
702 
710  virtual UBool operator==(const Format& other) const;
711 
712 //-----------------------------------------------------------------------
713 // public API functions
714 //-----------------------------------------------------------------------
715 
721  virtual UnicodeString getRules() const;
722 
728  virtual int32_t getNumberOfRuleSetNames() const;
729 
737  virtual UnicodeString getRuleSetName(int32_t index) const;
738 
744  virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const;
745 
754  virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const;
755 
769  virtual UnicodeString getRuleSetDisplayName(int32_t index,
770  const Locale& locale = Locale::getDefault());
771 
781  const Locale& locale = Locale::getDefault());
782 
783 
784  using NumberFormat::format;
785 
794  virtual UnicodeString& format(int32_t number,
795  UnicodeString& toAppendTo,
796  FieldPosition& pos) const;
797 
806  virtual UnicodeString& format(int64_t number,
807  UnicodeString& toAppendTo,
808  FieldPosition& pos) const;
817  virtual UnicodeString& format(double number,
818  UnicodeString& toAppendTo,
819  FieldPosition& pos) const;
820 
832  virtual UnicodeString& format(int32_t number,
833  const UnicodeString& ruleSetName,
834  UnicodeString& toAppendTo,
835  FieldPosition& pos,
836  UErrorCode& status) const;
848  virtual UnicodeString& format(int64_t number,
849  const UnicodeString& ruleSetName,
850  UnicodeString& toAppendTo,
851  FieldPosition& pos,
852  UErrorCode& status) const;
864  virtual UnicodeString& format(double number,
865  const UnicodeString& ruleSetName,
866  UnicodeString& toAppendTo,
867  FieldPosition& pos,
868  UErrorCode& status) const;
869 
870 protected:
888  virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
889  UnicodeString& appendTo,
890  FieldPosition& pos,
891  UErrorCode& status) const;
892 public:
893 
894  using NumberFormat::parse;
895 
910  virtual void parse(const UnicodeString& text,
911  Formattable& result,
912  ParsePosition& parsePosition) const;
913 
914 #if !UCONFIG_NO_COLLATION
915 
949  virtual void setLenient(UBool enabled);
950 
958  virtual inline UBool isLenient(void) const;
959 
960 #endif
961 
970  virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
971 
979 
990  virtual void setContext(UDisplayContext value, UErrorCode& status);
991 
997  virtual ERoundingMode getRoundingMode(void) const;
998 
1004  virtual void setRoundingMode(ERoundingMode roundingMode);
1005 
1006 public:
1012  static UClassID U_EXPORT2 getStaticClassID(void);
1013 
1019  virtual UClassID getDynamicClassID(void) const;
1020 
1029  virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
1030 
1040  virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1041 
1042 private:
1043  RuleBasedNumberFormat(); // default constructor not implemented
1044 
1045  // this will ref the localizations if they are not NULL
1046  // caller must deref to get adoption
1047  RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
1048  const Locale& locale, UParseError& perror, UErrorCode& status);
1049 
1050  void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status);
1051  void initCapitalizationContextInfo(const Locale& thelocale);
1052  void dispose();
1053  void stripWhitespace(UnicodeString& src);
1054  void initDefaultRuleSet();
1055  NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
1056 
1057  /* friend access */
1058  friend class NFSubstitution;
1059  friend class NFRule;
1060  friend class NFRuleSet;
1061  friend class FractionalPartSubstitution;
1062 
1063  inline NFRuleSet * getDefaultRuleSet() const;
1064  const RuleBasedCollator * getCollator() const;
1065  DecimalFormatSymbols * initializeDecimalFormatSymbols(UErrorCode &status);
1066  const DecimalFormatSymbols * getDecimalFormatSymbols() const;
1067  NFRule * initializeDefaultInfinityRule(UErrorCode &status);
1068  const NFRule * getDefaultInfinityRule() const;
1069  NFRule * initializeDefaultNaNRule(UErrorCode &status);
1070  const NFRule * getDefaultNaNRule() const;
1071  PluralFormat *createPluralFormat(UPluralType pluralType, const UnicodeString &pattern, UErrorCode& status) const;
1072  UnicodeString& adjustForCapitalizationContext(int32_t startPos, UnicodeString& currentResult, UErrorCode& status) const;
1073  UnicodeString& format(int64_t number, NFRuleSet *ruleSet, UnicodeString& toAppendTo, UErrorCode& status) const;
1074  void format(double number, NFRuleSet& rs, UnicodeString& toAppendTo, UErrorCode& status) const;
1075 
1076 private:
1077  NFRuleSet **fRuleSets;
1078  UnicodeString* ruleSetDescriptions;
1079  int32_t numRuleSets;
1080  NFRuleSet *defaultRuleSet;
1081  Locale locale;
1082  RuleBasedCollator* collator;
1083  DecimalFormatSymbols* decimalFormatSymbols;
1084  NFRule *defaultInfinityRule;
1085  NFRule *defaultNaNRule;
1086  ERoundingMode fRoundingMode;
1087  UBool lenient;
1088  UnicodeString* lenientParseRules;
1089  LocalizationInfo* localizations;
1090  UnicodeString originalDescription;
1091  UBool capitalizationInfoSet;
1092  UBool capitalizationForUIListMenu;
1093  UBool capitalizationForStandAlone;
1094  BreakIterator* capitalizationBrkIter;
1095 };
1096 
1097 // ---------------
1098 
1099 #if !UCONFIG_NO_COLLATION
1100 
1101 inline UBool
1102 RuleBasedNumberFormat::isLenient(void) const {
1103  return lenient;
1104 }
1105 
1106 #endif
1107 
1108 inline NFRuleSet*
1109 RuleBasedNumberFormat::getDefaultRuleSet() const {
1110  return defaultRuleSet;
1111 }
1112 
1113 U_NAMESPACE_END
1114 
1115 /* U_HAVE_RBNF */
1116 #endif
1117 
1118 #endif /* U_SHOW_CPLUSPLUS_API */
1119 
1120 /* RBNF_H */
1121 #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:1259
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:562
virtual UClassID getDynamicClassID(void) const
ICU "poor man's RTTI", returns a UClassID for the actual class.
RuleBasedNumberFormat & operator=(const RuleBasedNumberFormat &rhs)
Assignment operator.
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 void setRoundingMode(ERoundingMode roundingMode)
Set the rounding mode.
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 UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a decimal number.
virtual UBool operator==(const Format &other) const
Return true if the given Format objects are semantically equal.
virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const
Return the number of locales for which we have localized rule set display names.
virtual UnicodeString & format(int64_t number, UnicodeString &toAppendTo, FieldPosition &pos) const
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 UnicodeString & format(double number, UnicodeString &toAppendTo, FieldPosition &pos) const
Formats the specified number using the default ruleset.
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 setDefaultRuleSet(const UnicodeString &ruleSetName, UErrorCode &status)
Override the default rule set to use.
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const
Parses the specfied string, beginning at the specified position, according to this formatter's rules.
virtual void setLenient(UBool enabled)
Turns lenient parse mode on and off.
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 void setContext(UDisplayContext value, UErrorCode &status)
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
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 RuleBasedNumberFormat * clone() const
Clone this object polymorphically.
virtual ERoundingMode getRoundingMode(void) const
Get the rounding mode.
virtual UnicodeString & format(int32_t number, const UnicodeString &ruleSetName, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
Formats the specified number using the named ruleset.
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.
virtual UnicodeString & format(int32_t number, UnicodeString &toAppendTo, FieldPosition &pos) const
Formats the specified 32-bit number using the default ruleset.
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:66
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.
Definition: upluralrules.h:58
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