ICU 60.1  60.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 
27 #if UCONFIG_NO_FORMATTING
28 #define U_HAVE_RBNF 0
29 #else
30 #define U_HAVE_RBNF 1
31 
32 #include "unicode/dcfmtsym.h"
33 #include "unicode/fmtable.h"
34 #include "unicode/locid.h"
35 #include "unicode/numfmt.h"
36 #include "unicode/unistr.h"
37 #include "unicode/strenum.h"
38 #include "unicode/brkiter.h"
39 #include "unicode/upluralrules.h"
40 
42 
43 class NFRule;
44 class NFRuleSet;
45 class LocalizationInfo;
46 class PluralFormat;
47 class RuleBasedCollator;
48 
55  URBNF_SPELLOUT,
56  URBNF_ORDINAL,
57  URBNF_DURATION,
58  URBNF_NUMBERING_SYSTEM,
59 #ifndef U_HIDE_DEPRECATED_API
60 
65 #endif // U_HIDE_DEPRECATED_API
66 };
67 
562 public:
563 
564  //-----------------------------------------------------------------------
565  // constructors
566  //-----------------------------------------------------------------------
567 
578  RuleBasedNumberFormat(const UnicodeString& rules, UParseError& perror, UErrorCode& status);
579 
603  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
604  UParseError& perror, UErrorCode& status);
605 
621  RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale,
622  UParseError& perror, UErrorCode& status);
623 
650  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
651  const Locale& locale, UParseError& perror, UErrorCode& status);
652 
668  RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& locale, UErrorCode& status);
669 
670  //-----------------------------------------------------------------------
671  // boilerplate
672  //-----------------------------------------------------------------------
673 
680 
687 
692  virtual ~RuleBasedNumberFormat();
693 
700  virtual Format* clone(void) const;
701 
709  virtual UBool operator==(const Format& other) const;
710 
711 //-----------------------------------------------------------------------
712 // public API functions
713 //-----------------------------------------------------------------------
714 
720  virtual UnicodeString getRules() const;
721 
727  virtual int32_t getNumberOfRuleSetNames() const;
728 
736  virtual UnicodeString getRuleSetName(int32_t index) const;
737 
743  virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const;
744 
753  virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const;
754 
768  virtual UnicodeString getRuleSetDisplayName(int32_t index,
769  const Locale& locale = Locale::getDefault());
770 
779  virtual UnicodeString getRuleSetDisplayName(const UnicodeString& ruleSetName,
780  const Locale& locale = Locale::getDefault());
781 
782 
783  using NumberFormat::format;
784 
793  virtual UnicodeString& format(int32_t number,
794  UnicodeString& toAppendTo,
795  FieldPosition& pos) const;
796 
805  virtual UnicodeString& format(int64_t number,
806  UnicodeString& toAppendTo,
807  FieldPosition& pos) const;
816  virtual UnicodeString& format(double number,
817  UnicodeString& toAppendTo,
818  FieldPosition& pos) const;
819 
831  virtual UnicodeString& format(int32_t number,
832  const UnicodeString& ruleSetName,
833  UnicodeString& toAppendTo,
834  FieldPosition& pos,
835  UErrorCode& status) const;
847  virtual UnicodeString& format(int64_t number,
848  const UnicodeString& ruleSetName,
849  UnicodeString& toAppendTo,
850  FieldPosition& pos,
851  UErrorCode& status) const;
863  virtual UnicodeString& format(double number,
864  const UnicodeString& ruleSetName,
865  UnicodeString& toAppendTo,
866  FieldPosition& pos,
867  UErrorCode& status) const;
868 
869 protected:
887  virtual UnicodeString& format(const DigitList &number,
888  UnicodeString& appendTo,
889  FieldPositionIterator* posIter,
890  UErrorCode& status) const;
891 
909  virtual UnicodeString& format(const DigitList &number,
910  UnicodeString& appendTo,
911  FieldPosition& pos,
912  UErrorCode& status) const;
913 public:
914 
915  using NumberFormat::parse;
916 
931  virtual void parse(const UnicodeString& text,
932  Formattable& result,
933  ParsePosition& parsePosition) const;
934 
935 #if !UCONFIG_NO_COLLATION
936 
970  virtual void setLenient(UBool enabled);
971 
979  virtual inline UBool isLenient(void) const;
980 
981 #endif
982 
991  virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
992 
999  virtual UnicodeString getDefaultRuleSetName() const;
1000 
1011  virtual void setContext(UDisplayContext value, UErrorCode& status);
1012 
1018  virtual ERoundingMode getRoundingMode(void) const;
1019 
1025  virtual void setRoundingMode(ERoundingMode roundingMode);
1026 
1027 public:
1033  static UClassID U_EXPORT2 getStaticClassID(void);
1034 
1040  virtual UClassID getDynamicClassID(void) const;
1041 
1050  virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
1051 
1061  virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1062 
1063 private:
1064  RuleBasedNumberFormat(); // default constructor not implemented
1065 
1066  // this will ref the localizations if they are not NULL
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 **ruleSets;
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 roundingMode;
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
1124  return lenient;
1125 }
1126 
1127 #endif
1128 
1129 inline NFRuleSet*
1130 RuleBasedNumberFormat::getDefaultRuleSet() const {
1131  return defaultRuleSet;
1132 }
1133 
1135 
1136 /* U_HAVE_RBNF */
1137 #endif
1138 
1139 /* RBNF_H */
1140 #endif
C++ API: Break Iterator.
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:88
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format an object to produce a string.
virtual UClassID getDynamicClassID(void) const =0
Returns a unique class ID POLYMORPHICALLY.
Abstract base class for all number formats.
Definition: numfmt.h:174
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
Return a long if possible (e.g.
UDisplayContext
Display context settings.
C++ API: Unicode String.
virtual void setRoundingMode(ERoundingMode roundingMode)
Set the rounding mode.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:93
virtual UBool isLenient(void) const
Returns true if lenient-parse mode is turned on.
Definition: rbnf.h:1123
virtual Format * clone() const =0
Clone this object polymorphically.
virtual void setLenient(UBool enable)
Sets whether lenient parsing should be enabled (it is off by default).
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:360
URBNFRuleSetTag
Tags for the predefined rulesets.
Definition: rbnf.h:54
One more than the highest normal URBNFRuleSetTag value.
Definition: rbnf.h:64
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
The RuleBasedCollator class provides the implementation of Collator, using data-driven tables...
Definition: tblcoll.h:113
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:62
The BreakIterator class implements methods for finding the location of boundaries in text...
Definition: brkiter.h:102
C API: Plural rules, select plural keywords for numeric values.
The RuleBasedNumberFormat class formats numbers according to a set of rules.
Definition: rbnf.h:561
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
virtual ERoundingMode getRoundingMode(void) const
Get the rounding mode.
virtual void setContext(UDisplayContext value, UErrorCode &status)
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:396
static UClassID getStaticClassID(void)
Return the class ID for this class.
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:50
ERoundingMode
Rounding mode.
Definition: numfmt.h:185
C++ API: Symbols for formatting numbers.
C++ API: String Enumeration.
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:49
C++ API: Locale ID object.
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
Basic definitions for ICU, for both C and C++ APIs.
virtual UBool operator==(const Format &other) const
Return true if the given Format objects are semantically equal.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:286
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:68
C++ API: Abstract base class for all number formats.
NumberFormat & operator=(const NumberFormat &)
Assignment operator.
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1217
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236
C++ API: Formattable is a thin wrapper for primitive types used for formatting and parsing...
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:188