michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2011-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: messagepattern.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2011mar14 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #ifndef __MESSAGEPATTERN_H__ michael@0: #define __MESSAGEPATTERN_H__ michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: MessagePattern class: Parses and represents ICU MessageFormat patterns. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/parseerr.h" michael@0: #include "unicode/unistr.h" michael@0: michael@0: /** michael@0: * Mode for when an apostrophe starts quoted literal text for MessageFormat output. michael@0: * The default is DOUBLE_OPTIONAL unless overridden via uconfig.h michael@0: * (UCONFIG_MSGPAT_DEFAULT_APOSTROPHE_MODE). michael@0: *
michael@0: * A pair of adjacent apostrophes always results in a single apostrophe in the output, michael@0: * even when the pair is between two single, text-quoting apostrophes. michael@0: *
michael@0: * The following table shows examples of desired MessageFormat.format() output michael@0: * with the pattern strings that yield that output. michael@0: *
michael@0: *
Desired output | michael@0: *DOUBLE_OPTIONAL | michael@0: *DOUBLE_REQUIRED | michael@0: *
---|---|---|
I see {many} | michael@0: *I see '{many}' | michael@0: *(same) | michael@0: *
I said {'Wow!'} | michael@0: *I said '{''Wow!''}' | michael@0: *(same) | michael@0: *
I don't know | michael@0: *I don't know OR I don''t know |
michael@0: * I don''t know | michael@0: *
michael@0: * This is the default behavior starting with ICU 4.8. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_APOS_DOUBLE_OPTIONAL, michael@0: /** michael@0: * A literal apostrophe must be represented by michael@0: * a double apostrophe pattern character. michael@0: * A single apostrophe always starts quoted literal text. michael@0: *
michael@0: * This is the behavior of ICU 4.6 and earlier, and of the JDK. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_APOS_DOUBLE_REQUIRED michael@0: }; michael@0: /** michael@0: * @stable ICU 4.8 michael@0: */ michael@0: typedef enum UMessagePatternApostropheMode UMessagePatternApostropheMode; michael@0: michael@0: /** michael@0: * MessagePattern::Part type constants. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: enum UMessagePatternPartType { michael@0: /** michael@0: * Start of a message pattern (main or nested). michael@0: * The length is 0 for the top-level message michael@0: * and for a choice argument sub-message, otherwise 1 for the '{'. michael@0: * The value indicates the nesting level, starting with 0 for the main message. michael@0: *
michael@0: * There is always a later MSG_LIMIT part. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_MSG_START, michael@0: /** michael@0: * End of a message pattern (main or nested). michael@0: * The length is 0 for the top-level message and michael@0: * the last sub-message of a choice argument, michael@0: * otherwise 1 for the '}' or (in a choice argument style) the '|'. michael@0: * The value indicates the nesting level, starting with 0 for the main message. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_MSG_LIMIT, michael@0: /** michael@0: * Indicates a substring of the pattern string which is to be skipped when formatting. michael@0: * For example, an apostrophe that begins or ends quoted text michael@0: * would be indicated with such a part. michael@0: * The value is undefined and currently always 0. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_SKIP_SYNTAX, michael@0: /** michael@0: * Indicates that a syntax character needs to be inserted for auto-quoting. michael@0: * The length is 0. michael@0: * The value is the character code of the insertion character. (U+0027=APOSTROPHE) michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_INSERT_CHAR, michael@0: /** michael@0: * Indicates a syntactic (non-escaped) # symbol in a plural variant. michael@0: * When formatting, replace this part's substring with the michael@0: * (value-offset) for the plural argument value. michael@0: * The value is undefined and currently always 0. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_REPLACE_NUMBER, michael@0: /** michael@0: * Start of an argument. michael@0: * The length is 1 for the '{'. michael@0: * The value is the ordinal value of the ArgType. Use getArgType(). michael@0: *
michael@0: * This part is followed by either an ARG_NUMBER or ARG_NAME, michael@0: * followed by optional argument sub-parts (see UMessagePatternArgType constants) michael@0: * and finally an ARG_LIMIT part. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_ARG_START, michael@0: /** michael@0: * End of an argument. michael@0: * The length is 1 for the '}'. michael@0: * The value is the ordinal value of the ArgType. Use getArgType(). michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_ARG_LIMIT, michael@0: /** michael@0: * The argument number, provided by the value. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_ARG_NUMBER, michael@0: /** michael@0: * The argument name. michael@0: * The value is undefined and currently always 0. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_ARG_NAME, michael@0: /** michael@0: * The argument type. michael@0: * The value is undefined and currently always 0. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_ARG_TYPE, michael@0: /** michael@0: * The argument style text. michael@0: * The value is undefined and currently always 0. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_ARG_STYLE, michael@0: /** michael@0: * A selector substring in a "complex" argument style. michael@0: * The value is undefined and currently always 0. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_ARG_SELECTOR, michael@0: /** michael@0: * An integer value, for example the offset or an explicit selector value michael@0: * in a PluralFormat style. michael@0: * The part value is the integer value. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_ARG_INT, michael@0: /** michael@0: * A numeric value, for example the offset or an explicit selector value michael@0: * in a PluralFormat style. michael@0: * The part value is an index into an internal array of numeric values; michael@0: * use getNumericValue(). michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_PART_TYPE_ARG_DOUBLE michael@0: }; michael@0: /** michael@0: * @stable ICU 4.8 michael@0: */ michael@0: typedef enum UMessagePatternPartType UMessagePatternPartType; michael@0: michael@0: /** michael@0: * Argument type constants. michael@0: * Returned by Part.getArgType() for ARG_START and ARG_LIMIT parts. michael@0: * michael@0: * Messages nested inside an argument are each delimited by MSG_START and MSG_LIMIT, michael@0: * with a nesting level one greater than the surrounding message. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: enum UMessagePatternArgType { michael@0: /** michael@0: * The argument has no specified type. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_ARG_TYPE_NONE, michael@0: /** michael@0: * The argument has a "simple" type which is provided by the ARG_TYPE part. michael@0: * An ARG_STYLE part might follow that. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_ARG_TYPE_SIMPLE, michael@0: /** michael@0: * The argument is a ChoiceFormat with one or more michael@0: * ((ARG_INT | ARG_DOUBLE), ARG_SELECTOR, message) tuples. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_ARG_TYPE_CHOICE, michael@0: /** michael@0: * The argument is a cardinal-number PluralFormat with an optional ARG_INT or ARG_DOUBLE offset michael@0: * (e.g., offset:1) michael@0: * and one or more (ARG_SELECTOR [explicit-value] message) tuples. michael@0: * If the selector has an explicit value (e.g., =2), then michael@0: * that value is provided by the ARG_INT or ARG_DOUBLE part preceding the message. michael@0: * Otherwise the message immediately follows the ARG_SELECTOR. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_ARG_TYPE_PLURAL, michael@0: /** michael@0: * The argument is a SelectFormat with one or more (ARG_SELECTOR, message) pairs. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_ARG_TYPE_SELECT, michael@0: /** michael@0: * The argument is an ordinal-number PluralFormat michael@0: * with the same style parts sequence and semantics as UMSGPAT_ARG_TYPE_PLURAL. michael@0: * @stable ICU 50 michael@0: */ michael@0: UMSGPAT_ARG_TYPE_SELECTORDINAL michael@0: }; michael@0: /** michael@0: * @stable ICU 4.8 michael@0: */ michael@0: typedef enum UMessagePatternArgType UMessagePatternArgType; michael@0: michael@0: /** michael@0: * \def UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE michael@0: * Returns TRUE if the argument type has a plural style part sequence and semantics, michael@0: * for example UMSGPAT_ARG_TYPE_PLURAL and UMSGPAT_ARG_TYPE_SELECTORDINAL. michael@0: * @stable ICU 50 michael@0: */ michael@0: #define UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) \ michael@0: ((argType)==UMSGPAT_ARG_TYPE_PLURAL || (argType)==UMSGPAT_ARG_TYPE_SELECTORDINAL) michael@0: michael@0: enum { michael@0: /** michael@0: * Return value from MessagePattern.validateArgumentName() for when michael@0: * the string is a valid "pattern identifier" but not a number. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_ARG_NAME_NOT_NUMBER=-1, michael@0: michael@0: /** michael@0: * Return value from MessagePattern.validateArgumentName() for when michael@0: * the string is invalid. michael@0: * It might not be a valid "pattern identifier", michael@0: * or it have only ASCII digits but there is a leading zero or the number is too large. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMSGPAT_ARG_NAME_NOT_VALID=-2 michael@0: }; michael@0: michael@0: /** michael@0: * Special value that is returned by getNumericValue(Part) when no michael@0: * numeric value is defined for a part. michael@0: * @see MessagePattern.getNumericValue() michael@0: * @stable ICU 4.8 michael@0: */ michael@0: #define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789)) michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class MessagePatternDoubleList; michael@0: class MessagePatternPartsList; michael@0: michael@0: /** michael@0: * Parses and represents ICU MessageFormat patterns. michael@0: * Also handles patterns for ChoiceFormat, PluralFormat and SelectFormat. michael@0: * Used in the implementations of those classes as well as in tools michael@0: * for message validation, translation and format conversion. michael@0: *
michael@0: * The parser handles all syntax relevant for identifying message arguments. michael@0: * This includes "complex" arguments whose style strings contain michael@0: * nested MessageFormat pattern substrings. michael@0: * For "simple" arguments (with no nested MessageFormat pattern substrings), michael@0: * the argument style is not parsed any further. michael@0: *
michael@0: * The parser handles named and numbered message arguments and allows both in one message. michael@0: *
michael@0: * Once a pattern has been parsed successfully, iterate through the parsed data michael@0: * with countParts(), getPart() and related methods. michael@0: *
michael@0: * The data logically represents a parse tree, but is stored and accessed michael@0: * as a list of "parts" for fast and simple parsing and to minimize object allocations. michael@0: * Arguments and nested messages are best handled via recursion. michael@0: * For every _START "part", MessagePattern.getLimitPartIndex() efficiently returns michael@0: * the index of the corresponding _LIMIT "part". michael@0: *
michael@0: * List of "parts": michael@0: *
michael@0: * message = MSG_START (SKIP_SYNTAX | INSERT_CHAR | REPLACE_NUMBER | argument)* MSG_LIMIT michael@0: * argument = noneArg | simpleArg | complexArg michael@0: * complexArg = choiceArg | pluralArg | selectArg michael@0: * michael@0: * noneArg = ARG_START.NONE (ARG_NAME | ARG_NUMBER) ARG_LIMIT.NONE michael@0: * simpleArg = ARG_START.SIMPLE (ARG_NAME | ARG_NUMBER) ARG_TYPE [ARG_STYLE] ARG_LIMIT.SIMPLE michael@0: * choiceArg = ARG_START.CHOICE (ARG_NAME | ARG_NUMBER) choiceStyle ARG_LIMIT.CHOICE michael@0: * pluralArg = ARG_START.PLURAL (ARG_NAME | ARG_NUMBER) pluralStyle ARG_LIMIT.PLURAL michael@0: * selectArg = ARG_START.SELECT (ARG_NAME | ARG_NUMBER) selectStyle ARG_LIMIT.SELECT michael@0: * michael@0: * choiceStyle = ((ARG_INT | ARG_DOUBLE) ARG_SELECTOR message)+ michael@0: * pluralStyle = [ARG_INT | ARG_DOUBLE] (ARG_SELECTOR [ARG_INT | ARG_DOUBLE] message)+ michael@0: * selectStyle = (ARG_SELECTOR message)+ michael@0: *michael@0: *
ARG_START.CHOICE
stands for an ARG_START Part with ArgType CHOICE.
michael@0: * michael@0: * This class is not intended for public subclassing. michael@0: * michael@0: * @stable ICU 4.8 michael@0: */ michael@0: class U_COMMON_API MessagePattern : public UObject { michael@0: public: michael@0: /** michael@0: * Constructs an empty MessagePattern with default UMessagePatternApostropheMode. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * @stable ICU 4.8 michael@0: */ michael@0: MessagePattern(UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Constructs an empty MessagePattern. michael@0: * @param mode Explicit UMessagePatternApostropheMode. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * @stable ICU 4.8 michael@0: */ michael@0: MessagePattern(UMessagePatternApostropheMode mode, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Constructs a MessagePattern with default UMessagePatternApostropheMode and michael@0: * parses the MessageFormat pattern string. michael@0: * @param pattern a MessageFormat pattern string michael@0: * @param parseError Struct to receive information on the position michael@0: * of an error within the pattern. michael@0: * Can be NULL. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * TODO: turn @throws into UErrorCode specifics? michael@0: * @throws IllegalArgumentException for syntax errors in the pattern string michael@0: * @throws IndexOutOfBoundsException if certain limits are exceeded michael@0: * (e.g., argument number too high, argument name too long, etc.) michael@0: * @throws NumberFormatException if a number could not be parsed michael@0: * @stable ICU 4.8 michael@0: */ michael@0: MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Copy constructor. michael@0: * @param other Object to copy. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: MessagePattern(const MessagePattern &other); michael@0: michael@0: /** michael@0: * Assignment operator. michael@0: * @param other Object to copy. michael@0: * @return *this=other michael@0: * @stable ICU 4.8 michael@0: */ michael@0: MessagePattern &operator=(const MessagePattern &other); michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual ~MessagePattern(); michael@0: michael@0: /** michael@0: * Parses a MessageFormat pattern string. michael@0: * @param pattern a MessageFormat pattern string michael@0: * @param parseError Struct to receive information on the position michael@0: * of an error within the pattern. michael@0: * Can be NULL. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * @return *this michael@0: * @throws IllegalArgumentException for syntax errors in the pattern string michael@0: * @throws IndexOutOfBoundsException if certain limits are exceeded michael@0: * (e.g., argument number too high, argument name too long, etc.) michael@0: * @throws NumberFormatException if a number could not be parsed michael@0: * @stable ICU 4.8 michael@0: */ michael@0: MessagePattern &parse(const UnicodeString &pattern, michael@0: UParseError *parseError, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Parses a ChoiceFormat pattern string. michael@0: * @param pattern a ChoiceFormat pattern string michael@0: * @param parseError Struct to receive information on the position michael@0: * of an error within the pattern. michael@0: * Can be NULL. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * @return *this michael@0: * @throws IllegalArgumentException for syntax errors in the pattern string michael@0: * @throws IndexOutOfBoundsException if certain limits are exceeded michael@0: * (e.g., argument number too high, argument name too long, etc.) michael@0: * @throws NumberFormatException if a number could not be parsed michael@0: * @stable ICU 4.8 michael@0: */ michael@0: MessagePattern &parseChoiceStyle(const UnicodeString &pattern, michael@0: UParseError *parseError, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Parses a PluralFormat pattern string. michael@0: * @param pattern a PluralFormat pattern string michael@0: * @param parseError Struct to receive information on the position michael@0: * of an error within the pattern. michael@0: * Can be NULL. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * @return *this michael@0: * @throws IllegalArgumentException for syntax errors in the pattern string michael@0: * @throws IndexOutOfBoundsException if certain limits are exceeded michael@0: * (e.g., argument number too high, argument name too long, etc.) michael@0: * @throws NumberFormatException if a number could not be parsed michael@0: * @stable ICU 4.8 michael@0: */ michael@0: MessagePattern &parsePluralStyle(const UnicodeString &pattern, michael@0: UParseError *parseError, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Parses a SelectFormat pattern string. michael@0: * @param pattern a SelectFormat pattern string michael@0: * @param parseError Struct to receive information on the position michael@0: * of an error within the pattern. michael@0: * Can be NULL. michael@0: * @param errorCode Standard ICU error code. Its input value must michael@0: * pass the U_SUCCESS() test, or else the function returns michael@0: * immediately. Check for U_FAILURE() on output or use with michael@0: * function chaining. (See User Guide for details.) michael@0: * @return *this michael@0: * @throws IllegalArgumentException for syntax errors in the pattern string michael@0: * @throws IndexOutOfBoundsException if certain limits are exceeded michael@0: * (e.g., argument number too high, argument name too long, etc.) michael@0: * @throws NumberFormatException if a number could not be parsed michael@0: * @stable ICU 4.8 michael@0: */ michael@0: MessagePattern &parseSelectStyle(const UnicodeString &pattern, michael@0: UParseError *parseError, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Clears this MessagePattern. michael@0: * countParts() will return 0. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: void clear(); michael@0: michael@0: /** michael@0: * Clears this MessagePattern and sets the UMessagePatternApostropheMode. michael@0: * countParts() will return 0. michael@0: * @param mode The new UMessagePatternApostropheMode. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: void clearPatternAndSetApostropheMode(UMessagePatternApostropheMode mode) { michael@0: clear(); michael@0: aposMode=mode; michael@0: } michael@0: michael@0: /** michael@0: * @param other another object to compare with. michael@0: * @return TRUE if this object is equivalent to the other one. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UBool operator==(const MessagePattern &other) const; michael@0: michael@0: /** michael@0: * @param other another object to compare with. michael@0: * @return FALSE if this object is equivalent to the other one. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: inline UBool operator!=(const MessagePattern &other) const { michael@0: return !operator==(other); michael@0: } michael@0: michael@0: /** michael@0: * @return A hash code for this object. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: int32_t hashCode() const; michael@0: michael@0: /** michael@0: * @return this instance's UMessagePatternApostropheMode. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UMessagePatternApostropheMode getApostropheMode() const { michael@0: return aposMode; michael@0: } michael@0: michael@0: // Java has package-private jdkAposMode() here. michael@0: // In C++, this is declared in the MessageImpl class. michael@0: michael@0: /** michael@0: * @return the parsed pattern string (null if none was parsed). michael@0: * @stable ICU 4.8 michael@0: */ michael@0: const UnicodeString &getPatternString() const { michael@0: return msg; michael@0: } michael@0: michael@0: /** michael@0: * Does the parsed pattern have named arguments like {first_name}? michael@0: * @return TRUE if the parsed pattern has at least one named argument. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UBool hasNamedArguments() const { michael@0: return hasArgNames; michael@0: } michael@0: michael@0: /** michael@0: * Does the parsed pattern have numbered arguments like {2}? michael@0: * @return TRUE if the parsed pattern has at least one numbered argument. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: UBool hasNumberedArguments() const { michael@0: return hasArgNumbers; michael@0: } michael@0: michael@0: /** michael@0: * Validates and parses an argument name or argument number string. michael@0: * An argument name must be a "pattern identifier", that is, it must contain michael@0: * no Unicode Pattern_Syntax or Pattern_White_Space characters. michael@0: * If it only contains ASCII digits, then it must be a small integer with no leading zero. michael@0: * @param name Input string. michael@0: * @return >=0 if the name is a valid number, michael@0: * ARG_NAME_NOT_NUMBER (-1) if it is a "pattern identifier" but not all ASCII digits, michael@0: * ARG_NAME_NOT_VALID (-2) if it is neither. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: static int32_t validateArgumentName(const UnicodeString &name); michael@0: michael@0: /** michael@0: * Returns a version of the parsed pattern string where each ASCII apostrophe michael@0: * is doubled (escaped) if it is not already, and if it is not interpreted as quoting syntax. michael@0: *
michael@0: * For example, this turns "I don't '{know}' {gender,select,female{h''er}other{h'im}}."
michael@0: * into "I don''t '{know}' {gender,select,female{h''er}other{h''im}}."
michael@0: * @return the deep-auto-quoted version of the parsed pattern string.
michael@0: * @see MessageFormat.autoQuoteApostrophe()
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: UnicodeString autoQuoteApostropheDeep() const;
michael@0:
michael@0: class Part;
michael@0:
michael@0: /**
michael@0: * Returns the number of "parts" created by parsing the pattern string.
michael@0: * Returns 0 if no pattern has been parsed or clear() was called.
michael@0: * @return the number of pattern parts.
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: int32_t countParts() const {
michael@0: return partsLength;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Gets the i-th pattern "part".
michael@0: * @param i The index of the Part data. (0..countParts()-1)
michael@0: * @return the i-th pattern "part".
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: const Part &getPart(int32_t i) const {
michael@0: return parts[i];
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the UMessagePatternPartType of the i-th pattern "part".
michael@0: * Convenience method for getPart(i).getType().
michael@0: * @param i The index of the Part data. (0..countParts()-1)
michael@0: * @return The UMessagePatternPartType of the i-th Part.
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: UMessagePatternPartType getPartType(int32_t i) const {
michael@0: return getPart(i).type;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the pattern index of the specified pattern "part".
michael@0: * Convenience method for getPart(partIndex).getIndex().
michael@0: * @param partIndex The index of the Part data. (0..countParts()-1)
michael@0: * @return The pattern index of this Part.
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: int32_t getPatternIndex(int32_t partIndex) const {
michael@0: return getPart(partIndex).index;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the substring of the pattern string indicated by the Part.
michael@0: * Convenience method for getPatternString().substring(part.getIndex(), part.getLimit()).
michael@0: * @param part a part of this MessagePattern.
michael@0: * @return the substring associated with part.
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: UnicodeString getSubstring(const Part &part) const {
michael@0: return msg.tempSubString(part.index, part.length);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Compares the part's substring with the input string s.
michael@0: * @param part a part of this MessagePattern.
michael@0: * @param s a string.
michael@0: * @return TRUE if getSubstring(part).equals(s).
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: UBool partSubstringMatches(const Part &part, const UnicodeString &s) const {
michael@0: return 0==msg.compare(part.index, part.length, s);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.
michael@0: * @param part a part of this MessagePattern.
michael@0: * @return the part's numeric value, or UMSGPAT_NO_NUMERIC_VALUE if this is not a numeric part.
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: double getNumericValue(const Part &part) const;
michael@0:
michael@0: /**
michael@0: * Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
michael@0: * @param pluralStart the index of the first PluralFormat argument style part. (0..countParts()-1)
michael@0: * @return the "offset:" value.
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: double getPluralOffset(int32_t pluralStart) const;
michael@0:
michael@0: /**
michael@0: * Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.
michael@0: * @param start The index of some Part data (0..countParts()-1);
michael@0: * this Part should be of Type ARG_START or MSG_START.
michael@0: * @return The first i>start where getPart(i).getType()==ARG|MSG_LIMIT at the same nesting level,
michael@0: * or start itself if getPartType(msgStart)!=ARG|MSG_START.
michael@0: * @stable ICU 4.8
michael@0: */
michael@0: int32_t getLimitPartIndex(int32_t start) const {
michael@0: int32_t limit=getPart(start).limitPartIndex;
michael@0: if(limit