ICU 57.1  57.1
rbnf.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * Copyright (C) 1997-2015, International Business Machines Corporation and others.
4 * All Rights Reserved.
5 *******************************************************************************
6 */
7 
8 #ifndef RBNF_H
9 #define RBNF_H
10 
11 #include "unicode/utypes.h"
12 
25 #if UCONFIG_NO_FORMATTING
26 #define U_HAVE_RBNF 0
27 #else
28 #define U_HAVE_RBNF 1
29 
30 #include "unicode/dcfmtsym.h"
31 #include "unicode/fmtable.h"
32 #include "unicode/locid.h"
33 #include "unicode/numfmt.h"
34 #include "unicode/unistr.h"
35 #include "unicode/strenum.h"
36 #include "unicode/brkiter.h"
37 #include "unicode/upluralrules.h"
38 
40 
41 class NFRule;
42 class NFRuleSet;
43 class LocalizationInfo;
44 class PluralFormat;
45 class RuleBasedCollator;
46 
53  URBNF_SPELLOUT,
54  URBNF_ORDINAL,
55  URBNF_DURATION,
56  URBNF_NUMBERING_SYSTEM,
57  URBNF_COUNT
58 };
59 
554 public:
555 
556  //-----------------------------------------------------------------------
557  // constructors
558  //-----------------------------------------------------------------------
559 
570  RuleBasedNumberFormat(const UnicodeString& rules, UParseError& perror, UErrorCode& status);
571 
595  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
596  UParseError& perror, UErrorCode& status);
597 
613  RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale,
614  UParseError& perror, UErrorCode& status);
615 
642  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
643  const Locale& locale, UParseError& perror, UErrorCode& status);
644 
660  RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& locale, UErrorCode& status);
661 
662  //-----------------------------------------------------------------------
663  // boilerplate
664  //-----------------------------------------------------------------------
665 
672 
679 
684  virtual ~RuleBasedNumberFormat();
685 
692  virtual Format* clone(void) const;
693 
701  virtual UBool operator==(const Format& other) const;
702 
703 //-----------------------------------------------------------------------
704 // public API functions
705 //-----------------------------------------------------------------------
706 
712  virtual UnicodeString getRules() const;
713 
719  virtual int32_t getNumberOfRuleSetNames() const;
720 
728  virtual UnicodeString getRuleSetName(int32_t index) const;
729 
735  virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const;
736 
745  virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const;
746 
760  virtual UnicodeString getRuleSetDisplayName(int32_t index,
761  const Locale& locale = Locale::getDefault());
762 
771  virtual UnicodeString getRuleSetDisplayName(const UnicodeString& ruleSetName,
772  const Locale& locale = Locale::getDefault());
773 
774 
775  using NumberFormat::format;
776 
785  virtual UnicodeString& format(int32_t number,
786  UnicodeString& toAppendTo,
787  FieldPosition& pos) const;
788 
797  virtual UnicodeString& format(int64_t number,
798  UnicodeString& toAppendTo,
799  FieldPosition& pos) const;
808  virtual UnicodeString& format(double number,
809  UnicodeString& toAppendTo,
810  FieldPosition& pos) const;
811 
823  virtual UnicodeString& format(int32_t number,
824  const UnicodeString& ruleSetName,
825  UnicodeString& toAppendTo,
826  FieldPosition& pos,
827  UErrorCode& status) const;
839  virtual UnicodeString& format(int64_t number,
840  const UnicodeString& ruleSetName,
841  UnicodeString& toAppendTo,
842  FieldPosition& pos,
843  UErrorCode& status) const;
855  virtual UnicodeString& format(double number,
856  const UnicodeString& ruleSetName,
857  UnicodeString& toAppendTo,
858  FieldPosition& pos,
859  UErrorCode& status) const;
860 
861  using NumberFormat::parse;
862 
877  virtual void parse(const UnicodeString& text,
878  Formattable& result,
879  ParsePosition& parsePosition) const;
880 
881 #if !UCONFIG_NO_COLLATION
882 
916  virtual void setLenient(UBool enabled);
917 
925  virtual inline UBool isLenient(void) const;
926 
927 #endif
928 
937  virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
938 
945  virtual UnicodeString getDefaultRuleSetName() const;
946 
957  virtual void setContext(UDisplayContext value, UErrorCode& status);
958 
959 public:
965  static UClassID U_EXPORT2 getStaticClassID(void);
966 
972  virtual UClassID getDynamicClassID(void) const;
973 
982  virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
983 
993  virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
994 
995 private:
996  RuleBasedNumberFormat(); // default constructor not implemented
997 
998  // this will ref the localizations if they are not NULL
999  // caller must deref to get adoption
1000  RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
1001  const Locale& locale, UParseError& perror, UErrorCode& status);
1002 
1003  void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status);
1004  void initCapitalizationContextInfo(const Locale& thelocale);
1005  void dispose();
1006  void stripWhitespace(UnicodeString& src);
1007  void initDefaultRuleSet();
1008  void format(double number, NFRuleSet& ruleSet);
1009  NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
1010 
1011  /* friend access */
1012  friend class NFSubstitution;
1013  friend class NFRule;
1014  friend class NFRuleSet;
1015  friend class FractionalPartSubstitution;
1016 
1017  inline NFRuleSet * getDefaultRuleSet() const;
1018  const RuleBasedCollator * getCollator() const;
1019  DecimalFormatSymbols * initializeDecimalFormatSymbols(UErrorCode &status);
1020  const DecimalFormatSymbols * getDecimalFormatSymbols() const;
1021  NFRule * initializeDefaultInfinityRule(UErrorCode &status);
1022  const NFRule * getDefaultInfinityRule() const;
1023  NFRule * initializeDefaultNaNRule(UErrorCode &status);
1024  const NFRule * getDefaultNaNRule() const;
1025  PluralFormat *createPluralFormat(UPluralType pluralType, const UnicodeString &pattern, UErrorCode& status) const;
1026  UnicodeString& adjustForCapitalizationContext(int32_t startPos, UnicodeString& currentResult) const;
1027 
1028 private:
1029  NFRuleSet **ruleSets;
1030  UnicodeString* ruleSetDescriptions;
1031  int32_t numRuleSets;
1032  NFRuleSet *defaultRuleSet;
1033  Locale locale;
1034  RuleBasedCollator* collator;
1035  DecimalFormatSymbols* decimalFormatSymbols;
1036  NFRule *defaultInfinityRule;
1037  NFRule *defaultNaNRule;
1038  UBool lenient;
1039  UnicodeString* lenientParseRules;
1040  LocalizationInfo* localizations;
1041  UnicodeString originalDescription;
1042  UBool capitalizationInfoSet;
1043  UBool capitalizationForUIListMenu;
1044  UBool capitalizationForStandAlone;
1045  BreakIterator* capitalizationBrkIter;
1046 };
1047 
1048 // ---------------
1049 
1050 #if !UCONFIG_NO_COLLATION
1051 
1052 inline UBool
1053 RuleBasedNumberFormat::isLenient(void) const {
1054  return lenient;
1055 }
1056 
1057 #endif
1058 
1059 inline NFRuleSet*
1060 RuleBasedNumberFormat::getDefaultRuleSet() const {
1061  return defaultRuleSet;
1062 }
1063 
1065 
1066 /* U_HAVE_RBNF */
1067 #endif
1068 
1069 /* RBNF_H */
1070 #endif
C++ API: Break Iterator.
Base class for all formats.
Definition: format.h:94
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:84
virtual UClassID getDynamicClassID(void) const =0
Returns a unique class ID POLYMORPHICALLY.
Abstract base class for all number formats.
Definition: numfmt.h:167
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.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:91
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).
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition: numfmt.h:1168
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:358
URBNFRuleSetTag
Tags for the predefined rulesets.
Definition: rbnf.h:52
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
The RuleBasedCollator class provides the implementation of Collator, using data-driven tables...
Definition: tblcoll.h:111
The BreakIterator class implements methods for finding the location of boundaries in text...
Definition: brkiter.h:100
C API: Plural rules, select plural keywords for numeric values.
The RuleBasedNumberFormat class formats numbers according to a set of rules.
Definition: rbnf.h:553
virtual UBool operator==(const Format &other) const
Return true if the given Format objects are semantically equal.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
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:476
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:106
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:44
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:47
C++ API: Locale ID object.
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:56
Basic definitions for ICU, for both C and C++ APIs.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:293
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:66
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format an object to produce a string.
C++ API: Abstract base class for all number formats.
NumberFormat & operator=(const NumberFormat &)
Assignment operator.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234
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:185