ICU 52.1  52.1
rbnf.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * Copyright (C) 1997-2013, 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/coll.h"
31 #include "unicode/dcfmtsym.h"
32 #include "unicode/fmtable.h"
33 #include "unicode/locid.h"
34 #include "unicode/numfmt.h"
35 #include "unicode/unistr.h"
36 #include "unicode/strenum.h"
37 
39 
40 class NFRuleSet;
41 class LocalizationInfo;
42 
49  URBNF_SPELLOUT,
50  URBNF_ORDINAL,
51  URBNF_DURATION,
52  URBNF_NUMBERING_SYSTEM,
53  URBNF_COUNT
54 };
55 
56 #if UCONFIG_NO_COLLATION
57 class Collator;
58 #endif
59 
504 public:
505 
506  //-----------------------------------------------------------------------
507  // constructors
508  //-----------------------------------------------------------------------
509 
520  RuleBasedNumberFormat(const UnicodeString& rules, UParseError& perror, UErrorCode& status);
521 
545  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
546  UParseError& perror, UErrorCode& status);
547 
563  RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale,
564  UParseError& perror, UErrorCode& status);
565 
592  RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
593  const Locale& locale, UParseError& perror, UErrorCode& status);
594 
610  RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& locale, UErrorCode& status);
611 
612  //-----------------------------------------------------------------------
613  // boilerplate
614  //-----------------------------------------------------------------------
615 
622 
629 
634  virtual ~RuleBasedNumberFormat();
635 
642  virtual Format* clone(void) const;
643 
651  virtual UBool operator==(const Format& other) const;
652 
653 //-----------------------------------------------------------------------
654 // public API functions
655 //-----------------------------------------------------------------------
656 
662  virtual UnicodeString getRules() const;
663 
669  virtual int32_t getNumberOfRuleSetNames() const;
670 
678  virtual UnicodeString getRuleSetName(int32_t index) const;
679 
685  virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const;
686 
695  virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const;
696 
710  virtual UnicodeString getRuleSetDisplayName(int32_t index,
711  const Locale& locale = Locale::getDefault());
712 
721  virtual UnicodeString getRuleSetDisplayName(const UnicodeString& ruleSetName,
722  const Locale& locale = Locale::getDefault());
723 
724 
725  using NumberFormat::format;
726 
735  virtual UnicodeString& format(int32_t number,
736  UnicodeString& toAppendTo,
737  FieldPosition& pos) const;
738 
747  virtual UnicodeString& format(int64_t number,
748  UnicodeString& toAppendTo,
749  FieldPosition& pos) const;
758  virtual UnicodeString& format(double number,
759  UnicodeString& toAppendTo,
760  FieldPosition& pos) const;
761 
773  virtual UnicodeString& format(int32_t number,
774  const UnicodeString& ruleSetName,
775  UnicodeString& toAppendTo,
776  FieldPosition& pos,
777  UErrorCode& status) const;
789  virtual UnicodeString& format(int64_t number,
790  const UnicodeString& ruleSetName,
791  UnicodeString& toAppendTo,
792  FieldPosition& pos,
793  UErrorCode& status) const;
805  virtual UnicodeString& format(double number,
806  const UnicodeString& ruleSetName,
807  UnicodeString& toAppendTo,
808  FieldPosition& pos,
809  UErrorCode& status) const;
810 
811  using NumberFormat::parse;
812 
827  virtual void parse(const UnicodeString& text,
828  Formattable& result,
829  ParsePosition& parsePosition) const;
830 
831 #if !UCONFIG_NO_COLLATION
832 
866  virtual void setLenient(UBool enabled);
867 
875  virtual inline UBool isLenient(void) const;
876 
877 #endif
878 
887  virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
888 
895  virtual UnicodeString getDefaultRuleSetName() const;
896 
897 public:
903  static UClassID U_EXPORT2 getStaticClassID(void);
904 
910  virtual UClassID getDynamicClassID(void) const;
911 
920  virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
921 
931  virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
932 
933 private:
934  RuleBasedNumberFormat(); // default constructor not implemented
935 
936  // this will ref the localizations if they are not NULL
937  // caller must deref to get adoption
938  RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
939  const Locale& locale, UParseError& perror, UErrorCode& status);
940 
941  void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status);
942  void dispose();
943  void stripWhitespace(UnicodeString& src);
944  void initDefaultRuleSet();
945  void format(double number, NFRuleSet& ruleSet);
946  NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
947 
948  /* friend access */
949  friend class NFSubstitution;
950  friend class NFRule;
951  friend class FractionalPartSubstitution;
952 
953  inline NFRuleSet * getDefaultRuleSet() const;
954  Collator * getCollator() const;
955  DecimalFormatSymbols * getDecimalFormatSymbols() const;
956 
957 private:
958  NFRuleSet **ruleSets;
959  UnicodeString* ruleSetDescriptions;
960  int32_t numRuleSets;
961  NFRuleSet *defaultRuleSet;
962  Locale locale;
963  Collator* collator;
964  DecimalFormatSymbols* decimalFormatSymbols;
965  UBool lenient;
966  UnicodeString* lenientParseRules;
967  LocalizationInfo* localizations;
968 };
969 
970 // ---------------
971 
972 #if !UCONFIG_NO_COLLATION
973 
974 inline UBool
975 RuleBasedNumberFormat::isLenient(void) const {
976  return lenient;
977 }
978 
979 #endif
980 
981 inline NFRuleSet*
982 RuleBasedNumberFormat::getDefaultRuleSet() const {
983  return defaultRuleSet;
984 }
985 
987 
988 /* U_HAVE_RBNF */
989 #endif
990 
991 /* RBNF_H */
992 #endif
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
The Collator class performs locale-sensitive string comparison.
Definition: coll.h:177
virtual UClassID getDynamicClassID(void) const =0
Returns a unique class ID POLYMORPHICALLY.
Abstract base class for all number formats.
Definition: numfmt.h:169
virtual void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
Return a long if possible (e.g.
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:1117
#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:48
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
C++ API: Collation Service.
The RuleBasedNumberFormat class formats numbers according to a set of rules.
Definition: rbnf.h:503
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
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
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:245
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:200
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