ICU 50.2 50.2
rbnf.h
Go to the documentation of this file.
1/*
2*******************************************************************************
3* Copyright (C) 1997-2012, 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
17
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
40class NFRuleSet;
41class 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
57class Collator;
58#endif
59
504public:
505
506 //-----------------------------------------------------------------------
507 // constructors
508 //-----------------------------------------------------------------------
509
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
611
612 //-----------------------------------------------------------------------
613 // boilerplate
614 //-----------------------------------------------------------------------
615
622
629
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
711 const Locale& locale = Locale::getDefault());
712
722 const Locale& locale = Locale::getDefault());
723
724
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
820 virtual UnicodeString& format(const Formattable& obj,
821 UnicodeString& toAppendTo,
822 FieldPosition& pos,
823 UErrorCode& status) const;
832 UnicodeString& format(const Formattable& obj,
833 UnicodeString& result,
834 UErrorCode& status) const;
835
843 UnicodeString& format(double number,
844 UnicodeString& output) const;
845
853 UnicodeString& format(int32_t number,
854 UnicodeString& output) const;
855
870 virtual void parse(const UnicodeString& text,
871 Formattable& result,
872 ParsePosition& parsePosition) const;
873
874
882 virtual inline void parse(const UnicodeString& text,
883 Formattable& result,
884 UErrorCode& status) const;
885
886#if !UCONFIG_NO_COLLATION
887
921 virtual void setLenient(UBool enabled);
922
930 virtual inline UBool isLenient(void) const;
931
932#endif
933
942 virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
943
951
952public:
958 static UClassID U_EXPORT2 getStaticClassID(void);
959
965 virtual UClassID getDynamicClassID(void) const;
966
975 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
976
986 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
987
988private:
989 RuleBasedNumberFormat(); // default constructor not implemented
990
991 // this will ref the localizations if they are not NULL
992 // caller must deref to get adoption
993 RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
994 const Locale& locale, UParseError& perror, UErrorCode& status);
995
996 void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status);
997 void dispose();
998 void stripWhitespace(UnicodeString& src);
999 void initDefaultRuleSet();
1000 void format(double number, NFRuleSet& ruleSet);
1001 NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
1002
1003 /* friend access */
1004 friend class NFSubstitution;
1005 friend class NFRule;
1006 friend class FractionalPartSubstitution;
1007
1008 inline NFRuleSet * getDefaultRuleSet() const;
1009 Collator * getCollator() const;
1010 DecimalFormatSymbols * getDecimalFormatSymbols() const;
1011
1012private:
1013 NFRuleSet **ruleSets;
1014 UnicodeString* ruleSetDescriptions;
1015 int32_t numRuleSets;
1016 NFRuleSet *defaultRuleSet;
1017 Locale locale;
1018 Collator* collator;
1019 DecimalFormatSymbols* decimalFormatSymbols;
1020 UBool lenient;
1021 UnicodeString* lenientParseRules;
1022 LocalizationInfo* localizations;
1023};
1024
1025// ---------------
1026
1027inline UnicodeString&
1029 UnicodeString& result,
1030 UErrorCode& status) const
1031{
1032 // Don't use Format:: - use immediate base class only,
1033 // in case immediate base modifies behavior later.
1034 // dlf - the above comment is bogus, if there were a reason to modify
1035 // it, it would be virtual, and there's no reason because it is
1036 // a one-line macro in NumberFormat anyway, just like this one.
1037 return NumberFormat::format(obj, result, status);
1038}
1039
1040inline UnicodeString&
1041RuleBasedNumberFormat::format(double number, UnicodeString& output) const {
1042 FieldPosition pos(0);
1043 return format(number, output, pos);
1044}
1045
1046inline UnicodeString&
1047RuleBasedNumberFormat::format(int32_t number, UnicodeString& output) const {
1048 FieldPosition pos(0);
1049 return format(number, output, pos);
1050}
1051
1052inline void
1054{
1055 NumberFormat::parse(text, result, status);
1056}
1057
1058#if !UCONFIG_NO_COLLATION
1059
1060inline UBool
1062 return lenient;
1063}
1064
1065#endif
1066
1067inline NFRuleSet*
1068RuleBasedNumberFormat::getDefaultRuleSet() const {
1069 return defaultRuleSet;
1070}
1071
1073
1074/* U_HAVE_RBNF */
1075#endif
1076
1077/* RBNF_H */
1078#endif
The Collator class performs locale-sensitive string comparison.
Definition coll.h:177
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition dcfmtsym.h:84
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition fieldpos.h:106
Format()
Default constructor for subclass use only.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition fmtable.h:62
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:182
static const Locale & getDefault(void)
Common methods of getting the current default Locale.
NumberFormat()
Default constructor for subclass use only.
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
Format an object to produce a string.
virtual UBool isLenient(void) const
Returns whether lenient parsing is enabled (it is off by default).
Definition numfmt.h:1110
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition parsepos.h:47
RuleBasedNumberFormat(const UnicodeString &rules, const UnicodeString &localizations, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the description passed in.
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.
RuleBasedNumberFormat(const RuleBasedNumberFormat &rhs)
Copy constructor.
RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale &locale, UErrorCode &status)
Creates a RuleBasedNumberFormat from a predefined ruleset.
virtual UnicodeString & format(int32_t number, const UnicodeString &ruleSetName, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
Formats the specified number using the named ruleset.
virtual void setDecimalFormatSymbols(const DecimalFormatSymbols &symbols)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
virtual int32_t getNumberOfRuleSetNames() const
Return the number of public rule set names.
virtual UnicodeString getRules() const
return the rules that were provided to the RuleBasedNumberFormat.
virtual UnicodeString & format(int64_t number, UnicodeString &toAppendTo, FieldPosition &pos) const
Formats the specified 64-bit number using the default ruleset.
virtual UnicodeString getRuleSetDisplayName(int32_t index, const Locale &locale=Locale::getDefault())
Return the rule set display names for the provided locale.
RuleBasedNumberFormat(const UnicodeString &rules, const Locale &locale, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the rules passed in.
static UClassID getStaticClassID(void)
ICU "poor man's RTTI", returns a UClassID for this class.
virtual UnicodeString & format(double number, const UnicodeString &ruleSetName, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
Formats the specified number using the named ruleset.
virtual Format * clone(void) const
Clone this object polymorphically.
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 UnicodeString getRuleSetName(int32_t index) const
Return the name of the index'th public ruleSet.
RuleBasedNumberFormat(const UnicodeString &rules, UParseError &perror, UErrorCode &status)
Creates a RuleBasedNumberFormat that behaves according to the description passed in.
virtual void setLenient(UBool enabled)
Turns lenient parse mode on and off.
virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode &status) const
Return the index'th display name locale.
virtual UnicodeString & format(double number, UnicodeString &toAppendTo, FieldPosition &pos) const
Formats the specified number using the default ruleset.
virtual UBool operator==(const Format &other) const
Return true if the given Format objects are semantically equal.
virtual ~RuleBasedNumberFormat()
Release memory allocated for a RuleBasedNumberFormat when you are finished with it.
RuleBasedNumberFormat & operator=(const RuleBasedNumberFormat &rhs)
Assignment operator.
virtual UnicodeString & format(int32_t number, UnicodeString &toAppendTo, FieldPosition &pos) const
Formats the specified 32-bit 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 UnicodeString & format(const Formattable &obj, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
Formats the specified number using the default ruleset.
virtual UClassID getDynamicClassID(void) const
ICU "poor man's RTTI", returns a UClassID for the actual 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 int32_t getNumberOfRuleSetDisplayNameLocales(void) const
Return the number of locales for which we have localized rule set display names.
virtual void setDefaultRuleSet(const UnicodeString &ruleSetName, UErrorCode &status)
Override the default rule set to use.
virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols *symbolsToAdopt)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
virtual UnicodeString getDefaultRuleSetName() const
Return the name of the current default rule set.
virtual UBool isLenient(void) const
Returns true if lenient-parse mode is turned on.
Definition rbnf.h:1061
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:247
C++ API: Collation Service.
C++ API: Symbols for formatting numbers.
C++ API: Formattable is a thin wrapper for primitive numeric types.
C++ API: Locale ID object.
C++ API: Abstract base class for all number formats.
URBNFRuleSetTag
Tags for the predefined rulesets.
Definition rbnf.h:48
C++ API: String Enumeration.
A UParseError struct is used to returned detailed information about parsing errors.
Definition parseerr.h:56
int8_t UBool
The ICU boolean type.
Definition umachine.h:200
C++ API: Unicode String.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition uobject.h:96
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition utypes.h:476
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:358
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition uversion.h:130
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition uversion.h:129