ICU 66.0.1
66.0.1
|
Parses and represents ICU MessageFormat patterns. More...
#include <messagepattern.h>
Data Structures | |
class | Part |
A message pattern "part", representing a pattern parsing event. More... | |
Public Member Functions | |
MessagePattern (UErrorCode &errorCode) | |
Constructs an empty MessagePattern with default UMessagePatternApostropheMode. More... | |
MessagePattern (UMessagePatternApostropheMode mode, UErrorCode &errorCode) | |
Constructs an empty MessagePattern. More... | |
MessagePattern (const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode) | |
Constructs a MessagePattern with default UMessagePatternApostropheMode and parses the MessageFormat pattern string. More... | |
MessagePattern (const MessagePattern &other) | |
Copy constructor. More... | |
MessagePattern & | operator= (const MessagePattern &other) |
Assignment operator. More... | |
virtual | ~MessagePattern () |
Destructor. More... | |
MessagePattern & | parse (const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode) |
Parses a MessageFormat pattern string. More... | |
MessagePattern & | parseChoiceStyle (const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode) |
Parses a ChoiceFormat pattern string. More... | |
MessagePattern & | parsePluralStyle (const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode) |
Parses a PluralFormat pattern string. More... | |
MessagePattern & | parseSelectStyle (const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode) |
Parses a SelectFormat pattern string. More... | |
void | clear () |
Clears this MessagePattern. More... | |
void | clearPatternAndSetApostropheMode (UMessagePatternApostropheMode mode) |
Clears this MessagePattern and sets the UMessagePatternApostropheMode. More... | |
UBool | operator== (const MessagePattern &other) const |
UBool | operator!= (const MessagePattern &other) const |
int32_t | hashCode () const |
UMessagePatternApostropheMode | getApostropheMode () const |
const UnicodeString & | getPatternString () const |
UBool | hasNamedArguments () const |
Does the parsed pattern have named arguments like {first_name}? More... | |
UBool | hasNumberedArguments () const |
Does the parsed pattern have numbered arguments like {2}? More... | |
UnicodeString | autoQuoteApostropheDeep () const |
Returns a version of the parsed pattern string where each ASCII apostrophe is doubled (escaped) if it is not already, and if it is not interpreted as quoting syntax. More... | |
int32_t | countParts () const |
Returns the number of "parts" created by parsing the pattern string. More... | |
const Part & | getPart (int32_t i) const |
Gets the i-th pattern "part". More... | |
UMessagePatternPartType | getPartType (int32_t i) const |
Returns the UMessagePatternPartType of the i-th pattern "part". More... | |
int32_t | getPatternIndex (int32_t partIndex) const |
Returns the pattern index of the specified pattern "part". More... | |
UnicodeString | getSubstring (const Part &part) const |
Returns the substring of the pattern string indicated by the Part. More... | |
UBool | partSubstringMatches (const Part &part, const UnicodeString &s) const |
Compares the part's substring with the input string s. More... | |
double | getNumericValue (const Part &part) const |
Returns the numeric value associated with an ARG_INT or ARG_DOUBLE. More... | |
double | getPluralOffset (int32_t pluralStart) const |
Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified. More... | |
int32_t | getLimitPartIndex (int32_t start) const |
Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start. More... | |
Public Member Functions inherited from icu::UObject | |
virtual | ~UObject () |
Destructor. More... | |
virtual UClassID | getDynamicClassID () const |
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class. More... | |
Static Public Member Functions | |
static int32_t | validateArgumentName (const UnicodeString &name) |
Validates and parses an argument name or argument number string. More... | |
Parses and represents ICU MessageFormat patterns.
Also handles patterns for ChoiceFormat, PluralFormat and SelectFormat. Used in the implementations of those classes as well as in tools for message validation, translation and format conversion.
The parser handles all syntax relevant for identifying message arguments. This includes "complex" arguments whose style strings contain nested MessageFormat pattern substrings. For "simple" arguments (with no nested MessageFormat pattern substrings), the argument style is not parsed any further.
The parser handles named and numbered message arguments and allows both in one message.
Once a pattern has been parsed successfully, iterate through the parsed data with countParts(), getPart() and related methods.
The data logically represents a parse tree, but is stored and accessed as a list of "parts" for fast and simple parsing and to minimize object allocations. Arguments and nested messages are best handled via recursion. For every _START "part", MessagePattern.getLimitPartIndex() efficiently returns the index of the corresponding _LIMIT "part".
List of "parts":
message = MSG_START (SKIP_SYNTAX | INSERT_CHAR | REPLACE_NUMBER | argument)* MSG_LIMIT argument = noneArg | simpleArg | complexArg complexArg = choiceArg | pluralArg | selectArg
noneArg = ARG_START.NONE (ARG_NAME | ARG_NUMBER) ARG_LIMIT.NONE simpleArg = ARG_START.SIMPLE (ARG_NAME | ARG_NUMBER) ARG_TYPE [ARG_STYLE] ARG_LIMIT.SIMPLE choiceArg = ARG_START.CHOICE (ARG_NAME | ARG_NUMBER) choiceStyle ARG_LIMIT.CHOICE pluralArg = ARG_START.PLURAL (ARG_NAME | ARG_NUMBER) pluralStyle ARG_LIMIT.PLURAL selectArg = ARG_START.SELECT (ARG_NAME | ARG_NUMBER) selectStyle ARG_LIMIT.SELECT
choiceStyle = ((ARG_INT | ARG_DOUBLE) ARG_SELECTOR message)+ pluralStyle = [ARG_INT | ARG_DOUBLE] (ARG_SELECTOR [ARG_INT | ARG_DOUBLE] message)+ selectStyle = (ARG_SELECTOR message)+
ARG_START.CHOICE
stands for an ARG_START Part with ArgType CHOICE. This class is not intended for public subclassing.
Definition at line 362 of file messagepattern.h.
icu::MessagePattern::MessagePattern | ( | UErrorCode & | errorCode | ) |
Constructs an empty MessagePattern with default UMessagePatternApostropheMode.
errorCode | Standard ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
icu::MessagePattern::MessagePattern | ( | UMessagePatternApostropheMode | mode, |
UErrorCode & | errorCode | ||
) |
Constructs an empty MessagePattern.
mode | Explicit UMessagePatternApostropheMode. |
errorCode | Standard ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
icu::MessagePattern::MessagePattern | ( | const UnicodeString & | pattern, |
UParseError * | parseError, | ||
UErrorCode & | errorCode | ||
) |
Constructs a MessagePattern with default UMessagePatternApostropheMode and parses the MessageFormat pattern string.
pattern | a MessageFormat pattern string |
parseError | Struct to receive information on the position of an error within the pattern. Can be NULL. |
errorCode | Standard ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) TODO: turn |
into | UErrorCode specifics? |
IllegalArgumentException | for syntax errors in the pattern string |
IndexOutOfBoundsException | if certain limits are exceeded (e.g., argument number too high, argument name too long, etc.) |
NumberFormatException | if a number could not be parsed |
icu::MessagePattern::MessagePattern | ( | const MessagePattern & | other | ) |
|
virtual |
Destructor.
UnicodeString icu::MessagePattern::autoQuoteApostropheDeep | ( | ) | const |
Returns a version of the parsed pattern string where each ASCII apostrophe is doubled (escaped) if it is not already, and if it is not interpreted as quoting syntax.
For example, this turns "I don't '{know}' {gender,select,female{h''er}other{h'im}}." into "I don''t '{know}' {gender,select,female{h''er}other{h''im}}."
void icu::MessagePattern::clear | ( | ) |
|
inline |
Clears this MessagePattern and sets the UMessagePatternApostropheMode.
countParts() will return 0.
mode | The new UMessagePatternApostropheMode. |
Definition at line 519 of file messagepattern.h.
References icu::operator==().
|
inline |
Returns the number of "parts" created by parsing the pattern string.
Returns 0 if no pattern has been parsed or clear() was called.
Definition at line 616 of file messagepattern.h.
|
inline |
Definition at line 550 of file messagepattern.h.
|
inline |
Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.
start | The index of some Part data (0..countParts()-1); this Part should be of Type ARG_START or MSG_START. |
Definition at line 698 of file messagepattern.h.
double icu::MessagePattern::getNumericValue | ( | const Part & | part | ) | const |
Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.
part | a part of this MessagePattern. |
|
inline |
Gets the i-th pattern "part".
i | The index of the Part data. (0..countParts()-1) |
Definition at line 626 of file messagepattern.h.
|
inline |
Returns the UMessagePatternPartType of the i-th pattern "part".
Convenience method for getPart(i).getType().
i | The index of the Part data. (0..countParts()-1) |
Definition at line 637 of file messagepattern.h.
|
inline |
Returns the pattern index of the specified pattern "part".
Convenience method for getPart(partIndex).getIndex().
partIndex | The index of the Part data. (0..countParts()-1) |
Definition at line 648 of file messagepattern.h.
|
inline |
Definition at line 561 of file messagepattern.h.
double icu::MessagePattern::getPluralOffset | ( | int32_t | pluralStart | ) | const |
Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
pluralStart | the index of the first PluralFormat argument style part. (0..countParts()-1) |
|
inline |
Returns the substring of the pattern string indicated by the Part.
Convenience method for getPatternString().substring(part.getIndex(), part.getLimit()).
part | a part of this MessagePattern. |
Definition at line 659 of file messagepattern.h.
References icu::UnicodeString::tempSubString().
int32_t icu::MessagePattern::hashCode | ( | ) | const |
|
inline |
Does the parsed pattern have named arguments like {first_name}?
Definition at line 570 of file messagepattern.h.
|
inline |
Does the parsed pattern have numbered arguments like {2}?
Definition at line 579 of file messagepattern.h.
|
inline |
other | another object to compare with. |
Definition at line 536 of file messagepattern.h.
References icu::operator==().
MessagePattern& icu::MessagePattern::operator= | ( | const MessagePattern & | other | ) |
UBool icu::MessagePattern::operator== | ( | const MessagePattern & | other | ) | const |
other | another object to compare with. |
MessagePattern& icu::MessagePattern::parse | ( | const UnicodeString & | pattern, |
UParseError * | parseError, | ||
UErrorCode & | errorCode | ||
) |
Parses a MessageFormat pattern string.
pattern | a MessageFormat pattern string |
parseError | Struct to receive information on the position of an error within the pattern. Can be NULL. |
errorCode | Standard ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
IllegalArgumentException | for syntax errors in the pattern string |
IndexOutOfBoundsException | if certain limits are exceeded (e.g., argument number too high, argument name too long, etc.) |
NumberFormatException | if a number could not be parsed |
MessagePattern& icu::MessagePattern::parseChoiceStyle | ( | const UnicodeString & | pattern, |
UParseError * | parseError, | ||
UErrorCode & | errorCode | ||
) |
Parses a ChoiceFormat pattern string.
pattern | a ChoiceFormat pattern string |
parseError | Struct to receive information on the position of an error within the pattern. Can be NULL. |
errorCode | Standard ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
IllegalArgumentException | for syntax errors in the pattern string |
IndexOutOfBoundsException | if certain limits are exceeded (e.g., argument number too high, argument name too long, etc.) |
NumberFormatException | if a number could not be parsed |
MessagePattern& icu::MessagePattern::parsePluralStyle | ( | const UnicodeString & | pattern, |
UParseError * | parseError, | ||
UErrorCode & | errorCode | ||
) |
Parses a PluralFormat pattern string.
pattern | a PluralFormat pattern string |
parseError | Struct to receive information on the position of an error within the pattern. Can be NULL. |
errorCode | Standard ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
IllegalArgumentException | for syntax errors in the pattern string |
IndexOutOfBoundsException | if certain limits are exceeded (e.g., argument number too high, argument name too long, etc.) |
NumberFormatException | if a number could not be parsed |
MessagePattern& icu::MessagePattern::parseSelectStyle | ( | const UnicodeString & | pattern, |
UParseError * | parseError, | ||
UErrorCode & | errorCode | ||
) |
Parses a SelectFormat pattern string.
pattern | a SelectFormat pattern string |
parseError | Struct to receive information on the position of an error within the pattern. Can be NULL. |
errorCode | Standard ICU error code. Its input value must pass the U_SUCCESS() test, or else the function returns immediately. Check for U_FAILURE() on output or use with function chaining. (See User Guide for details.) |
IllegalArgumentException | for syntax errors in the pattern string |
IndexOutOfBoundsException | if certain limits are exceeded (e.g., argument number too high, argument name too long, etc.) |
NumberFormatException | if a number could not be parsed |
|
inline |
Compares the part's substring with the input string s.
part | a part of this MessagePattern. |
s | a string. |
Definition at line 670 of file messagepattern.h.
|
static |
Validates and parses an argument name or argument number string.
An argument name must be a "pattern identifier", that is, it must contain no Unicode Pattern_Syntax or Pattern_White_Space characters. If it only contains ASCII digits, then it must be a small integer with no leading zero.
name | Input string. |