1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2011-2013, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: messagepattern.h
10 * tab size: 8 (not used)
13 * created on: 2011mar14
14 * created by: Markus W. Scherer
17 #ifndef __MESSAGEPATTERN_H__
18 #define __MESSAGEPATTERN_H__
22 * \brief C++ API: MessagePattern class: Parses and represents ICU MessageFormat patterns.
25 #include "unicode/utypes.h"
27 #if !UCONFIG_NO_FORMATTING
29 #include "unicode/parseerr.h"
30 #include "unicode/unistr.h"
33 * Mode for when an apostrophe starts quoted literal text for MessageFormat output.
34 * The default is DOUBLE_OPTIONAL unless overridden via uconfig.h
35 * (UCONFIG_MSGPAT_DEFAULT_APOSTROPHE_MODE).
37 * A pair of adjacent apostrophes always results in a single apostrophe in the output,
38 * even when the pair is between two single, text-quoting apostrophes.
40 * The following table shows examples of desired MessageFormat.format() output
41 * with the pattern strings that yield that output.
45 * <th>Desired output</th>
46 * <th>DOUBLE_OPTIONAL</th>
47 * <th>DOUBLE_REQUIRED</th>
50 * <td>I see {many}</td>
51 * <td>I see '{many}'</td>
55 * <td>I said {'Wow!'}</td>
56 * <td>I said '{''Wow!''}'</td>
60 * <td>I don't know</td>
61 * <td>I don't know OR<br> I don''t know</td>
62 * <td>I don''t know</td>
66 * @see UCONFIG_MSGPAT_DEFAULT_APOSTROPHE_MODE
68 enum UMessagePatternApostropheMode
{
70 * A literal apostrophe is represented by
71 * either a single or a double apostrophe pattern character.
72 * Within a MessageFormat pattern, a single apostrophe only starts quoted literal text
73 * if it immediately precedes a curly brace {},
74 * or a pipe symbol | if inside a choice format,
75 * or a pound symbol # if inside a plural format.
77 * This is the default behavior starting with ICU 4.8.
80 UMSGPAT_APOS_DOUBLE_OPTIONAL
,
82 * A literal apostrophe must be represented by
83 * a double apostrophe pattern character.
84 * A single apostrophe always starts quoted literal text.
86 * This is the behavior of ICU 4.6 and earlier, and of the JDK.
89 UMSGPAT_APOS_DOUBLE_REQUIRED
94 typedef enum UMessagePatternApostropheMode UMessagePatternApostropheMode
;
97 * MessagePattern::Part type constants.
100 enum UMessagePatternPartType
{
102 * Start of a message pattern (main or nested).
103 * The length is 0 for the top-level message
104 * and for a choice argument sub-message, otherwise 1 for the '{'.
105 * The value indicates the nesting level, starting with 0 for the main message.
107 * There is always a later MSG_LIMIT part.
110 UMSGPAT_PART_TYPE_MSG_START
,
112 * End of a message pattern (main or nested).
113 * The length is 0 for the top-level message and
114 * the last sub-message of a choice argument,
115 * otherwise 1 for the '}' or (in a choice argument style) the '|'.
116 * The value indicates the nesting level, starting with 0 for the main message.
119 UMSGPAT_PART_TYPE_MSG_LIMIT
,
121 * Indicates a substring of the pattern string which is to be skipped when formatting.
122 * For example, an apostrophe that begins or ends quoted text
123 * would be indicated with such a part.
124 * The value is undefined and currently always 0.
127 UMSGPAT_PART_TYPE_SKIP_SYNTAX
,
129 * Indicates that a syntax character needs to be inserted for auto-quoting.
131 * The value is the character code of the insertion character. (U+0027=APOSTROPHE)
134 UMSGPAT_PART_TYPE_INSERT_CHAR
,
136 * Indicates a syntactic (non-escaped) # symbol in a plural variant.
137 * When formatting, replace this part's substring with the
138 * (value-offset) for the plural argument value.
139 * The value is undefined and currently always 0.
142 UMSGPAT_PART_TYPE_REPLACE_NUMBER
,
144 * Start of an argument.
145 * The length is 1 for the '{'.
146 * The value is the ordinal value of the ArgType. Use getArgType().
148 * This part is followed by either an ARG_NUMBER or ARG_NAME,
149 * followed by optional argument sub-parts (see UMessagePatternArgType constants)
150 * and finally an ARG_LIMIT part.
153 UMSGPAT_PART_TYPE_ARG_START
,
155 * End of an argument.
156 * The length is 1 for the '}'.
157 * The value is the ordinal value of the ArgType. Use getArgType().
160 UMSGPAT_PART_TYPE_ARG_LIMIT
,
162 * The argument number, provided by the value.
165 UMSGPAT_PART_TYPE_ARG_NUMBER
,
168 * The value is undefined and currently always 0.
171 UMSGPAT_PART_TYPE_ARG_NAME
,
174 * The value is undefined and currently always 0.
177 UMSGPAT_PART_TYPE_ARG_TYPE
,
179 * The argument style text.
180 * The value is undefined and currently always 0.
183 UMSGPAT_PART_TYPE_ARG_STYLE
,
185 * A selector substring in a "complex" argument style.
186 * The value is undefined and currently always 0.
189 UMSGPAT_PART_TYPE_ARG_SELECTOR
,
191 * An integer value, for example the offset or an explicit selector value
192 * in a PluralFormat style.
193 * The part value is the integer value.
196 UMSGPAT_PART_TYPE_ARG_INT
,
198 * A numeric value, for example the offset or an explicit selector value
199 * in a PluralFormat style.
200 * The part value is an index into an internal array of numeric values;
201 * use getNumericValue().
204 UMSGPAT_PART_TYPE_ARG_DOUBLE
209 typedef enum UMessagePatternPartType UMessagePatternPartType
;
212 * Argument type constants.
213 * Returned by Part.getArgType() for ARG_START and ARG_LIMIT parts.
215 * Messages nested inside an argument are each delimited by MSG_START and MSG_LIMIT,
216 * with a nesting level one greater than the surrounding message.
219 enum UMessagePatternArgType
{
221 * The argument has no specified type.
224 UMSGPAT_ARG_TYPE_NONE
,
226 * The argument has a "simple" type which is provided by the ARG_TYPE part.
227 * An ARG_STYLE part might follow that.
230 UMSGPAT_ARG_TYPE_SIMPLE
,
232 * The argument is a ChoiceFormat with one or more
233 * ((ARG_INT | ARG_DOUBLE), ARG_SELECTOR, message) tuples.
236 UMSGPAT_ARG_TYPE_CHOICE
,
238 * The argument is a cardinal-number PluralFormat with an optional ARG_INT or ARG_DOUBLE offset
240 * and one or more (ARG_SELECTOR [explicit-value] message) tuples.
241 * If the selector has an explicit value (e.g., =2), then
242 * that value is provided by the ARG_INT or ARG_DOUBLE part preceding the message.
243 * Otherwise the message immediately follows the ARG_SELECTOR.
246 UMSGPAT_ARG_TYPE_PLURAL
,
248 * The argument is a SelectFormat with one or more (ARG_SELECTOR, message) pairs.
251 UMSGPAT_ARG_TYPE_SELECT
,
253 * The argument is an ordinal-number PluralFormat
254 * with the same style parts sequence and semantics as UMSGPAT_ARG_TYPE_PLURAL.
257 UMSGPAT_ARG_TYPE_SELECTORDINAL
262 typedef enum UMessagePatternArgType UMessagePatternArgType
;
265 * \def UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE
266 * Returns TRUE if the argument type has a plural style part sequence and semantics,
267 * for example UMSGPAT_ARG_TYPE_PLURAL and UMSGPAT_ARG_TYPE_SELECTORDINAL.
270 #define UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) \
271 ((argType)==UMSGPAT_ARG_TYPE_PLURAL || (argType)==UMSGPAT_ARG_TYPE_SELECTORDINAL)
275 * Return value from MessagePattern.validateArgumentName() for when
276 * the string is a valid "pattern identifier" but not a number.
279 UMSGPAT_ARG_NAME_NOT_NUMBER
=-1,
282 * Return value from MessagePattern.validateArgumentName() for when
283 * the string is invalid.
284 * It might not be a valid "pattern identifier",
285 * or it have only ASCII digits but there is a leading zero or the number is too large.
288 UMSGPAT_ARG_NAME_NOT_VALID
=-2
292 * Special value that is returned by getNumericValue(Part) when no
293 * numeric value is defined for a part.
294 * @see MessagePattern.getNumericValue()
297 #define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789))
299 #if U_SHOW_CPLUSPLUS_API
302 class MessagePatternDoubleList
;
303 class MessagePatternPartsList
;
306 * Parses and represents ICU MessageFormat patterns.
307 * Also handles patterns for ChoiceFormat, PluralFormat and SelectFormat.
308 * Used in the implementations of those classes as well as in tools
309 * for message validation, translation and format conversion.
311 * The parser handles all syntax relevant for identifying message arguments.
312 * This includes "complex" arguments whose style strings contain
313 * nested MessageFormat pattern substrings.
314 * For "simple" arguments (with no nested MessageFormat pattern substrings),
315 * the argument style is not parsed any further.
317 * The parser handles named and numbered message arguments and allows both in one message.
319 * Once a pattern has been parsed successfully, iterate through the parsed data
320 * with countParts(), getPart() and related methods.
322 * The data logically represents a parse tree, but is stored and accessed
323 * as a list of "parts" for fast and simple parsing and to minimize object allocations.
324 * Arguments and nested messages are best handled via recursion.
325 * For every _START "part", MessagePattern.getLimitPartIndex() efficiently returns
326 * the index of the corresponding _LIMIT "part".
330 * message = MSG_START (SKIP_SYNTAX | INSERT_CHAR | REPLACE_NUMBER | argument)* MSG_LIMIT
331 * argument = noneArg | simpleArg | complexArg
332 * complexArg = choiceArg | pluralArg | selectArg
334 * noneArg = ARG_START.NONE (ARG_NAME | ARG_NUMBER) ARG_LIMIT.NONE
335 * simpleArg = ARG_START.SIMPLE (ARG_NAME | ARG_NUMBER) ARG_TYPE [ARG_STYLE] ARG_LIMIT.SIMPLE
336 * choiceArg = ARG_START.CHOICE (ARG_NAME | ARG_NUMBER) choiceStyle ARG_LIMIT.CHOICE
337 * pluralArg = ARG_START.PLURAL (ARG_NAME | ARG_NUMBER) pluralStyle ARG_LIMIT.PLURAL
338 * selectArg = ARG_START.SELECT (ARG_NAME | ARG_NUMBER) selectStyle ARG_LIMIT.SELECT
340 * choiceStyle = ((ARG_INT | ARG_DOUBLE) ARG_SELECTOR message)+
341 * pluralStyle = [ARG_INT | ARG_DOUBLE] (ARG_SELECTOR [ARG_INT | ARG_DOUBLE] message)+
342 * selectStyle = (ARG_SELECTOR message)+
345 * <li>Literal output text is not represented directly by "parts" but accessed
346 * between parts of a message, from one part's getLimit() to the next part's getIndex().
347 * <li><code>ARG_START.CHOICE</code> stands for an ARG_START Part with ArgType CHOICE.
348 * <li>In the choiceStyle, the ARG_SELECTOR has the '<', the '#' or
349 * the less-than-or-equal-to sign (U+2264).
350 * <li>In the pluralStyle, the first, optional numeric Part has the "offset:" value.
351 * The optional numeric Part between each (ARG_SELECTOR, message) pair
352 * is the value of an explicit-number selector like "=2",
353 * otherwise the selector is a non-numeric identifier.
354 * <li>The REPLACE_NUMBER Part can occur only in an immediate sub-message of the pluralStyle.
357 * This class is not intended for public subclassing.
361 class U_COMMON_API MessagePattern
: public UObject
{
364 * Constructs an empty MessagePattern with default UMessagePatternApostropheMode.
365 * @param errorCode Standard ICU error code. Its input value must
366 * pass the U_SUCCESS() test, or else the function returns
367 * immediately. Check for U_FAILURE() on output or use with
368 * function chaining. (See User Guide for details.)
371 MessagePattern(UErrorCode
&errorCode
);
374 * Constructs an empty MessagePattern.
375 * @param mode Explicit UMessagePatternApostropheMode.
376 * @param errorCode Standard ICU error code. Its input value must
377 * pass the U_SUCCESS() test, or else the function returns
378 * immediately. Check for U_FAILURE() on output or use with
379 * function chaining. (See User Guide for details.)
382 MessagePattern(UMessagePatternApostropheMode mode
, UErrorCode
&errorCode
);
385 * Constructs a MessagePattern with default UMessagePatternApostropheMode and
386 * parses the MessageFormat pattern string.
387 * @param pattern a MessageFormat pattern string
388 * @param parseError Struct to receive information on the position
389 * of an error within the pattern.
391 * @param errorCode Standard ICU error code. Its input value must
392 * pass the U_SUCCESS() test, or else the function returns
393 * immediately. Check for U_FAILURE() on output or use with
394 * function chaining. (See User Guide for details.)
395 * TODO: turn @throws into UErrorCode specifics?
396 * @throws IllegalArgumentException for syntax errors in the pattern string
397 * @throws IndexOutOfBoundsException if certain limits are exceeded
398 * (e.g., argument number too high, argument name too long, etc.)
399 * @throws NumberFormatException if a number could not be parsed
402 MessagePattern(const UnicodeString
&pattern
, UParseError
*parseError
, UErrorCode
&errorCode
);
406 * @param other Object to copy.
409 MessagePattern(const MessagePattern
&other
);
412 * Assignment operator.
413 * @param other Object to copy.
414 * @return *this=other
417 MessagePattern
&operator=(const MessagePattern
&other
);
423 virtual ~MessagePattern();
426 * Parses a MessageFormat pattern string.
427 * @param pattern a MessageFormat pattern string
428 * @param parseError Struct to receive information on the position
429 * of an error within the pattern.
431 * @param errorCode Standard ICU error code. Its input value must
432 * pass the U_SUCCESS() test, or else the function returns
433 * immediately. Check for U_FAILURE() on output or use with
434 * function chaining. (See User Guide for details.)
436 * @throws IllegalArgumentException for syntax errors in the pattern string
437 * @throws IndexOutOfBoundsException if certain limits are exceeded
438 * (e.g., argument number too high, argument name too long, etc.)
439 * @throws NumberFormatException if a number could not be parsed
442 MessagePattern
&parse(const UnicodeString
&pattern
,
443 UParseError
*parseError
, UErrorCode
&errorCode
);
446 * Parses a ChoiceFormat pattern string.
447 * @param pattern a ChoiceFormat pattern string
448 * @param parseError Struct to receive information on the position
449 * of an error within the pattern.
451 * @param errorCode Standard ICU error code. Its input value must
452 * pass the U_SUCCESS() test, or else the function returns
453 * immediately. Check for U_FAILURE() on output or use with
454 * function chaining. (See User Guide for details.)
456 * @throws IllegalArgumentException for syntax errors in the pattern string
457 * @throws IndexOutOfBoundsException if certain limits are exceeded
458 * (e.g., argument number too high, argument name too long, etc.)
459 * @throws NumberFormatException if a number could not be parsed
462 MessagePattern
&parseChoiceStyle(const UnicodeString
&pattern
,
463 UParseError
*parseError
, UErrorCode
&errorCode
);
466 * Parses a PluralFormat pattern string.
467 * @param pattern a PluralFormat pattern string
468 * @param parseError Struct to receive information on the position
469 * of an error within the pattern.
471 * @param errorCode Standard ICU error code. Its input value must
472 * pass the U_SUCCESS() test, or else the function returns
473 * immediately. Check for U_FAILURE() on output or use with
474 * function chaining. (See User Guide for details.)
476 * @throws IllegalArgumentException for syntax errors in the pattern string
477 * @throws IndexOutOfBoundsException if certain limits are exceeded
478 * (e.g., argument number too high, argument name too long, etc.)
479 * @throws NumberFormatException if a number could not be parsed
482 MessagePattern
&parsePluralStyle(const UnicodeString
&pattern
,
483 UParseError
*parseError
, UErrorCode
&errorCode
);
486 * Parses a SelectFormat pattern string.
487 * @param pattern a SelectFormat pattern string
488 * @param parseError Struct to receive information on the position
489 * of an error within the pattern.
491 * @param errorCode Standard ICU error code. Its input value must
492 * pass the U_SUCCESS() test, or else the function returns
493 * immediately. Check for U_FAILURE() on output or use with
494 * function chaining. (See User Guide for details.)
496 * @throws IllegalArgumentException for syntax errors in the pattern string
497 * @throws IndexOutOfBoundsException if certain limits are exceeded
498 * (e.g., argument number too high, argument name too long, etc.)
499 * @throws NumberFormatException if a number could not be parsed
502 MessagePattern
&parseSelectStyle(const UnicodeString
&pattern
,
503 UParseError
*parseError
, UErrorCode
&errorCode
);
506 * Clears this MessagePattern.
507 * countParts() will return 0.
513 * Clears this MessagePattern and sets the UMessagePatternApostropheMode.
514 * countParts() will return 0.
515 * @param mode The new UMessagePatternApostropheMode.
518 void clearPatternAndSetApostropheMode(UMessagePatternApostropheMode mode
) {
524 * @param other another object to compare with.
525 * @return TRUE if this object is equivalent to the other one.
528 UBool
operator==(const MessagePattern
&other
) const;
531 * @param other another object to compare with.
532 * @return FALSE if this object is equivalent to the other one.
535 inline UBool
operator!=(const MessagePattern
&other
) const {
536 return !operator==(other
);
540 * @return A hash code for this object.
543 int32_t hashCode() const;
546 * @return this instance's UMessagePatternApostropheMode.
549 UMessagePatternApostropheMode
getApostropheMode() const {
553 // Java has package-private jdkAposMode() here.
554 // In C++, this is declared in the MessageImpl class.
557 * @return the parsed pattern string (null if none was parsed).
560 const UnicodeString
&getPatternString() const {
565 * Does the parsed pattern have named arguments like {first_name}?
566 * @return TRUE if the parsed pattern has at least one named argument.
569 UBool
hasNamedArguments() const {
574 * Does the parsed pattern have numbered arguments like {2}?
575 * @return TRUE if the parsed pattern has at least one numbered argument.
578 UBool
hasNumberedArguments() const {
579 return hasArgNumbers
;
583 * Validates and parses an argument name or argument number string.
584 * An argument name must be a "pattern identifier", that is, it must contain
585 * no Unicode Pattern_Syntax or Pattern_White_Space characters.
586 * If it only contains ASCII digits, then it must be a small integer with no leading zero.
587 * @param name Input string.
588 * @return >=0 if the name is a valid number,
589 * ARG_NAME_NOT_NUMBER (-1) if it is a "pattern identifier" but not all ASCII digits,
590 * ARG_NAME_NOT_VALID (-2) if it is neither.
593 static int32_t validateArgumentName(const UnicodeString
&name
);
596 * Returns a version of the parsed pattern string where each ASCII apostrophe
597 * is doubled (escaped) if it is not already, and if it is not interpreted as quoting syntax.
599 * For example, this turns "I don't '{know}' {gender,select,female{h''er}other{h'im}}."
600 * into "I don''t '{know}' {gender,select,female{h''er}other{h''im}}."
601 * @return the deep-auto-quoted version of the parsed pattern string.
602 * @see MessageFormat.autoQuoteApostrophe()
605 UnicodeString
autoQuoteApostropheDeep() const;
610 * Returns the number of "parts" created by parsing the pattern string.
611 * Returns 0 if no pattern has been parsed or clear() was called.
612 * @return the number of pattern parts.
615 int32_t countParts() const {
620 * Gets the i-th pattern "part".
621 * @param i The index of the Part data. (0..countParts()-1)
622 * @return the i-th pattern "part".
625 const Part
&getPart(int32_t i
) const {
630 * Returns the UMessagePatternPartType of the i-th pattern "part".
631 * Convenience method for getPart(i).getType().
632 * @param i The index of the Part data. (0..countParts()-1)
633 * @return The UMessagePatternPartType of the i-th Part.
636 UMessagePatternPartType
getPartType(int32_t i
) const {
637 return getPart(i
).type
;
641 * Returns the pattern index of the specified pattern "part".
642 * Convenience method for getPart(partIndex).getIndex().
643 * @param partIndex The index of the Part data. (0..countParts()-1)
644 * @return The pattern index of this Part.
647 int32_t getPatternIndex(int32_t partIndex
) const {
648 return getPart(partIndex
).index
;
652 * Returns the substring of the pattern string indicated by the Part.
653 * Convenience method for getPatternString().substring(part.getIndex(), part.getLimit()).
654 * @param part a part of this MessagePattern.
655 * @return the substring associated with part.
658 UnicodeString
getSubstring(const Part
&part
) const {
659 return msg
.tempSubString(part
.index
, part
.length
);
663 * Compares the part's substring with the input string s.
664 * @param part a part of this MessagePattern.
666 * @return TRUE if getSubstring(part).equals(s).
669 UBool
partSubstringMatches(const Part
&part
, const UnicodeString
&s
) const {
670 return 0==msg
.compare(part
.index
, part
.length
, s
);
674 * Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.
675 * @param part a part of this MessagePattern.
676 * @return the part's numeric value, or UMSGPAT_NO_NUMERIC_VALUE if this is not a numeric part.
679 double getNumericValue(const Part
&part
) const;
682 * Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
683 * @param pluralStart the index of the first PluralFormat argument style part. (0..countParts()-1)
684 * @return the "offset:" value.
687 double getPluralOffset(int32_t pluralStart
) const;
690 * Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.
691 * @param start The index of some Part data (0..countParts()-1);
692 * this Part should be of Type ARG_START or MSG_START.
693 * @return The first i>start where getPart(i).getType()==ARG|MSG_LIMIT at the same nesting level,
694 * or start itself if getPartType(msgStart)!=ARG|MSG_START.
697 int32_t getLimitPartIndex(int32_t start
) const {
698 int32_t limit
=getPart(start
).limitPartIndex
;
706 * A message pattern "part", representing a pattern parsing event.
707 * There is a part for the start and end of a message or argument,
708 * for quoting and escaping of and with ASCII apostrophes,
709 * and for syntax elements of "complex" arguments.
712 class Part
: public UMemory
{
715 * Default constructor, do not use.
721 * Returns the type of this part.
722 * @return the part type.
725 UMessagePatternPartType
getType() const {
730 * Returns the pattern string index associated with this Part.
731 * @return this part's pattern string index.
734 int32_t getIndex() const {
739 * Returns the length of the pattern substring associated with this Part.
740 * This is 0 for some parts.
741 * @return this part's pattern substring length.
744 int32_t getLength() const {
749 * Returns the pattern string limit (exclusive-end) index associated with this Part.
750 * Convenience method for getIndex()+getLength().
751 * @return this part's pattern string limit index, same as getIndex()+getLength().
754 int32_t getLimit() const {
759 * Returns a value associated with this part.
760 * See the documentation of each part type for details.
761 * @return the part value.
764 int32_t getValue() const {
769 * Returns the argument type if this part is of type ARG_START or ARG_LIMIT,
770 * otherwise UMSGPAT_ARG_TYPE_NONE.
771 * @return the argument type for this part.
774 UMessagePatternArgType
getArgType() const {
775 UMessagePatternPartType msgType
=getType();
776 if(msgType
==UMSGPAT_PART_TYPE_ARG_START
|| msgType
==UMSGPAT_PART_TYPE_ARG_LIMIT
) {
777 return (UMessagePatternArgType
)value
;
779 return UMSGPAT_ARG_TYPE_NONE
;
784 * Indicates whether the Part type has a numeric value.
785 * If so, then that numeric value can be retrieved via MessagePattern.getNumericValue().
786 * @param type The Part type to be tested.
787 * @return TRUE if the Part type has a numeric value.
790 static UBool
hasNumericValue(UMessagePatternPartType type
) {
791 return type
==UMSGPAT_PART_TYPE_ARG_INT
|| type
==UMSGPAT_PART_TYPE_ARG_DOUBLE
;
795 * @param other another object to compare with.
796 * @return TRUE if this object is equivalent to the other one.
799 UBool
operator==(const Part
&other
) const;
802 * @param other another object to compare with.
803 * @return FALSE if this object is equivalent to the other one.
806 inline UBool
operator!=(const Part
&other
) const {
807 return !operator==(other
);
811 * @return A hash code for this object.
814 int32_t hashCode() const {
815 return ((type
*37+index
)*37+length
)*37+value
;
819 friend class MessagePattern
;
821 static const int32_t MAX_LENGTH
=0xffff;
822 static const int32_t MAX_VALUE
=0x7fff;
824 // Some fields are not final because they are modified during pattern parsing.
825 // After pattern parsing, the parts are effectively immutable.
826 UMessagePatternPartType type
;
830 int32_t limitPartIndex
;
834 void preParse(const UnicodeString
&pattern
, UParseError
*parseError
, UErrorCode
&errorCode
);
838 int32_t parseMessage(int32_t index
, int32_t msgStartLength
,
839 int32_t nestingLevel
, UMessagePatternArgType parentType
,
840 UParseError
*parseError
, UErrorCode
&errorCode
);
842 int32_t parseArg(int32_t index
, int32_t argStartLength
, int32_t nestingLevel
,
843 UParseError
*parseError
, UErrorCode
&errorCode
);
845 int32_t parseSimpleStyle(int32_t index
, UParseError
*parseError
, UErrorCode
&errorCode
);
847 int32_t parseChoiceStyle(int32_t index
, int32_t nestingLevel
,
848 UParseError
*parseError
, UErrorCode
&errorCode
);
850 int32_t parsePluralOrSelectStyle(UMessagePatternArgType argType
, int32_t index
, int32_t nestingLevel
,
851 UParseError
*parseError
, UErrorCode
&errorCode
);
854 * Validates and parses an argument name or argument number string.
855 * This internal method assumes that the input substring is a "pattern identifier".
856 * @return >=0 if the name is a valid number,
857 * ARG_NAME_NOT_NUMBER (-1) if it is a "pattern identifier" but not all ASCII digits,
858 * ARG_NAME_NOT_VALID (-2) if it is neither.
859 * @see #validateArgumentName(String)
861 static int32_t parseArgNumber(const UnicodeString
&s
, int32_t start
, int32_t limit
);
863 int32_t parseArgNumber(int32_t start
, int32_t limit
) {
864 return parseArgNumber(msg
, start
, limit
);
868 * Parses a number from the specified message substring.
869 * @param start start index into the message string
870 * @param limit limit index into the message string, must be start<limit
871 * @param allowInfinity TRUE if U+221E is allowed (for ChoiceFormat)
875 void parseDouble(int32_t start
, int32_t limit
, UBool allowInfinity
,
876 UParseError
*parseError
, UErrorCode
&errorCode
);
878 // Java has package-private appendReducedApostrophes() here.
879 // In C++, this is declared in the MessageImpl class.
881 int32_t skipWhiteSpace(int32_t index
);
883 int32_t skipIdentifier(int32_t index
);
886 * Skips a sequence of characters that could occur in a double value.
887 * Does not fully parse or validate the value.
889 int32_t skipDouble(int32_t index
);
891 static UBool
isArgTypeChar(UChar32 c
);
893 UBool
isChoice(int32_t index
);
895 UBool
isPlural(int32_t index
);
897 UBool
isSelect(int32_t index
);
899 UBool
isOrdinal(int32_t index
);
902 * @return TRUE if we are inside a MessageFormat (sub-)pattern,
903 * as opposed to inside a top-level choice/plural/select pattern.
905 UBool
inMessageFormatPattern(int32_t nestingLevel
);
908 * @return TRUE if we are in a MessageFormat sub-pattern
909 * of a top-level ChoiceFormat pattern.
911 UBool
inTopLevelChoiceMessage(int32_t nestingLevel
, UMessagePatternArgType parentType
);
913 void addPart(UMessagePatternPartType type
, int32_t index
, int32_t length
,
914 int32_t value
, UErrorCode
&errorCode
);
916 void addLimitPart(int32_t start
,
917 UMessagePatternPartType type
, int32_t index
, int32_t length
,
918 int32_t value
, UErrorCode
&errorCode
);
920 void addArgDoublePart(double numericValue
, int32_t start
, int32_t length
, UErrorCode
&errorCode
);
922 void setParseError(UParseError
*parseError
, int32_t index
);
924 UBool
init(UErrorCode
&errorCode
);
925 UBool
copyStorage(const MessagePattern
&other
, UErrorCode
&errorCode
);
927 UMessagePatternApostropheMode aposMode
;
929 // ArrayList<Part> parts=new ArrayList<Part>();
930 MessagePatternPartsList
*partsList
;
933 // ArrayList<Double> numericValues;
934 MessagePatternDoubleList
*numericValuesList
;
935 double *numericValues
;
936 int32_t numericValuesLength
;
939 UBool needsAutoQuoting
;
943 #endif // U_SHOW_CPLUSPLUS_API
945 #endif // !UCONFIG_NO_FORMATTING
947 #endif // __MESSAGEPATTERN_H__