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 U_SHOW_CPLUSPLUS_API
29 #if !UCONFIG_NO_FORMATTING
31 #include "unicode/parseerr.h"
32 #include "unicode/unistr.h"
35 * Mode for when an apostrophe starts quoted literal text for MessageFormat output.
36 * The default is DOUBLE_OPTIONAL unless overridden via uconfig.h
37 * (UCONFIG_MSGPAT_DEFAULT_APOSTROPHE_MODE).
39 * A pair of adjacent apostrophes always results in a single apostrophe in the output,
40 * even when the pair is between two single, text-quoting apostrophes.
42 * The following table shows examples of desired MessageFormat.format() output
43 * with the pattern strings that yield that output.
47 * <th>Desired output</th>
48 * <th>DOUBLE_OPTIONAL</th>
49 * <th>DOUBLE_REQUIRED</th>
52 * <td>I see {many}</td>
53 * <td>I see '{many}'</td>
57 * <td>I said {'Wow!'}</td>
58 * <td>I said '{''Wow!''}'</td>
62 * <td>I don't know</td>
63 * <td>I don't know OR<br> I don''t know</td>
64 * <td>I don''t know</td>
68 * @see UCONFIG_MSGPAT_DEFAULT_APOSTROPHE_MODE
70 enum UMessagePatternApostropheMode
{
72 * A literal apostrophe is represented by
73 * either a single or a double apostrophe pattern character.
74 * Within a MessageFormat pattern, a single apostrophe only starts quoted literal text
75 * if it immediately precedes a curly brace {},
76 * or a pipe symbol | if inside a choice format,
77 * or a pound symbol # if inside a plural format.
79 * This is the default behavior starting with ICU 4.8.
82 UMSGPAT_APOS_DOUBLE_OPTIONAL
,
84 * A literal apostrophe must be represented by
85 * a double apostrophe pattern character.
86 * A single apostrophe always starts quoted literal text.
88 * This is the behavior of ICU 4.6 and earlier, and of the JDK.
91 UMSGPAT_APOS_DOUBLE_REQUIRED
96 typedef enum UMessagePatternApostropheMode UMessagePatternApostropheMode
;
99 * MessagePattern::Part type constants.
102 enum UMessagePatternPartType
{
104 * Start of a message pattern (main or nested).
105 * The length is 0 for the top-level message
106 * and for a choice argument sub-message, otherwise 1 for the '{'.
107 * The value indicates the nesting level, starting with 0 for the main message.
109 * There is always a later MSG_LIMIT part.
112 UMSGPAT_PART_TYPE_MSG_START
,
114 * End of a message pattern (main or nested).
115 * The length is 0 for the top-level message and
116 * the last sub-message of a choice argument,
117 * otherwise 1 for the '}' or (in a choice argument style) the '|'.
118 * The value indicates the nesting level, starting with 0 for the main message.
121 UMSGPAT_PART_TYPE_MSG_LIMIT
,
123 * Indicates a substring of the pattern string which is to be skipped when formatting.
124 * For example, an apostrophe that begins or ends quoted text
125 * would be indicated with such a part.
126 * The value is undefined and currently always 0.
129 UMSGPAT_PART_TYPE_SKIP_SYNTAX
,
131 * Indicates that a syntax character needs to be inserted for auto-quoting.
133 * The value is the character code of the insertion character. (U+0027=APOSTROPHE)
136 UMSGPAT_PART_TYPE_INSERT_CHAR
,
138 * Indicates a syntactic (non-escaped) # symbol in a plural variant.
139 * When formatting, replace this part's substring with the
140 * (value-offset) for the plural argument value.
141 * The value is undefined and currently always 0.
144 UMSGPAT_PART_TYPE_REPLACE_NUMBER
,
146 * Start of an argument.
147 * The length is 1 for the '{'.
148 * The value is the ordinal value of the ArgType. Use getArgType().
150 * This part is followed by either an ARG_NUMBER or ARG_NAME,
151 * followed by optional argument sub-parts (see UMessagePatternArgType constants)
152 * and finally an ARG_LIMIT part.
155 UMSGPAT_PART_TYPE_ARG_START
,
157 * End of an argument.
158 * The length is 1 for the '}'.
159 * The value is the ordinal value of the ArgType. Use getArgType().
162 UMSGPAT_PART_TYPE_ARG_LIMIT
,
164 * The argument number, provided by the value.
167 UMSGPAT_PART_TYPE_ARG_NUMBER
,
170 * The value is undefined and currently always 0.
173 UMSGPAT_PART_TYPE_ARG_NAME
,
176 * The value is undefined and currently always 0.
179 UMSGPAT_PART_TYPE_ARG_TYPE
,
181 * The argument style text.
182 * The value is undefined and currently always 0.
185 UMSGPAT_PART_TYPE_ARG_STYLE
,
187 * A selector substring in a "complex" argument style.
188 * The value is undefined and currently always 0.
191 UMSGPAT_PART_TYPE_ARG_SELECTOR
,
193 * An integer value, for example the offset or an explicit selector value
194 * in a PluralFormat style.
195 * The part value is the integer value.
198 UMSGPAT_PART_TYPE_ARG_INT
,
200 * A numeric value, for example the offset or an explicit selector value
201 * in a PluralFormat style.
202 * The part value is an index into an internal array of numeric values;
203 * use getNumericValue().
206 UMSGPAT_PART_TYPE_ARG_DOUBLE
211 typedef enum UMessagePatternPartType UMessagePatternPartType
;
214 * Argument type constants.
215 * Returned by Part.getArgType() for ARG_START and ARG_LIMIT parts.
217 * Messages nested inside an argument are each delimited by MSG_START and MSG_LIMIT,
218 * with a nesting level one greater than the surrounding message.
221 enum UMessagePatternArgType
{
223 * The argument has no specified type.
226 UMSGPAT_ARG_TYPE_NONE
,
228 * The argument has a "simple" type which is provided by the ARG_TYPE part.
229 * An ARG_STYLE part might follow that.
232 UMSGPAT_ARG_TYPE_SIMPLE
,
234 * The argument is a ChoiceFormat with one or more
235 * ((ARG_INT | ARG_DOUBLE), ARG_SELECTOR, message) tuples.
238 UMSGPAT_ARG_TYPE_CHOICE
,
240 * The argument is a cardinal-number PluralFormat with an optional ARG_INT or ARG_DOUBLE offset
242 * and one or more (ARG_SELECTOR [explicit-value] message) tuples.
243 * If the selector has an explicit value (e.g., =2), then
244 * that value is provided by the ARG_INT or ARG_DOUBLE part preceding the message.
245 * Otherwise the message immediately follows the ARG_SELECTOR.
248 UMSGPAT_ARG_TYPE_PLURAL
,
250 * The argument is a SelectFormat with one or more (ARG_SELECTOR, message) pairs.
253 UMSGPAT_ARG_TYPE_SELECT
,
255 * The argument is an ordinal-number PluralFormat
256 * with the same style parts sequence and semantics as UMSGPAT_ARG_TYPE_PLURAL.
259 UMSGPAT_ARG_TYPE_SELECTORDINAL
264 typedef enum UMessagePatternArgType UMessagePatternArgType
;
267 * \def UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE
268 * Returns TRUE if the argument type has a plural style part sequence and semantics,
269 * for example UMSGPAT_ARG_TYPE_PLURAL and UMSGPAT_ARG_TYPE_SELECTORDINAL.
272 #define UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) \
273 ((argType)==UMSGPAT_ARG_TYPE_PLURAL || (argType)==UMSGPAT_ARG_TYPE_SELECTORDINAL)
277 * Return value from MessagePattern.validateArgumentName() for when
278 * the string is a valid "pattern identifier" but not a number.
281 UMSGPAT_ARG_NAME_NOT_NUMBER
=-1,
284 * Return value from MessagePattern.validateArgumentName() for when
285 * the string is invalid.
286 * It might not be a valid "pattern identifier",
287 * or it have only ASCII digits but there is a leading zero or the number is too large.
290 UMSGPAT_ARG_NAME_NOT_VALID
=-2
294 * Special value that is returned by getNumericValue(Part) when no
295 * numeric value is defined for a part.
296 * @see MessagePattern.getNumericValue()
299 #define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789))
303 class MessagePatternDoubleList
;
304 class MessagePatternPartsList
;
307 * Parses and represents ICU MessageFormat patterns.
308 * Also handles patterns for ChoiceFormat, PluralFormat and SelectFormat.
309 * Used in the implementations of those classes as well as in tools
310 * for message validation, translation and format conversion.
312 * The parser handles all syntax relevant for identifying message arguments.
313 * This includes "complex" arguments whose style strings contain
314 * nested MessageFormat pattern substrings.
315 * For "simple" arguments (with no nested MessageFormat pattern substrings),
316 * the argument style is not parsed any further.
318 * The parser handles named and numbered message arguments and allows both in one message.
320 * Once a pattern has been parsed successfully, iterate through the parsed data
321 * with countParts(), getPart() and related methods.
323 * The data logically represents a parse tree, but is stored and accessed
324 * as a list of "parts" for fast and simple parsing and to minimize object allocations.
325 * Arguments and nested messages are best handled via recursion.
326 * For every _START "part", MessagePattern.getLimitPartIndex() efficiently returns
327 * the index of the corresponding _LIMIT "part".
331 * message = MSG_START (SKIP_SYNTAX | INSERT_CHAR | REPLACE_NUMBER | argument)* MSG_LIMIT
332 * argument = noneArg | simpleArg | complexArg
333 * complexArg = choiceArg | pluralArg | selectArg
335 * noneArg = ARG_START.NONE (ARG_NAME | ARG_NUMBER) ARG_LIMIT.NONE
336 * simpleArg = ARG_START.SIMPLE (ARG_NAME | ARG_NUMBER) ARG_TYPE [ARG_STYLE] ARG_LIMIT.SIMPLE
337 * choiceArg = ARG_START.CHOICE (ARG_NAME | ARG_NUMBER) choiceStyle ARG_LIMIT.CHOICE
338 * pluralArg = ARG_START.PLURAL (ARG_NAME | ARG_NUMBER) pluralStyle ARG_LIMIT.PLURAL
339 * selectArg = ARG_START.SELECT (ARG_NAME | ARG_NUMBER) selectStyle ARG_LIMIT.SELECT
341 * choiceStyle = ((ARG_INT | ARG_DOUBLE) ARG_SELECTOR message)+
342 * pluralStyle = [ARG_INT | ARG_DOUBLE] (ARG_SELECTOR [ARG_INT | ARG_DOUBLE] message)+
343 * selectStyle = (ARG_SELECTOR message)+
346 * <li>Literal output text is not represented directly by "parts" but accessed
347 * between parts of a message, from one part's getLimit() to the next part's getIndex().
348 * <li><code>ARG_START.CHOICE</code> stands for an ARG_START Part with ArgType CHOICE.
349 * <li>In the choiceStyle, the ARG_SELECTOR has the '<', the '#' or
350 * the less-than-or-equal-to sign (U+2264).
351 * <li>In the pluralStyle, the first, optional numeric Part has the "offset:" value.
352 * The optional numeric Part between each (ARG_SELECTOR, message) pair
353 * is the value of an explicit-number selector like "=2",
354 * otherwise the selector is a non-numeric identifier.
355 * <li>The REPLACE_NUMBER Part can occur only in an immediate sub-message of the pluralStyle.
358 * This class is not intended for public subclassing.
362 class U_COMMON_API MessagePattern
: public UObject
{
365 * Constructs an empty MessagePattern with default UMessagePatternApostropheMode.
366 * @param errorCode Standard ICU error code. Its input value must
367 * pass the U_SUCCESS() test, or else the function returns
368 * immediately. Check for U_FAILURE() on output or use with
369 * function chaining. (See User Guide for details.)
372 MessagePattern(UErrorCode
&errorCode
);
375 * Constructs an empty MessagePattern.
376 * @param mode Explicit UMessagePatternApostropheMode.
377 * @param errorCode Standard ICU error code. Its input value must
378 * pass the U_SUCCESS() test, or else the function returns
379 * immediately. Check for U_FAILURE() on output or use with
380 * function chaining. (See User Guide for details.)
383 MessagePattern(UMessagePatternApostropheMode mode
, UErrorCode
&errorCode
);
386 * Constructs a MessagePattern with default UMessagePatternApostropheMode and
387 * parses the MessageFormat pattern string.
388 * @param pattern a MessageFormat pattern string
389 * @param parseError Struct to receive information on the position
390 * of an error within the pattern.
392 * @param errorCode Standard ICU error code. Its input value must
393 * pass the U_SUCCESS() test, or else the function returns
394 * immediately. Check for U_FAILURE() on output or use with
395 * function chaining. (See User Guide for details.)
396 * TODO: turn @throws into UErrorCode specifics?
397 * @throws IllegalArgumentException for syntax errors in the pattern string
398 * @throws IndexOutOfBoundsException if certain limits are exceeded
399 * (e.g., argument number too high, argument name too long, etc.)
400 * @throws NumberFormatException if a number could not be parsed
403 MessagePattern(const UnicodeString
&pattern
, UParseError
*parseError
, UErrorCode
&errorCode
);
407 * @param other Object to copy.
410 MessagePattern(const MessagePattern
&other
);
413 * Assignment operator.
414 * @param other Object to copy.
415 * @return *this=other
418 MessagePattern
&operator=(const MessagePattern
&other
);
424 virtual ~MessagePattern();
427 * Parses a MessageFormat pattern string.
428 * @param pattern a MessageFormat pattern string
429 * @param parseError Struct to receive information on the position
430 * of an error within the pattern.
432 * @param errorCode Standard ICU error code. Its input value must
433 * pass the U_SUCCESS() test, or else the function returns
434 * immediately. Check for U_FAILURE() on output or use with
435 * function chaining. (See User Guide for details.)
437 * @throws IllegalArgumentException for syntax errors in the pattern string
438 * @throws IndexOutOfBoundsException if certain limits are exceeded
439 * (e.g., argument number too high, argument name too long, etc.)
440 * @throws NumberFormatException if a number could not be parsed
443 MessagePattern
&parse(const UnicodeString
&pattern
,
444 UParseError
*parseError
, UErrorCode
&errorCode
);
447 * Parses a ChoiceFormat pattern string.
448 * @param pattern a ChoiceFormat pattern string
449 * @param parseError Struct to receive information on the position
450 * of an error within the pattern.
452 * @param errorCode Standard ICU error code. Its input value must
453 * pass the U_SUCCESS() test, or else the function returns
454 * immediately. Check for U_FAILURE() on output or use with
455 * function chaining. (See User Guide for details.)
457 * @throws IllegalArgumentException for syntax errors in the pattern string
458 * @throws IndexOutOfBoundsException if certain limits are exceeded
459 * (e.g., argument number too high, argument name too long, etc.)
460 * @throws NumberFormatException if a number could not be parsed
463 MessagePattern
&parseChoiceStyle(const UnicodeString
&pattern
,
464 UParseError
*parseError
, UErrorCode
&errorCode
);
467 * Parses a PluralFormat pattern string.
468 * @param pattern a PluralFormat pattern string
469 * @param parseError Struct to receive information on the position
470 * of an error within the pattern.
472 * @param errorCode Standard ICU error code. Its input value must
473 * pass the U_SUCCESS() test, or else the function returns
474 * immediately. Check for U_FAILURE() on output or use with
475 * function chaining. (See User Guide for details.)
477 * @throws IllegalArgumentException for syntax errors in the pattern string
478 * @throws IndexOutOfBoundsException if certain limits are exceeded
479 * (e.g., argument number too high, argument name too long, etc.)
480 * @throws NumberFormatException if a number could not be parsed
483 MessagePattern
&parsePluralStyle(const UnicodeString
&pattern
,
484 UParseError
*parseError
, UErrorCode
&errorCode
);
487 * Parses a SelectFormat pattern string.
488 * @param pattern a SelectFormat pattern string
489 * @param parseError Struct to receive information on the position
490 * of an error within the pattern.
492 * @param errorCode Standard ICU error code. Its input value must
493 * pass the U_SUCCESS() test, or else the function returns
494 * immediately. Check for U_FAILURE() on output or use with
495 * function chaining. (See User Guide for details.)
497 * @throws IllegalArgumentException for syntax errors in the pattern string
498 * @throws IndexOutOfBoundsException if certain limits are exceeded
499 * (e.g., argument number too high, argument name too long, etc.)
500 * @throws NumberFormatException if a number could not be parsed
503 MessagePattern
&parseSelectStyle(const UnicodeString
&pattern
,
504 UParseError
*parseError
, UErrorCode
&errorCode
);
507 * Clears this MessagePattern.
508 * countParts() will return 0.
514 * Clears this MessagePattern and sets the UMessagePatternApostropheMode.
515 * countParts() will return 0.
516 * @param mode The new UMessagePatternApostropheMode.
519 void clearPatternAndSetApostropheMode(UMessagePatternApostropheMode mode
) {
525 * @param other another object to compare with.
526 * @return TRUE if this object is equivalent to the other one.
529 UBool
operator==(const MessagePattern
&other
) const;
532 * @param other another object to compare with.
533 * @return FALSE if this object is equivalent to the other one.
536 inline UBool
operator!=(const MessagePattern
&other
) const {
537 return !operator==(other
);
541 * @return A hash code for this object.
544 int32_t hashCode() const;
547 * @return this instance's UMessagePatternApostropheMode.
550 UMessagePatternApostropheMode
getApostropheMode() const {
554 // Java has package-private jdkAposMode() here.
555 // In C++, this is declared in the MessageImpl class.
558 * @return the parsed pattern string (null if none was parsed).
561 const UnicodeString
&getPatternString() const {
566 * Does the parsed pattern have named arguments like {first_name}?
567 * @return TRUE if the parsed pattern has at least one named argument.
570 UBool
hasNamedArguments() const {
575 * Does the parsed pattern have numbered arguments like {2}?
576 * @return TRUE if the parsed pattern has at least one numbered argument.
579 UBool
hasNumberedArguments() const {
580 return hasArgNumbers
;
584 * Validates and parses an argument name or argument number string.
585 * An argument name must be a "pattern identifier", that is, it must contain
586 * no Unicode Pattern_Syntax or Pattern_White_Space characters.
587 * If it only contains ASCII digits, then it must be a small integer with no leading zero.
588 * @param name Input string.
589 * @return >=0 if the name is a valid number,
590 * ARG_NAME_NOT_NUMBER (-1) if it is a "pattern identifier" but not all ASCII digits,
591 * ARG_NAME_NOT_VALID (-2) if it is neither.
594 static int32_t validateArgumentName(const UnicodeString
&name
);
597 * Returns a version of the parsed pattern string where each ASCII apostrophe
598 * is doubled (escaped) if it is not already, and if it is not interpreted as quoting syntax.
600 * For example, this turns "I don't '{know}' {gender,select,female{h''er}other{h'im}}."
601 * into "I don''t '{know}' {gender,select,female{h''er}other{h''im}}."
602 * @return the deep-auto-quoted version of the parsed pattern string.
603 * @see MessageFormat.autoQuoteApostrophe()
606 UnicodeString
autoQuoteApostropheDeep() const;
611 * Returns the number of "parts" created by parsing the pattern string.
612 * Returns 0 if no pattern has been parsed or clear() was called.
613 * @return the number of pattern parts.
616 int32_t countParts() const {
621 * Gets the i-th pattern "part".
622 * @param i The index of the Part data. (0..countParts()-1)
623 * @return the i-th pattern "part".
626 const Part
&getPart(int32_t i
) const {
631 * Returns the UMessagePatternPartType of the i-th pattern "part".
632 * Convenience method for getPart(i).getType().
633 * @param i The index of the Part data. (0..countParts()-1)
634 * @return The UMessagePatternPartType of the i-th Part.
637 UMessagePatternPartType
getPartType(int32_t i
) const {
638 return getPart(i
).type
;
642 * Returns the pattern index of the specified pattern "part".
643 * Convenience method for getPart(partIndex).getIndex().
644 * @param partIndex The index of the Part data. (0..countParts()-1)
645 * @return The pattern index of this Part.
648 int32_t getPatternIndex(int32_t partIndex
) const {
649 return getPart(partIndex
).index
;
653 * Returns the substring of the pattern string indicated by the Part.
654 * Convenience method for getPatternString().substring(part.getIndex(), part.getLimit()).
655 * @param part a part of this MessagePattern.
656 * @return the substring associated with part.
659 UnicodeString
getSubstring(const Part
&part
) const {
660 return msg
.tempSubString(part
.index
, part
.length
);
664 * Compares the part's substring with the input string s.
665 * @param part a part of this MessagePattern.
667 * @return TRUE if getSubstring(part).equals(s).
670 UBool
partSubstringMatches(const Part
&part
, const UnicodeString
&s
) const {
671 return 0==msg
.compare(part
.index
, part
.length
, s
);
675 * Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.
676 * @param part a part of this MessagePattern.
677 * @return the part's numeric value, or UMSGPAT_NO_NUMERIC_VALUE if this is not a numeric part.
680 double getNumericValue(const Part
&part
) const;
683 * Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
684 * @param pluralStart the index of the first PluralFormat argument style part. (0..countParts()-1)
685 * @return the "offset:" value.
688 double getPluralOffset(int32_t pluralStart
) const;
691 * Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.
692 * @param start The index of some Part data (0..countParts()-1);
693 * this Part should be of Type ARG_START or MSG_START.
694 * @return The first i>start where getPart(i).getType()==ARG|MSG_LIMIT at the same nesting level,
695 * or start itself if getPartType(msgStart)!=ARG|MSG_START.
698 int32_t getLimitPartIndex(int32_t start
) const {
699 int32_t limit
=getPart(start
).limitPartIndex
;
707 * A message pattern "part", representing a pattern parsing event.
708 * There is a part for the start and end of a message or argument,
709 * for quoting and escaping of and with ASCII apostrophes,
710 * and for syntax elements of "complex" arguments.
713 class Part
: public UMemory
{
716 * Default constructor, do not use.
722 * Returns the type of this part.
723 * @return the part type.
726 UMessagePatternPartType
getType() const {
731 * Returns the pattern string index associated with this Part.
732 * @return this part's pattern string index.
735 int32_t getIndex() const {
740 * Returns the length of the pattern substring associated with this Part.
741 * This is 0 for some parts.
742 * @return this part's pattern substring length.
745 int32_t getLength() const {
750 * Returns the pattern string limit (exclusive-end) index associated with this Part.
751 * Convenience method for getIndex()+getLength().
752 * @return this part's pattern string limit index, same as getIndex()+getLength().
755 int32_t getLimit() const {
760 * Returns a value associated with this part.
761 * See the documentation of each part type for details.
762 * @return the part value.
765 int32_t getValue() const {
770 * Returns the argument type if this part is of type ARG_START or ARG_LIMIT,
771 * otherwise UMSGPAT_ARG_TYPE_NONE.
772 * @return the argument type for this part.
775 UMessagePatternArgType
getArgType() const {
776 UMessagePatternPartType msgType
=getType();
777 if(msgType
==UMSGPAT_PART_TYPE_ARG_START
|| msgType
==UMSGPAT_PART_TYPE_ARG_LIMIT
) {
778 return (UMessagePatternArgType
)value
;
780 return UMSGPAT_ARG_TYPE_NONE
;
785 * Indicates whether the Part type has a numeric value.
786 * If so, then that numeric value can be retrieved via MessagePattern.getNumericValue().
787 * @param type The Part type to be tested.
788 * @return TRUE if the Part type has a numeric value.
791 static UBool
hasNumericValue(UMessagePatternPartType type
) {
792 return type
==UMSGPAT_PART_TYPE_ARG_INT
|| type
==UMSGPAT_PART_TYPE_ARG_DOUBLE
;
796 * @param other another object to compare with.
797 * @return TRUE if this object is equivalent to the other one.
800 UBool
operator==(const Part
&other
) const;
803 * @param other another object to compare with.
804 * @return FALSE if this object is equivalent to the other one.
807 inline UBool
operator!=(const Part
&other
) const {
808 return !operator==(other
);
812 * @return A hash code for this object.
815 int32_t hashCode() const {
816 return ((type
*37+index
)*37+length
)*37+value
;
820 friend class MessagePattern
;
822 static const int32_t MAX_LENGTH
=0xffff;
823 static const int32_t MAX_VALUE
=0x7fff;
825 // Some fields are not final because they are modified during pattern parsing.
826 // After pattern parsing, the parts are effectively immutable.
827 UMessagePatternPartType type
;
831 int32_t limitPartIndex
;
835 void preParse(const UnicodeString
&pattern
, UParseError
*parseError
, UErrorCode
&errorCode
);
839 int32_t parseMessage(int32_t index
, int32_t msgStartLength
,
840 int32_t nestingLevel
, UMessagePatternArgType parentType
,
841 UParseError
*parseError
, UErrorCode
&errorCode
);
843 int32_t parseArg(int32_t index
, int32_t argStartLength
, int32_t nestingLevel
,
844 UParseError
*parseError
, UErrorCode
&errorCode
);
846 int32_t parseSimpleStyle(int32_t index
, UParseError
*parseError
, UErrorCode
&errorCode
);
848 int32_t parseChoiceStyle(int32_t index
, int32_t nestingLevel
,
849 UParseError
*parseError
, UErrorCode
&errorCode
);
851 int32_t parsePluralOrSelectStyle(UMessagePatternArgType argType
, int32_t index
, int32_t nestingLevel
,
852 UParseError
*parseError
, UErrorCode
&errorCode
);
855 * Validates and parses an argument name or argument number string.
856 * This internal method assumes that the input substring is a "pattern identifier".
857 * @return >=0 if the name is a valid number,
858 * ARG_NAME_NOT_NUMBER (-1) if it is a "pattern identifier" but not all ASCII digits,
859 * ARG_NAME_NOT_VALID (-2) if it is neither.
860 * @see #validateArgumentName(String)
862 static int32_t parseArgNumber(const UnicodeString
&s
, int32_t start
, int32_t limit
);
864 int32_t parseArgNumber(int32_t start
, int32_t limit
) {
865 return parseArgNumber(msg
, start
, limit
);
869 * Parses a number from the specified message substring.
870 * @param start start index into the message string
871 * @param limit limit index into the message string, must be start<limit
872 * @param allowInfinity TRUE if U+221E is allowed (for ChoiceFormat)
876 void parseDouble(int32_t start
, int32_t limit
, UBool allowInfinity
,
877 UParseError
*parseError
, UErrorCode
&errorCode
);
879 // Java has package-private appendReducedApostrophes() here.
880 // In C++, this is declared in the MessageImpl class.
882 int32_t skipWhiteSpace(int32_t index
);
884 int32_t skipIdentifier(int32_t index
);
887 * Skips a sequence of characters that could occur in a double value.
888 * Does not fully parse or validate the value.
890 int32_t skipDouble(int32_t index
);
892 static UBool
isArgTypeChar(UChar32 c
);
894 UBool
isChoice(int32_t index
);
896 UBool
isPlural(int32_t index
);
898 UBool
isSelect(int32_t index
);
900 UBool
isOrdinal(int32_t index
);
903 * @return TRUE if we are inside a MessageFormat (sub-)pattern,
904 * as opposed to inside a top-level choice/plural/select pattern.
906 UBool
inMessageFormatPattern(int32_t nestingLevel
);
909 * @return TRUE if we are in a MessageFormat sub-pattern
910 * of a top-level ChoiceFormat pattern.
912 UBool
inTopLevelChoiceMessage(int32_t nestingLevel
, UMessagePatternArgType parentType
);
914 void addPart(UMessagePatternPartType type
, int32_t index
, int32_t length
,
915 int32_t value
, UErrorCode
&errorCode
);
917 void addLimitPart(int32_t start
,
918 UMessagePatternPartType type
, int32_t index
, int32_t length
,
919 int32_t value
, UErrorCode
&errorCode
);
921 void addArgDoublePart(double numericValue
, int32_t start
, int32_t length
, UErrorCode
&errorCode
);
923 void setParseError(UParseError
*parseError
, int32_t index
);
925 UBool
init(UErrorCode
&errorCode
);
926 UBool
copyStorage(const MessagePattern
&other
, UErrorCode
&errorCode
);
928 UMessagePatternApostropheMode aposMode
;
930 // ArrayList<Part> parts=new ArrayList<Part>();
931 MessagePatternPartsList
*partsList
;
934 // ArrayList<Double> numericValues;
935 MessagePatternDoubleList
*numericValuesList
;
936 double *numericValues
;
937 int32_t numericValuesLength
;
940 UBool needsAutoQuoting
;
945 #endif // !UCONFIG_NO_FORMATTING
947 #endif /* U_SHOW_CPLUSPLUS_API */
949 #endif // __MESSAGEPATTERN_H__