2 *******************************************************************************
3 * Copyright (C) 2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: messagepattern.cpp
8 * tab size: 8 (not used)
11 * created on: 2011mar14
12 * created by: Markus W. Scherer
15 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_FORMATTING
19 #include "unicode/messagepattern.h"
20 #include "unicode/unistr.h"
21 #include "unicode/utf16.h"
24 #include "messageimpl.h"
25 #include "patternprops.h"
31 // Unicode character/code point constants ---------------------------------- ***
33 static const UChar u_pound
=0x23;
34 static const UChar u_apos
=0x27;
35 static const UChar u_plus
=0x2B;
36 static const UChar u_comma
=0x2C;
37 static const UChar u_minus
=0x2D;
38 static const UChar u_dot
=0x2E;
39 static const UChar u_colon
=0x3A;
40 static const UChar u_lessThan
=0x3C;
41 static const UChar u_equal
=0x3D;
42 static const UChar u_A
=0x41;
43 static const UChar u_C
=0x43;
44 static const UChar u_E
=0x45;
45 static const UChar u_H
=0x48;
46 static const UChar u_I
=0x49;
47 static const UChar u_L
=0x4C;
48 static const UChar u_O
=0x4F;
49 static const UChar u_P
=0x50;
50 static const UChar u_R
=0x52;
51 static const UChar u_S
=0x53;
52 static const UChar u_T
=0x54;
53 static const UChar u_U
=0x55;
54 static const UChar u_Z
=0x5A;
55 static const UChar u_a
=0x61;
56 static const UChar u_c
=0x63;
57 static const UChar u_e
=0x65;
58 static const UChar u_f
=0x66;
59 static const UChar u_h
=0x68;
60 static const UChar u_i
=0x69;
61 static const UChar u_l
=0x6C;
62 static const UChar u_o
=0x6F;
63 static const UChar u_p
=0x70;
64 static const UChar u_r
=0x72;
65 static const UChar u_s
=0x73;
66 static const UChar u_t
=0x74;
67 static const UChar u_u
=0x75;
68 static const UChar u_z
=0x7A;
69 static const UChar u_leftCurlyBrace
=0x7B;
70 static const UChar u_pipe
=0x7C;
71 static const UChar u_rightCurlyBrace
=0x7D;
72 static const UChar u_lessOrEqual
=0x2264; // U+2264 is <=
74 static const UChar kOffsetColon
[]={ // "offset:"
75 u_o
, u_f
, u_f
, u_s
, u_e
, u_t
, u_colon
78 static const UChar kOther
[]={ // "other"
79 u_o
, u_t
, u_h
, u_e
, u_r
82 // MessagePatternList ------------------------------------------------------ ***
84 template<typename T
, int32_t stackCapacity
>
85 class MessagePatternList
: public UMemory
{
87 MessagePatternList() {}
88 void copyFrom(const MessagePatternList
<T
, stackCapacity
> &other
,
90 UErrorCode
&errorCode
);
91 UBool
ensureCapacityForOneMore(int32_t oldLength
, UErrorCode
&errorCode
);
92 UBool
memEquals(const MessagePatternList
<T
, stackCapacity
> &other
, int32_t length
) const {
93 return 0==uprv_memcmp(a
.getAlias(), other
.a
.getAlias(), length
*sizeof(T
));
96 MaybeStackArray
<T
, stackCapacity
> a
;
99 template<typename T
, int32_t stackCapacity
>
101 MessagePatternList
<T
, stackCapacity
>::copyFrom(
102 const MessagePatternList
<T
, stackCapacity
> &other
,
104 UErrorCode
&errorCode
) {
105 if(U_SUCCESS(errorCode
) && length
>0) {
106 if(length
>a
.getCapacity() && NULL
==a
.resize(length
)) {
107 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
110 uprv_memcpy(a
.getAlias(), other
.a
.getAlias(), length
*sizeof(T
));
114 template<typename T
, int32_t stackCapacity
>
116 MessagePatternList
<T
, stackCapacity
>::ensureCapacityForOneMore(int32_t oldLength
, UErrorCode
&errorCode
) {
117 if(U_FAILURE(errorCode
)) {
120 if(a
.getCapacity()>oldLength
|| a
.resize(2*oldLength
, oldLength
)!=NULL
) {
123 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
127 // MessagePatternList specializations -------------------------------------- ***
129 class MessagePatternDoubleList
: public MessagePatternList
<double, 8> {
132 class MessagePatternPartsList
: public MessagePatternList
<MessagePattern::Part
, 32> {
135 // MessagePattern constructors etc. ---------------------------------------- ***
137 MessagePattern::MessagePattern(UErrorCode
&errorCode
)
138 : aposMode(UCONFIG_MSGPAT_DEFAULT_APOSTROPHE_MODE
),
139 partsList(NULL
), parts(NULL
), partsLength(0),
140 numericValuesList(NULL
), numericValues(NULL
), numericValuesLength(0),
141 hasArgNames(FALSE
), hasArgNumbers(FALSE
), needsAutoQuoting(FALSE
) {
145 MessagePattern::MessagePattern(UMessagePatternApostropheMode mode
, UErrorCode
&errorCode
)
147 partsList(NULL
), parts(NULL
), partsLength(0),
148 numericValuesList(NULL
), numericValues(NULL
), numericValuesLength(0),
149 hasArgNames(FALSE
), hasArgNumbers(FALSE
), needsAutoQuoting(FALSE
) {
153 MessagePattern::MessagePattern(const UnicodeString
&pattern
, UParseError
*parseError
, UErrorCode
&errorCode
)
154 : aposMode(UCONFIG_MSGPAT_DEFAULT_APOSTROPHE_MODE
),
155 partsList(NULL
), parts(NULL
), partsLength(0),
156 numericValuesList(NULL
), numericValues(NULL
), numericValuesLength(0),
157 hasArgNames(FALSE
), hasArgNumbers(FALSE
), needsAutoQuoting(FALSE
) {
158 if(init(errorCode
)) {
159 parse(pattern
, parseError
, errorCode
);
164 MessagePattern::init(UErrorCode
&errorCode
) {
165 if(U_FAILURE(errorCode
)) {
168 partsList
=new MessagePatternPartsList();
169 if(partsList
==NULL
) {
170 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
173 parts
=partsList
->a
.getAlias();
177 MessagePattern::MessagePattern(const MessagePattern
&other
)
178 : aposMode(other
.aposMode
), msg(other
.msg
),
179 partsList(NULL
), parts(NULL
), partsLength(0),
180 numericValuesList(NULL
), numericValues(NULL
), numericValuesLength(0),
181 hasArgNames(other
.hasArgNames
), hasArgNumbers(other
.hasArgNumbers
),
182 needsAutoQuoting(other
.needsAutoQuoting
) {
183 UErrorCode errorCode
=U_ZERO_ERROR
;
184 if(!copyStorage(other
, errorCode
)) {
190 MessagePattern::operator=(const MessagePattern
&other
) {
194 aposMode
=other
.aposMode
;
196 hasArgNames
=other
.hasArgNames
;
197 hasArgNumbers
=other
.hasArgNumbers
;
198 needsAutoQuoting
=other
.needsAutoQuoting
;
199 UErrorCode errorCode
=U_ZERO_ERROR
;
200 if(!copyStorage(other
, errorCode
)) {
207 MessagePattern::copyStorage(const MessagePattern
&other
, UErrorCode
&errorCode
) {
208 if(U_FAILURE(errorCode
)) {
214 numericValuesLength
=0;
215 if(partsList
==NULL
) {
216 partsList
=new MessagePatternPartsList();
217 if(partsList
==NULL
) {
218 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
221 parts
=partsList
->a
.getAlias();
223 if(other
.partsLength
>0) {
224 partsList
->copyFrom(*other
.partsList
, other
.partsLength
, errorCode
);
225 if(U_FAILURE(errorCode
)) {
228 parts
=partsList
->a
.getAlias();
229 partsLength
=other
.partsLength
;
231 if(other
.numericValuesLength
>0) {
232 if(numericValuesList
==NULL
) {
233 numericValuesList
=new MessagePatternDoubleList();
234 if(numericValuesList
==NULL
) {
235 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
238 numericValues
=numericValuesList
->a
.getAlias();
240 numericValuesList
->copyFrom(
241 *other
.numericValuesList
, other
.numericValuesLength
, errorCode
);
242 if(U_FAILURE(errorCode
)) {
245 numericValues
=numericValuesList
->a
.getAlias();
246 numericValuesLength
=other
.numericValuesLength
;
251 MessagePattern::~MessagePattern() {
253 delete numericValuesList
;
256 // MessagePattern API ------------------------------------------------------ ***
259 MessagePattern::parse(const UnicodeString
&pattern
, UParseError
*parseError
, UErrorCode
&errorCode
) {
260 preParse(pattern
, parseError
, errorCode
);
261 parseMessage(0, 0, 0, UMSGPAT_ARG_TYPE_NONE
, parseError
, errorCode
);
267 MessagePattern::parseChoiceStyle(const UnicodeString
&pattern
,
268 UParseError
*parseError
, UErrorCode
&errorCode
) {
269 preParse(pattern
, parseError
, errorCode
);
270 parseChoiceStyle(0, 0, parseError
, errorCode
);
276 MessagePattern::parsePluralStyle(const UnicodeString
&pattern
,
277 UParseError
*parseError
, UErrorCode
&errorCode
) {
278 preParse(pattern
, parseError
, errorCode
);
279 parsePluralOrSelectStyle(UMSGPAT_ARG_TYPE_PLURAL
, 0, 0, parseError
, errorCode
);
285 MessagePattern::parseSelectStyle(const UnicodeString
&pattern
,
286 UParseError
*parseError
, UErrorCode
&errorCode
) {
287 preParse(pattern
, parseError
, errorCode
);
288 parsePluralOrSelectStyle(UMSGPAT_ARG_TYPE_SELECT
, 0, 0, parseError
, errorCode
);
294 MessagePattern::clear() {
295 // Mostly the same as preParse().
297 hasArgNames
=hasArgNumbers
=FALSE
;
298 needsAutoQuoting
=FALSE
;
300 numericValuesLength
=0;
304 MessagePattern::operator==(const MessagePattern
&other
) const {
309 aposMode
==other
.aposMode
&&
311 // parts.equals(o.parts)
312 partsLength
==other
.partsLength
&&
313 (partsLength
==0 || partsList
->memEquals(*other
.partsList
, partsLength
));
314 // No need to compare numericValues if msg and parts are the same.
318 MessagePattern::hashCode() const {
319 int32_t hash
=(aposMode
*37+msg
.hashCode())*37+partsLength
;
320 for(int32_t i
=0; i
<partsLength
; ++i
) {
321 hash
=hash
*37+parts
[i
].hashCode();
327 MessagePattern::validateArgumentName(const UnicodeString
&name
) {
328 if(!PatternProps::isIdentifier(name
.getBuffer(), name
.length())) {
329 return UMSGPAT_ARG_NAME_NOT_VALID
;
331 return parseArgNumber(name
, 0, name
.length());
335 MessagePattern::autoQuoteApostropheDeep() const {
336 if(!needsAutoQuoting
) {
339 UnicodeString
modified(msg
);
340 // Iterate backward so that the insertion indexes do not change.
341 int32_t count
=countParts();
342 for(int32_t i
=count
; i
>0;) {
343 const Part
&part
=getPart(--i
);
344 if(part
.getType()==UMSGPAT_PART_TYPE_INSERT_CHAR
) {
345 modified
.insert(part
.index
, (UChar
)part
.value
);
352 MessagePattern::getNumericValue(const Part
&part
) const {
353 UMessagePatternPartType type
=part
.type
;
354 if(type
==UMSGPAT_PART_TYPE_ARG_INT
) {
356 } else if(type
==UMSGPAT_PART_TYPE_ARG_DOUBLE
) {
357 return numericValues
[part
.value
];
359 return UMSGPAT_NO_NUMERIC_VALUE
;
364 * Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
365 * @param pluralStart the index of the first PluralFormat argument style part. (0..countParts()-1)
366 * @return the "offset:" value.
370 MessagePattern::getPluralOffset(int32_t pluralStart
) const {
371 const Part
&part
=getPart(pluralStart
);
372 if(Part::hasNumericValue(part
.type
)) {
373 return getNumericValue(part
);
379 // MessagePattern::Part ---------------------------------------------------- ***
382 MessagePattern::Part::operator==(const Part
&other
) const {
388 index
==other
.index
&&
389 length
==other
.length
&&
390 value
==other
.value
&&
391 limitPartIndex
==other
.limitPartIndex
;
394 // MessagePattern parser --------------------------------------------------- ***
397 MessagePattern::preParse(const UnicodeString
&pattern
, UParseError
*parseError
, UErrorCode
&errorCode
) {
398 if(U_FAILURE(errorCode
)) {
401 if(parseError
!=NULL
) {
403 parseError
->offset
=0;
404 parseError
->preContext
[0]=0;
405 parseError
->postContext
[0]=0;
408 hasArgNames
=hasArgNumbers
=FALSE
;
409 needsAutoQuoting
=FALSE
;
411 numericValuesLength
=0;
415 MessagePattern::postParse() {
416 if(partsList
!=NULL
) {
417 parts
=partsList
->a
.getAlias();
419 if(numericValuesList
!=NULL
) {
420 numericValues
=numericValuesList
->a
.getAlias();
425 MessagePattern::parseMessage(int32_t index
, int32_t msgStartLength
,
426 int32_t nestingLevel
, UMessagePatternArgType parentType
,
427 UParseError
*parseError
, UErrorCode
&errorCode
) {
428 if(U_FAILURE(errorCode
)) {
431 if(nestingLevel
>Part::MAX_VALUE
) {
432 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
435 int32_t msgStart
=partsLength
;
436 addPart(UMSGPAT_PART_TYPE_MSG_START
, index
, msgStartLength
, nestingLevel
, errorCode
);
437 index
+=msgStartLength
;
438 for(;;) { // while(index<msg.length()) with U_FAILURE(errorCode) check
439 if(U_FAILURE(errorCode
)) {
442 if(index
>=msg
.length()) {
445 UChar c
=msg
.charAt(index
++);
447 if(index
==msg
.length()) {
448 // The apostrophe is the last character in the pattern.
449 // Add a Part for auto-quoting.
450 addPart(UMSGPAT_PART_TYPE_INSERT_CHAR
, index
, 0,
451 u_apos
, errorCode
); // value=char to be inserted
452 needsAutoQuoting
=TRUE
;
456 // double apostrophe, skip the second one
457 addPart(UMSGPAT_PART_TYPE_SKIP_SYNTAX
, index
++, 1, 0, errorCode
);
459 aposMode
==UMSGPAT_APOS_DOUBLE_REQUIRED
||
460 c
==u_leftCurlyBrace
|| c
==u_rightCurlyBrace
||
461 (parentType
==UMSGPAT_ARG_TYPE_CHOICE
&& c
==u_pipe
) ||
462 (parentType
==UMSGPAT_ARG_TYPE_PLURAL
&& c
==u_pound
)
464 // skip the quote-starting apostrophe
465 addPart(UMSGPAT_PART_TYPE_SKIP_SYNTAX
, index
-1, 1, 0, errorCode
);
466 // find the end of the quoted literal text
468 index
=msg
.indexOf(u_apos
, index
+1);
470 if(/*(index+1)<msg.length() &&*/ msg
.charAt(index
+1)==u_apos
) {
471 // double apostrophe inside quoted literal text
472 // still encodes a single apostrophe, skip the second one
473 addPart(UMSGPAT_PART_TYPE_SKIP_SYNTAX
, ++index
, 1, 0, errorCode
);
475 // skip the quote-ending apostrophe
476 addPart(UMSGPAT_PART_TYPE_SKIP_SYNTAX
, index
++, 1, 0, errorCode
);
480 // The quoted text reaches to the end of the of the message.
482 // Add a Part for auto-quoting.
483 addPart(UMSGPAT_PART_TYPE_INSERT_CHAR
, index
, 0,
484 u_apos
, errorCode
); // value=char to be inserted
485 needsAutoQuoting
=TRUE
;
490 // Interpret the apostrophe as literal text.
491 // Add a Part for auto-quoting.
492 addPart(UMSGPAT_PART_TYPE_INSERT_CHAR
, index
, 0,
493 u_apos
, errorCode
); // value=char to be inserted
494 needsAutoQuoting
=TRUE
;
497 } else if(parentType
==UMSGPAT_ARG_TYPE_PLURAL
&& c
==u_pound
) {
498 // The unquoted # in a plural message fragment will be replaced
499 // with the (number-offset).
500 addPart(UMSGPAT_PART_TYPE_REPLACE_NUMBER
, index
-1, 1, 0, errorCode
);
501 } else if(c
==u_leftCurlyBrace
) {
502 index
=parseArg(index
-1, 1, nestingLevel
, parseError
, errorCode
);
503 } else if((nestingLevel
>0 && c
==u_rightCurlyBrace
) ||
504 (parentType
==UMSGPAT_ARG_TYPE_CHOICE
&& c
==u_pipe
)) {
505 // Finish the message before the terminator.
506 // In a choice style, report the "}" substring only for the following ARG_LIMIT,
507 // not for this MSG_LIMIT.
508 int32_t limitLength
=(parentType
==UMSGPAT_ARG_TYPE_CHOICE
&& c
==u_rightCurlyBrace
) ? 0 : 1;
509 addLimitPart(msgStart
, UMSGPAT_PART_TYPE_MSG_LIMIT
, index
-1, limitLength
,
510 nestingLevel
, errorCode
);
511 if(parentType
==UMSGPAT_ARG_TYPE_CHOICE
) {
512 // Let the choice style parser see the '}' or '|'.
515 // continue parsing after the '}'
518 } // else: c is part of literal text
520 if(nestingLevel
>0 && !inTopLevelChoiceMessage(nestingLevel
, parentType
)) {
521 setParseError(parseError
, 0); // Unmatched '{' braces in message.
522 errorCode
=U_UNMATCHED_BRACES
;
525 addLimitPart(msgStart
, UMSGPAT_PART_TYPE_MSG_LIMIT
, index
, 0, nestingLevel
, errorCode
);
530 MessagePattern::parseArg(int32_t index
, int32_t argStartLength
, int32_t nestingLevel
,
531 UParseError
*parseError
, UErrorCode
&errorCode
) {
532 int32_t argStart
=partsLength
;
533 UMessagePatternArgType argType
=UMSGPAT_ARG_TYPE_NONE
;
534 addPart(UMSGPAT_PART_TYPE_ARG_START
, index
, argStartLength
, argType
, errorCode
);
535 if(U_FAILURE(errorCode
)) {
538 int32_t nameIndex
=index
=skipWhiteSpace(index
+argStartLength
);
539 if(index
==msg
.length()) {
540 setParseError(parseError
, 0); // Unmatched '{' braces in message.
541 errorCode
=U_UNMATCHED_BRACES
;
544 // parse argument name or number
545 index
=skipIdentifier(index
);
546 int32_t number
=parseArgNumber(nameIndex
, index
);
548 int32_t length
=index
-nameIndex
;
549 if(length
>Part::MAX_LENGTH
|| number
>Part::MAX_VALUE
) {
550 setParseError(parseError
, nameIndex
); // Argument number too large.
551 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
555 addPart(UMSGPAT_PART_TYPE_ARG_NUMBER
, nameIndex
, length
, number
, errorCode
);
556 } else if(number
==UMSGPAT_ARG_NAME_NOT_NUMBER
) {
557 int32_t length
=index
-nameIndex
;
558 if(length
>Part::MAX_LENGTH
) {
559 setParseError(parseError
, nameIndex
); // Argument name too long.
560 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
564 addPart(UMSGPAT_PART_TYPE_ARG_NAME
, nameIndex
, length
, 0, errorCode
);
565 } else { // number<-1 (ARG_NAME_NOT_VALID)
566 setParseError(parseError
, nameIndex
); // Bad argument syntax.
567 errorCode
=U_PATTERN_SYNTAX_ERROR
;
570 index
=skipWhiteSpace(index
);
571 if(index
==msg
.length()) {
572 setParseError(parseError
, 0); // Unmatched '{' braces in message.
573 errorCode
=U_UNMATCHED_BRACES
;
576 UChar c
=msg
.charAt(index
);
577 if(c
==u_rightCurlyBrace
) {
579 } else if(c
!=u_comma
) {
580 setParseError(parseError
, nameIndex
); // Bad argument syntax.
581 errorCode
=U_PATTERN_SYNTAX_ERROR
;
584 // parse argument type: case-sensitive a-zA-Z
585 int32_t typeIndex
=index
=skipWhiteSpace(index
+1);
586 while(index
<msg
.length() && isArgTypeChar(msg
.charAt(index
))) {
589 int32_t length
=index
-typeIndex
;
590 index
=skipWhiteSpace(index
);
591 if(index
==msg
.length()) {
592 setParseError(parseError
, 0); // Unmatched '{' braces in message.
593 errorCode
=U_UNMATCHED_BRACES
;
596 if(length
==0 || ((c
=msg
.charAt(index
))!=u_comma
&& c
!=u_rightCurlyBrace
)) {
597 setParseError(parseError
, nameIndex
); // Bad argument syntax.
598 errorCode
=U_PATTERN_SYNTAX_ERROR
;
601 if(length
>Part::MAX_LENGTH
) {
602 setParseError(parseError
, nameIndex
); // Argument type name too long.
603 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
606 argType
=UMSGPAT_ARG_TYPE_SIMPLE
;
608 // case-insensitive comparisons for complex-type names
609 if(isChoice(typeIndex
)) {
610 argType
=UMSGPAT_ARG_TYPE_CHOICE
;
611 } else if(isPlural(typeIndex
)) {
612 argType
=UMSGPAT_ARG_TYPE_PLURAL
;
613 } else if(isSelect(typeIndex
)) {
614 argType
=UMSGPAT_ARG_TYPE_SELECT
;
617 // change the ARG_START type from NONE to argType
618 partsList
->a
[argStart
].value
=(int16_t)argType
;
619 if(argType
==UMSGPAT_ARG_TYPE_SIMPLE
) {
620 addPart(UMSGPAT_PART_TYPE_ARG_TYPE
, typeIndex
, length
, 0, errorCode
);
622 // look for an argument style (pattern)
623 if(c
==u_rightCurlyBrace
) {
624 if(argType
!=UMSGPAT_ARG_TYPE_SIMPLE
) {
625 setParseError(parseError
, nameIndex
); // No style field for complex argument.
626 errorCode
=U_PATTERN_SYNTAX_ERROR
;
631 if(argType
==UMSGPAT_ARG_TYPE_SIMPLE
) {
632 index
=parseSimpleStyle(index
, parseError
, errorCode
);
633 } else if(argType
==UMSGPAT_ARG_TYPE_CHOICE
) {
634 index
=parseChoiceStyle(index
, nestingLevel
, parseError
, errorCode
);
636 index
=parsePluralOrSelectStyle(argType
, index
, nestingLevel
, parseError
, errorCode
);
640 // Argument parsing stopped on the '}'.
641 addLimitPart(argStart
, UMSGPAT_PART_TYPE_ARG_LIMIT
, index
, 1, argType
, errorCode
);
646 MessagePattern::parseSimpleStyle(int32_t index
, UParseError
*parseError
, UErrorCode
&errorCode
) {
647 if(U_FAILURE(errorCode
)) {
651 int32_t nestedBraces
=0;
652 while(index
<msg
.length()) {
653 UChar c
=msg
.charAt(index
++);
655 // Treat apostrophe as quoting but include it in the style part.
656 // Find the end of the quoted literal text.
657 index
=msg
.indexOf(u_apos
, index
);
659 // Quoted literal argument style text reaches to the end of the message.
660 setParseError(parseError
, start
);
661 errorCode
=U_PATTERN_SYNTAX_ERROR
;
664 // skip the quote-ending apostrophe
666 } else if(c
==u_leftCurlyBrace
) {
668 } else if(c
==u_rightCurlyBrace
) {
672 int32_t length
=--index
-start
;
673 if(length
>Part::MAX_LENGTH
) {
674 setParseError(parseError
, start
); // Argument style text too long.
675 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
678 addPart(UMSGPAT_PART_TYPE_ARG_STYLE
, start
, length
, 0, errorCode
);
681 } // c is part of literal text
683 setParseError(parseError
, 0); // Unmatched '{' braces in message.
684 errorCode
=U_UNMATCHED_BRACES
;
689 MessagePattern::parseChoiceStyle(int32_t index
, int32_t nestingLevel
,
690 UParseError
*parseError
, UErrorCode
&errorCode
) {
691 if(U_FAILURE(errorCode
)) {
695 index
=skipWhiteSpace(index
);
696 if(index
==msg
.length() || msg
.charAt(index
)==u_rightCurlyBrace
) {
697 setParseError(parseError
, 0); // Missing choice argument pattern.
698 errorCode
=U_PATTERN_SYNTAX_ERROR
;
702 // The choice argument style contains |-separated (number, separator, message) triples.
704 int32_t numberIndex
=index
;
705 index
=skipDouble(index
);
706 int32_t length
=index
-numberIndex
;
708 setParseError(parseError
, start
); // Bad choice pattern syntax.
709 errorCode
=U_PATTERN_SYNTAX_ERROR
;
712 if(length
>Part::MAX_LENGTH
) {
713 setParseError(parseError
, numberIndex
); // Choice number too long.
714 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
717 parseDouble(numberIndex
, index
, TRUE
, parseError
, errorCode
); // adds ARG_INT or ARG_DOUBLE
718 if(U_FAILURE(errorCode
)) {
721 // Parse the separator.
722 index
=skipWhiteSpace(index
);
723 if(index
==msg
.length()) {
724 setParseError(parseError
, start
); // Bad choice pattern syntax.
725 errorCode
=U_PATTERN_SYNTAX_ERROR
;
728 UChar c
=msg
.charAt(index
);
729 if(!(c
==u_pound
|| c
==u_lessThan
|| c
==u_lessOrEqual
)) { // U+2264 is <=
730 setParseError(parseError
, start
); // Expected choice separator (#<\u2264) instead of c.
731 errorCode
=U_PATTERN_SYNTAX_ERROR
;
734 addPart(UMSGPAT_PART_TYPE_ARG_SELECTOR
, index
, 1, 0, errorCode
);
735 // Parse the message fragment.
736 index
=parseMessage(++index
, 0, nestingLevel
+1, UMSGPAT_ARG_TYPE_CHOICE
, parseError
, errorCode
);
737 if(U_FAILURE(errorCode
)) {
740 // parseMessage(..., CHOICE) returns the index of the terminator, or msg.length().
741 if(index
==msg
.length()) {
744 if(msg
.charAt(index
)==u_rightCurlyBrace
) {
745 if(!inMessageFormatPattern(nestingLevel
)) {
746 setParseError(parseError
, start
); // Bad choice pattern syntax.
747 errorCode
=U_PATTERN_SYNTAX_ERROR
;
751 } // else the terminator is '|'
752 index
=skipWhiteSpace(index
+1);
757 MessagePattern::parsePluralOrSelectStyle(UMessagePatternArgType argType
,
758 int32_t index
, int32_t nestingLevel
,
759 UParseError
*parseError
, UErrorCode
&errorCode
) {
760 if(U_FAILURE(errorCode
)) {
765 UBool hasOther
=FALSE
;
767 // First, collect the selector looking for a small set of terminators.
768 // It would be a little faster to consider the syntax of each possible
769 // token right here, but that makes the code too complicated.
770 index
=skipWhiteSpace(index
);
771 UBool eos
=index
==msg
.length();
772 if(eos
|| msg
.charAt(index
)==u_rightCurlyBrace
) {
773 if(eos
==inMessageFormatPattern(nestingLevel
)) {
774 setParseError(parseError
, start
); // Bad plural/select pattern syntax.
775 errorCode
=U_PATTERN_SYNTAX_ERROR
;
779 setParseError(parseError
, 0); // Missing 'other' keyword in plural/select pattern.
780 errorCode
=U_DEFAULT_KEYWORD_MISSING
;
785 int32_t selectorIndex
=index
;
786 if(argType
==UMSGPAT_ARG_TYPE_PLURAL
&& msg
.charAt(selectorIndex
)==u_equal
) {
787 // explicit-value plural selector: =double
788 index
=skipDouble(index
+1);
789 int32_t length
=index
-selectorIndex
;
791 setParseError(parseError
, start
); // Bad plural/select pattern syntax.
792 errorCode
=U_PATTERN_SYNTAX_ERROR
;
795 if(length
>Part::MAX_LENGTH
) {
796 setParseError(parseError
, selectorIndex
); // Argument selector too long.
797 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
800 addPart(UMSGPAT_PART_TYPE_ARG_SELECTOR
, selectorIndex
, length
, 0, errorCode
);
801 parseDouble(selectorIndex
+1, index
, FALSE
,
802 parseError
, errorCode
); // adds ARG_INT or ARG_DOUBLE
804 index
=skipIdentifier(index
);
805 int32_t length
=index
-selectorIndex
;
807 setParseError(parseError
, start
); // Bad plural/select pattern syntax.
808 errorCode
=U_PATTERN_SYNTAX_ERROR
;
811 // Note: The ':' in "offset:" is just beyond the skipIdentifier() range.
812 if( argType
==UMSGPAT_ARG_TYPE_PLURAL
&& length
==6 && index
<msg
.length() &&
813 0==msg
.compare(selectorIndex
, 7, kOffsetColon
, 0, 7)
815 // plural offset, not a selector
817 // Plural argument 'offset:' (if present) must precede key-message pairs.
818 setParseError(parseError
, start
);
819 errorCode
=U_PATTERN_SYNTAX_ERROR
;
822 // allow whitespace between offset: and its value
823 int32_t valueIndex
=skipWhiteSpace(index
+1); // The ':' is at index.
824 index
=skipDouble(valueIndex
);
825 if(index
==valueIndex
) {
826 setParseError(parseError
, start
); // Missing value for plural 'offset:'.
827 errorCode
=U_PATTERN_SYNTAX_ERROR
;
830 if((index
-valueIndex
)>Part::MAX_LENGTH
) {
831 setParseError(parseError
, valueIndex
); // Plural offset value too long.
832 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
835 parseDouble(valueIndex
, index
, FALSE
,
836 parseError
, errorCode
); // adds ARG_INT or ARG_DOUBLE
837 if(U_FAILURE(errorCode
)) {
841 continue; // no message fragment after the offset
843 // normal selector word
844 if(length
>Part::MAX_LENGTH
) {
845 setParseError(parseError
, selectorIndex
); // Argument selector too long.
846 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
849 addPart(UMSGPAT_PART_TYPE_ARG_SELECTOR
, selectorIndex
, length
, 0, errorCode
);
850 if(0==msg
.compare(selectorIndex
, length
, kOther
, 0, 5)) {
855 if(U_FAILURE(errorCode
)) {
859 // parse the message fragment following the selector
860 index
=skipWhiteSpace(index
);
861 if(index
==msg
.length() || msg
.charAt(index
)!=u_leftCurlyBrace
) {
862 setParseError(parseError
, selectorIndex
); // No message fragment after plural/select selector.
863 errorCode
=U_PATTERN_SYNTAX_ERROR
;
866 index
=parseMessage(index
, 1, nestingLevel
+1, argType
, parseError
, errorCode
);
867 if(U_FAILURE(errorCode
)) {
875 MessagePattern::parseArgNumber(const UnicodeString
&s
, int32_t start
, int32_t limit
) {
876 // If the identifier contains only ASCII digits, then it is an argument _number_
877 // and must not have leading zeros (except "0" itself).
878 // Otherwise it is an argument _name_.
880 return UMSGPAT_ARG_NAME_NOT_VALID
;
883 // Defer numeric errors until we know there are only digits.
885 UChar c
=s
.charAt(start
++);
891 badNumber
=TRUE
; // leading zero
893 } else if(0x31<=c
&& c
<=0x39) {
897 return UMSGPAT_ARG_NAME_NOT_NUMBER
;
901 if(0x30<=c
&& c
<=0x39) {
902 if(number
>=INT32_MAX
/10) {
903 badNumber
=TRUE
; // overflow
905 number
=number
*10+(c
-0x30);
907 return UMSGPAT_ARG_NAME_NOT_NUMBER
;
910 // There are only ASCII digits.
912 return UMSGPAT_ARG_NAME_NOT_VALID
;
919 MessagePattern::parseDouble(int32_t start
, int32_t limit
, UBool allowInfinity
,
920 UParseError
*parseError
, UErrorCode
&errorCode
) {
921 if(U_FAILURE(errorCode
)) {
924 U_ASSERT(start
<limit
);
925 // fake loop for easy exit and single throw statement
926 for(;;) { /*loop doesn't iterate*/
927 // fast path for small integers and infinity
929 int32_t isNegative
=0; // not boolean so that we can easily add it to value
931 UChar c
=msg
.charAt(index
++);
937 c
=msg
.charAt(index
++);
938 } else if(c
==u_plus
) {
942 c
=msg
.charAt(index
++);
944 if(c
==0x221e) { // infinity
945 if(allowInfinity
&& index
==limit
) {
946 double infinity
=uprv_getInfinity();
948 isNegative
!=0 ? -infinity
: infinity
,
949 start
, limit
-start
, errorCode
);
955 // try to parse the number as a small integer but fall back to a double
956 while('0'<=c
&& c
<='9') {
957 value
=value
*10+(c
-'0');
958 if(value
>(Part::MAX_VALUE
+isNegative
)) {
959 break; // not a small-enough integer
962 addPart(UMSGPAT_PART_TYPE_ARG_INT
, start
, limit
-start
,
963 isNegative
!=0 ? -value
: value
, errorCode
);
966 c
=msg
.charAt(index
++);
968 // Let Double.parseDouble() throw a NumberFormatException.
969 char numberChars
[128];
970 int32_t capacity
=(int32_t)sizeof(numberChars
);
971 int32_t length
=limit
-start
;
972 if(length
>=capacity
) {
973 break; // number too long
975 msg
.extract(start
, length
, numberChars
, capacity
, US_INV
);
976 if((int32_t)uprv_strlen(numberChars
)<length
) {
977 break; // contains non-invariant character that was turned into NUL
980 double numericValue
=uprv_strtod(numberChars
, &end
);
981 if(end
!=(numberChars
+length
)) {
982 break; // parsing error
984 addArgDoublePart(numericValue
, start
, length
, errorCode
);
987 setParseError(parseError
, start
/*, limit*/); // Bad syntax for numeric value.
988 errorCode
=U_PATTERN_SYNTAX_ERROR
;
993 MessagePattern::skipWhiteSpace(int32_t index
) {
994 const UChar
*s
=msg
.getBuffer();
995 int32_t msgLength
=msg
.length();
996 const UChar
*t
=PatternProps::skipWhiteSpace(s
+index
, msgLength
-index
);
997 return (int32_t)(t
-s
);
1001 MessagePattern::skipIdentifier(int32_t index
) {
1002 const UChar
*s
=msg
.getBuffer();
1003 int32_t msgLength
=msg
.length();
1004 const UChar
*t
=PatternProps::skipIdentifier(s
+index
, msgLength
-index
);
1005 return (int32_t)(t
-s
);
1009 MessagePattern::skipDouble(int32_t index
) {
1010 int32_t msgLength
=msg
.length();
1011 while(index
<msgLength
) {
1012 UChar c
=msg
.charAt(index
);
1013 // U+221E: Allow the infinity symbol, for ChoiceFormat patterns.
1014 if((c
<0x30 && c
!=u_plus
&& c
!=u_minus
&& c
!=u_dot
) || (c
>0x39 && c
!=u_e
&& c
!=u_E
&& c
!=0x221e)) {
1023 MessagePattern::isArgTypeChar(UChar32 c
) {
1024 return (u_a
<=c
&& c
<=u_z
) || (u_A
<=c
&& c
<=u_Z
);
1028 MessagePattern::isChoice(int32_t index
) {
1031 ((c
=msg
.charAt(index
++))==u_c
|| c
==u_C
) &&
1032 ((c
=msg
.charAt(index
++))==u_h
|| c
==u_H
) &&
1033 ((c
=msg
.charAt(index
++))==u_o
|| c
==u_O
) &&
1034 ((c
=msg
.charAt(index
++))==u_i
|| c
==u_I
) &&
1035 ((c
=msg
.charAt(index
++))==u_c
|| c
==u_C
) &&
1036 ((c
=msg
.charAt(index
))==u_e
|| c
==u_E
);
1040 MessagePattern::isPlural(int32_t index
) {
1043 ((c
=msg
.charAt(index
++))==u_p
|| c
==u_P
) &&
1044 ((c
=msg
.charAt(index
++))==u_l
|| c
==u_L
) &&
1045 ((c
=msg
.charAt(index
++))==u_u
|| c
==u_U
) &&
1046 ((c
=msg
.charAt(index
++))==u_r
|| c
==u_R
) &&
1047 ((c
=msg
.charAt(index
++))==u_a
|| c
==u_A
) &&
1048 ((c
=msg
.charAt(index
))==u_l
|| c
==u_L
);
1052 MessagePattern::isSelect(int32_t index
) {
1055 ((c
=msg
.charAt(index
++))==u_s
|| c
==u_S
) &&
1056 ((c
=msg
.charAt(index
++))==u_e
|| c
==u_E
) &&
1057 ((c
=msg
.charAt(index
++))==u_l
|| c
==u_L
) &&
1058 ((c
=msg
.charAt(index
++))==u_e
|| c
==u_E
) &&
1059 ((c
=msg
.charAt(index
++))==u_c
|| c
==u_C
) &&
1060 ((c
=msg
.charAt(index
))==u_t
|| c
==u_T
);
1064 MessagePattern::inMessageFormatPattern(int32_t nestingLevel
) {
1065 return nestingLevel
>0 || partsList
->a
[0].type
==UMSGPAT_PART_TYPE_MSG_START
;
1069 MessagePattern::inTopLevelChoiceMessage(int32_t nestingLevel
, UMessagePatternArgType parentType
) {
1072 parentType
==UMSGPAT_ARG_TYPE_CHOICE
&&
1073 partsList
->a
[0].type
!=UMSGPAT_PART_TYPE_MSG_START
;
1077 MessagePattern::addPart(UMessagePatternPartType type
, int32_t index
, int32_t length
,
1078 int32_t value
, UErrorCode
&errorCode
) {
1079 if(partsList
->ensureCapacityForOneMore(partsLength
, errorCode
)) {
1080 Part
&part
=partsList
->a
[partsLength
++];
1083 part
.length
=(uint16_t)length
;
1084 part
.value
=(int16_t)value
;
1085 part
.limitPartIndex
=0;
1090 MessagePattern::addLimitPart(int32_t start
,
1091 UMessagePatternPartType type
, int32_t index
, int32_t length
,
1092 int32_t value
, UErrorCode
&errorCode
) {
1093 partsList
->a
[start
].limitPartIndex
=partsLength
;
1094 addPart(type
, index
, length
, value
, errorCode
);
1098 MessagePattern::addArgDoublePart(double numericValue
, int32_t start
, int32_t length
,
1099 UErrorCode
&errorCode
) {
1100 if(U_FAILURE(errorCode
)) {
1103 int32_t numericIndex
=numericValuesLength
;
1104 if(numericValuesList
==NULL
) {
1105 numericValuesList
=new MessagePatternDoubleList();
1106 if(numericValuesList
==NULL
) {
1107 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
1110 } else if(!numericValuesList
->ensureCapacityForOneMore(numericValuesLength
, errorCode
)) {
1113 if(numericIndex
>Part::MAX_VALUE
) {
1114 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1118 numericValuesList
->a
[numericValuesLength
++]=numericValue
;
1119 addPart(UMSGPAT_PART_TYPE_ARG_DOUBLE
, start
, length
, numericIndex
, errorCode
);
1123 MessagePattern::setParseError(UParseError
*parseError
, int32_t index
) {
1124 if(parseError
==NULL
) {
1127 parseError
->offset
=index
;
1129 // Set preContext to some of msg before index.
1130 // Avoid splitting a surrogate pair.
1131 int32_t length
=index
;
1132 if(length
>=U_PARSE_CONTEXT_LEN
) {
1133 length
=U_PARSE_CONTEXT_LEN
-1;
1134 if(length
>0 && U16_IS_TRAIL(msg
[index
-length
])) {
1138 msg
.extract(index
-length
, length
, parseError
->preContext
);
1139 parseError
->preContext
[length
]=0;
1141 // Set postContext to some of msg starting at index.
1142 length
=msg
.length()-index
;
1143 if(length
>=U_PARSE_CONTEXT_LEN
) {
1144 length
=U_PARSE_CONTEXT_LEN
-1;
1145 if(length
>0 && U16_IS_LEAD(msg
[index
+length
-1])) {
1149 msg
.extract(index
, length
, parseError
->postContext
);
1150 parseError
->postContext
[length
]=0;
1153 UOBJECT_DEFINE_NO_RTTI_IMPLEMENTATION(MessagePattern
)
1155 // MessageImpl ------------------------------------------------------------- ***
1158 MessageImpl::appendReducedApostrophes(const UnicodeString
&s
, int32_t start
, int32_t limit
,
1159 UnicodeString
&sb
) {
1160 int32_t doubleApos
=-1;
1162 int32_t i
=s
.indexOf(u_apos
, start
);
1163 if(i
<0 || i
>=limit
) {
1164 sb
.append(s
, start
, limit
-start
);
1168 // Double apostrophe at start-1 and start==i, append one.
1173 // Append text between apostrophes and skip this one.
1174 sb
.append(s
, start
, i
-start
);
1175 doubleApos
=start
=i
+1;
1180 // Ported from second half of ICU4J SelectFormat.format(String).
1182 MessageImpl::appendSubMessageWithoutSkipSyntax(const MessagePattern
&msgPattern
,
1184 UnicodeString
&result
) {
1185 const UnicodeString
&msgString
=msgPattern
.getPatternString();
1186 int32_t prevIndex
=msgPattern
.getPart(msgStart
).getLimit();
1187 for(int32_t i
=msgStart
;;) {
1188 const MessagePattern::Part
&part
=msgPattern
.getPart(++i
);
1189 UMessagePatternPartType type
=part
.getType();
1190 int32_t index
=part
.getIndex();
1191 if(type
==UMSGPAT_PART_TYPE_MSG_LIMIT
) {
1192 return result
.append(msgString
, prevIndex
, index
-prevIndex
);
1193 } else if(type
==UMSGPAT_PART_TYPE_SKIP_SYNTAX
) {
1194 result
.append(msgString
, prevIndex
, index
-prevIndex
);
1195 prevIndex
=part
.getLimit();
1196 } else if(type
==UMSGPAT_PART_TYPE_ARG_START
) {
1197 result
.append(msgString
, prevIndex
, index
-prevIndex
);
1199 i
=msgPattern
.getLimitPartIndex(i
);
1200 index
=msgPattern
.getPart(i
).getLimit();
1201 appendReducedApostrophes(msgString
, prevIndex
, index
, result
);
1209 #endif // !UCONFIG_NO_FORMATTING