]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/messagepattern.cpp
ICU-491.11.3.tar.gz
[apple/icu.git] / icuSources / common / messagepattern.cpp
CommitLineData
4388f060
A
1/*
2*******************************************************************************
3* Copyright (C) 2011, International Business Machines
4* Corporation and others. All Rights Reserved.
5*******************************************************************************
6* file name: messagepattern.cpp
7* encoding: US-ASCII
8* tab size: 8 (not used)
9* indentation:4
10*
11* created on: 2011mar14
12* created by: Markus W. Scherer
13*/
14
15#include "unicode/utypes.h"
16
17#if !UCONFIG_NO_FORMATTING
18
19#include "unicode/messagepattern.h"
20#include "unicode/unistr.h"
21#include "unicode/utf16.h"
22#include "cmemory.h"
23#include "cstring.h"
24#include "messageimpl.h"
25#include "patternprops.h"
26#include "putilimp.h"
27#include "uassert.h"
28
29U_NAMESPACE_BEGIN
30
31// Unicode character/code point constants ---------------------------------- ***
32
33static const UChar u_pound=0x23;
34static const UChar u_apos=0x27;
35static const UChar u_plus=0x2B;
36static const UChar u_comma=0x2C;
37static const UChar u_minus=0x2D;
38static const UChar u_dot=0x2E;
39static const UChar u_colon=0x3A;
40static const UChar u_lessThan=0x3C;
41static const UChar u_equal=0x3D;
42static const UChar u_A=0x41;
43static const UChar u_C=0x43;
44static const UChar u_E=0x45;
45static const UChar u_H=0x48;
46static const UChar u_I=0x49;
47static const UChar u_L=0x4C;
48static const UChar u_O=0x4F;
49static const UChar u_P=0x50;
50static const UChar u_R=0x52;
51static const UChar u_S=0x53;
52static const UChar u_T=0x54;
53static const UChar u_U=0x55;
54static const UChar u_Z=0x5A;
55static const UChar u_a=0x61;
56static const UChar u_c=0x63;
57static const UChar u_e=0x65;
58static const UChar u_f=0x66;
59static const UChar u_h=0x68;
60static const UChar u_i=0x69;
61static const UChar u_l=0x6C;
62static const UChar u_o=0x6F;
63static const UChar u_p=0x70;
64static const UChar u_r=0x72;
65static const UChar u_s=0x73;
66static const UChar u_t=0x74;
67static const UChar u_u=0x75;
68static const UChar u_z=0x7A;
69static const UChar u_leftCurlyBrace=0x7B;
70static const UChar u_pipe=0x7C;
71static const UChar u_rightCurlyBrace=0x7D;
72static const UChar u_lessOrEqual=0x2264; // U+2264 is <=
73
74static const UChar kOffsetColon[]={ // "offset:"
75 u_o, u_f, u_f, u_s, u_e, u_t, u_colon
76};
77
78static const UChar kOther[]={ // "other"
79 u_o, u_t, u_h, u_e, u_r
80};
81
82// MessagePatternList ------------------------------------------------------ ***
83
84template<typename T, int32_t stackCapacity>
85class MessagePatternList : public UMemory {
86public:
87 MessagePatternList() {}
88 void copyFrom(const MessagePatternList<T, stackCapacity> &other,
89 int32_t length,
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));
94 }
95
96 MaybeStackArray<T, stackCapacity> a;
97};
98
99template<typename T, int32_t stackCapacity>
100void
101MessagePatternList<T, stackCapacity>::copyFrom(
102 const MessagePatternList<T, stackCapacity> &other,
103 int32_t length,
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;
108 return;
109 }
110 uprv_memcpy(a.getAlias(), other.a.getAlias(), length*sizeof(T));
111 }
112}
113
114template<typename T, int32_t stackCapacity>
115UBool
116MessagePatternList<T, stackCapacity>::ensureCapacityForOneMore(int32_t oldLength, UErrorCode &errorCode) {
117 if(U_FAILURE(errorCode)) {
118 return FALSE;
119 }
120 if(a.getCapacity()>oldLength || a.resize(2*oldLength, oldLength)!=NULL) {
121 return TRUE;
122 }
123 errorCode=U_MEMORY_ALLOCATION_ERROR;
124 return FALSE;
125}
126
127// MessagePatternList specializations -------------------------------------- ***
128
129class MessagePatternDoubleList : public MessagePatternList<double, 8> {
130};
131
132class MessagePatternPartsList : public MessagePatternList<MessagePattern::Part, 32> {
133};
134
135// MessagePattern constructors etc. ---------------------------------------- ***
136
137MessagePattern::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) {
142 init(errorCode);
143}
144
145MessagePattern::MessagePattern(UMessagePatternApostropheMode mode, UErrorCode &errorCode)
146 : aposMode(mode),
147 partsList(NULL), parts(NULL), partsLength(0),
148 numericValuesList(NULL), numericValues(NULL), numericValuesLength(0),
149 hasArgNames(FALSE), hasArgNumbers(FALSE), needsAutoQuoting(FALSE) {
150 init(errorCode);
151}
152
153MessagePattern::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);
160 }
161}
162
163UBool
164MessagePattern::init(UErrorCode &errorCode) {
165 if(U_FAILURE(errorCode)) {
166 return FALSE;
167 }
168 partsList=new MessagePatternPartsList();
169 if(partsList==NULL) {
170 errorCode=U_MEMORY_ALLOCATION_ERROR;
171 return FALSE;
172 }
173 parts=partsList->a.getAlias();
174 return TRUE;
175}
176
177MessagePattern::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)) {
185 clear();
186 }
187}
188
189MessagePattern &
190MessagePattern::operator=(const MessagePattern &other) {
191 if(this==&other) {
192 return *this;
193 }
194 aposMode=other.aposMode;
195 msg=other.msg;
196 hasArgNames=other.hasArgNames;
197 hasArgNumbers=other.hasArgNumbers;
198 needsAutoQuoting=other.needsAutoQuoting;
199 UErrorCode errorCode=U_ZERO_ERROR;
200 if(!copyStorage(other, errorCode)) {
201 clear();
202 }
203 return *this;
204}
205
206UBool
207MessagePattern::copyStorage(const MessagePattern &other, UErrorCode &errorCode) {
208 if(U_FAILURE(errorCode)) {
209 return FALSE;
210 }
211 parts=NULL;
212 partsLength=0;
213 numericValues=NULL;
214 numericValuesLength=0;
215 if(partsList==NULL) {
216 partsList=new MessagePatternPartsList();
217 if(partsList==NULL) {
218 errorCode=U_MEMORY_ALLOCATION_ERROR;
219 return FALSE;
220 }
221 parts=partsList->a.getAlias();
222 }
223 if(other.partsLength>0) {
224 partsList->copyFrom(*other.partsList, other.partsLength, errorCode);
225 if(U_FAILURE(errorCode)) {
226 return FALSE;
227 }
228 parts=partsList->a.getAlias();
229 partsLength=other.partsLength;
230 }
231 if(other.numericValuesLength>0) {
232 if(numericValuesList==NULL) {
233 numericValuesList=new MessagePatternDoubleList();
234 if(numericValuesList==NULL) {
235 errorCode=U_MEMORY_ALLOCATION_ERROR;
236 return FALSE;
237 }
238 numericValues=numericValuesList->a.getAlias();
239 }
240 numericValuesList->copyFrom(
241 *other.numericValuesList, other.numericValuesLength, errorCode);
242 if(U_FAILURE(errorCode)) {
243 return FALSE;
244 }
245 numericValues=numericValuesList->a.getAlias();
246 numericValuesLength=other.numericValuesLength;
247 }
248 return TRUE;
249}
250
251MessagePattern::~MessagePattern() {
252 delete partsList;
253 delete numericValuesList;
254}
255
256// MessagePattern API ------------------------------------------------------ ***
257
258MessagePattern &
259MessagePattern::parse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode) {
260 preParse(pattern, parseError, errorCode);
261 parseMessage(0, 0, 0, UMSGPAT_ARG_TYPE_NONE, parseError, errorCode);
262 postParse();
263 return *this;
264}
265
266MessagePattern &
267MessagePattern::parseChoiceStyle(const UnicodeString &pattern,
268 UParseError *parseError, UErrorCode &errorCode) {
269 preParse(pattern, parseError, errorCode);
270 parseChoiceStyle(0, 0, parseError, errorCode);
271 postParse();
272 return *this;
273}
274
275MessagePattern &
276MessagePattern::parsePluralStyle(const UnicodeString &pattern,
277 UParseError *parseError, UErrorCode &errorCode) {
278 preParse(pattern, parseError, errorCode);
279 parsePluralOrSelectStyle(UMSGPAT_ARG_TYPE_PLURAL, 0, 0, parseError, errorCode);
280 postParse();
281 return *this;
282}
283
284MessagePattern &
285MessagePattern::parseSelectStyle(const UnicodeString &pattern,
286 UParseError *parseError, UErrorCode &errorCode) {
287 preParse(pattern, parseError, errorCode);
288 parsePluralOrSelectStyle(UMSGPAT_ARG_TYPE_SELECT, 0, 0, parseError, errorCode);
289 postParse();
290 return *this;
291}
292
293void
294MessagePattern::clear() {
295 // Mostly the same as preParse().
296 msg.remove();
297 hasArgNames=hasArgNumbers=FALSE;
298 needsAutoQuoting=FALSE;
299 partsLength=0;
300 numericValuesLength=0;
301}
302
303UBool
304MessagePattern::operator==(const MessagePattern &other) const {
305 if(this==&other) {
306 return TRUE;
307 }
308 return
309 aposMode==other.aposMode &&
310 msg==other.msg &&
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.
315}
316
317int32_t
318MessagePattern::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();
322 }
323 return hash;
324}
325
326int32_t
327MessagePattern::validateArgumentName(const UnicodeString &name) {
328 if(!PatternProps::isIdentifier(name.getBuffer(), name.length())) {
329 return UMSGPAT_ARG_NAME_NOT_VALID;
330 }
331 return parseArgNumber(name, 0, name.length());
332}
333
334UnicodeString
335MessagePattern::autoQuoteApostropheDeep() const {
336 if(!needsAutoQuoting) {
337 return msg;
338 }
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);
346 }
347 }
348 return modified;
349}
350
351double
352MessagePattern::getNumericValue(const Part &part) const {
353 UMessagePatternPartType type=part.type;
354 if(type==UMSGPAT_PART_TYPE_ARG_INT) {
355 return part.value;
356 } else if(type==UMSGPAT_PART_TYPE_ARG_DOUBLE) {
357 return numericValues[part.value];
358 } else {
359 return UMSGPAT_NO_NUMERIC_VALUE;
360 }
361}
362
363/**
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.
367 * @draft ICU 4.8
368 */
369double
370MessagePattern::getPluralOffset(int32_t pluralStart) const {
371 const Part &part=getPart(pluralStart);
372 if(Part::hasNumericValue(part.type)) {
373 return getNumericValue(part);
374 } else {
375 return 0;
376 }
377}
378
379// MessagePattern::Part ---------------------------------------------------- ***
380
381UBool
382MessagePattern::Part::operator==(const Part &other) const {
383 if(this==&other) {
384 return TRUE;
385 }
386 return
387 type==other.type &&
388 index==other.index &&
389 length==other.length &&
390 value==other.value &&
391 limitPartIndex==other.limitPartIndex;
392}
393
394// MessagePattern parser --------------------------------------------------- ***
395
396void
397MessagePattern::preParse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode) {
398 if(U_FAILURE(errorCode)) {
399 return;
400 }
401 if(parseError!=NULL) {
402 parseError->line=0;
403 parseError->offset=0;
404 parseError->preContext[0]=0;
405 parseError->postContext[0]=0;
406 }
407 msg=pattern;
408 hasArgNames=hasArgNumbers=FALSE;
409 needsAutoQuoting=FALSE;
410 partsLength=0;
411 numericValuesLength=0;
412}
413
414void
415MessagePattern::postParse() {
416 if(partsList!=NULL) {
417 parts=partsList->a.getAlias();
418 }
419 if(numericValuesList!=NULL) {
420 numericValues=numericValuesList->a.getAlias();
421 }
422}
423
424int32_t
425MessagePattern::parseMessage(int32_t index, int32_t msgStartLength,
426 int32_t nestingLevel, UMessagePatternArgType parentType,
427 UParseError *parseError, UErrorCode &errorCode) {
428 if(U_FAILURE(errorCode)) {
429 return 0;
430 }
431 if(nestingLevel>Part::MAX_VALUE) {
432 errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
433 return 0;
434 }
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)) {
440 return 0;
441 }
442 if(index>=msg.length()) {
443 break;
444 }
445 UChar c=msg.charAt(index++);
446 if(c==u_apos) {
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;
453 } else {
454 c=msg.charAt(index);
455 if(c==u_apos) {
456 // double apostrophe, skip the second one
457 addPart(UMSGPAT_PART_TYPE_SKIP_SYNTAX, index++, 1, 0, errorCode);
458 } else if(
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)
463 ) {
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
467 for(;;) {
468 index=msg.indexOf(u_apos, index+1);
469 if(index>=0) {
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);
474 } else {
475 // skip the quote-ending apostrophe
476 addPart(UMSGPAT_PART_TYPE_SKIP_SYNTAX, index++, 1, 0, errorCode);
477 break;
478 }
479 } else {
480 // The quoted text reaches to the end of the of the message.
481 index=msg.length();
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;
486 break;
487 }
488 }
489 } else {
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;
495 }
496 }
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 '|'.
513 return index-1;
514 } else {
515 // continue parsing after the '}'
516 return index;
517 }
518 } // else: c is part of literal text
519 }
520 if(nestingLevel>0 && !inTopLevelChoiceMessage(nestingLevel, parentType)) {
521 setParseError(parseError, 0); // Unmatched '{' braces in message.
522 errorCode=U_UNMATCHED_BRACES;
523 return 0;
524 }
525 addLimitPart(msgStart, UMSGPAT_PART_TYPE_MSG_LIMIT, index, 0, nestingLevel, errorCode);
526 return index;
527}
528
529int32_t
530MessagePattern::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)) {
536 return 0;
537 }
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;
542 return 0;
543 }
544 // parse argument name or number
545 index=skipIdentifier(index);
546 int32_t number=parseArgNumber(nameIndex, index);
547 if(number>=0) {
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;
552 return 0;
553 }
554 hasArgNumbers=TRUE;
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;
561 return 0;
562 }
563 hasArgNames=TRUE;
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;
568 return 0;
569 }
570 index=skipWhiteSpace(index);
571 if(index==msg.length()) {
572 setParseError(parseError, 0); // Unmatched '{' braces in message.
573 errorCode=U_UNMATCHED_BRACES;
574 return 0;
575 }
576 UChar c=msg.charAt(index);
577 if(c==u_rightCurlyBrace) {
578 // all done
579 } else if(c!=u_comma) {
580 setParseError(parseError, nameIndex); // Bad argument syntax.
581 errorCode=U_PATTERN_SYNTAX_ERROR;
582 return 0;
583 } else /* ',' */ {
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))) {
587 ++index;
588 }
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;
594 return 0;
595 }
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;
599 return 0;
600 }
601 if(length>Part::MAX_LENGTH) {
602 setParseError(parseError, nameIndex); // Argument type name too long.
603 errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
604 return 0;
605 }
606 argType=UMSGPAT_ARG_TYPE_SIMPLE;
607 if(length==6) {
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;
615 }
616 }
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);
621 }
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;
627 return 0;
628 }
629 } else /* ',' */ {
630 ++index;
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);
635 } else {
636 index=parsePluralOrSelectStyle(argType, index, nestingLevel, parseError, errorCode);
637 }
638 }
639 }
640 // Argument parsing stopped on the '}'.
641 addLimitPart(argStart, UMSGPAT_PART_TYPE_ARG_LIMIT, index, 1, argType, errorCode);
642 return index+1;
643}
644
645int32_t
646MessagePattern::parseSimpleStyle(int32_t index, UParseError *parseError, UErrorCode &errorCode) {
647 if(U_FAILURE(errorCode)) {
648 return 0;
649 }
650 int32_t start=index;
651 int32_t nestedBraces=0;
652 while(index<msg.length()) {
653 UChar c=msg.charAt(index++);
654 if(c==u_apos) {
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);
658 if(index<0) {
659 // Quoted literal argument style text reaches to the end of the message.
660 setParseError(parseError, start);
661 errorCode=U_PATTERN_SYNTAX_ERROR;
662 return 0;
663 }
664 // skip the quote-ending apostrophe
665 ++index;
666 } else if(c==u_leftCurlyBrace) {
667 ++nestedBraces;
668 } else if(c==u_rightCurlyBrace) {
669 if(nestedBraces>0) {
670 --nestedBraces;
671 } else {
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;
676 return 0;
677 }
678 addPart(UMSGPAT_PART_TYPE_ARG_STYLE, start, length, 0, errorCode);
679 return index;
680 }
681 } // c is part of literal text
682 }
683 setParseError(parseError, 0); // Unmatched '{' braces in message.
684 errorCode=U_UNMATCHED_BRACES;
685 return 0;
686}
687
688int32_t
689MessagePattern::parseChoiceStyle(int32_t index, int32_t nestingLevel,
690 UParseError *parseError, UErrorCode &errorCode) {
691 if(U_FAILURE(errorCode)) {
692 return 0;
693 }
694 int32_t start=index;
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;
699 return 0;
700 }
701 for(;;) {
702 // The choice argument style contains |-separated (number, separator, message) triples.
703 // Parse the number.
704 int32_t numberIndex=index;
705 index=skipDouble(index);
706 int32_t length=index-numberIndex;
707 if(length==0) {
708 setParseError(parseError, start); // Bad choice pattern syntax.
709 errorCode=U_PATTERN_SYNTAX_ERROR;
710 return 0;
711 }
712 if(length>Part::MAX_LENGTH) {
713 setParseError(parseError, numberIndex); // Choice number too long.
714 errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
715 return 0;
716 }
717 parseDouble(numberIndex, index, TRUE, parseError, errorCode); // adds ARG_INT or ARG_DOUBLE
718 if(U_FAILURE(errorCode)) {
719 return 0;
720 }
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;
726 return 0;
727 }
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;
732 return 0;
733 }
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)) {
738 return 0;
739 }
740 // parseMessage(..., CHOICE) returns the index of the terminator, or msg.length().
741 if(index==msg.length()) {
742 return index;
743 }
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;
748 return 0;
749 }
750 return index;
751 } // else the terminator is '|'
752 index=skipWhiteSpace(index+1);
753 }
754}
755
756int32_t
757MessagePattern::parsePluralOrSelectStyle(UMessagePatternArgType argType,
758 int32_t index, int32_t nestingLevel,
759 UParseError *parseError, UErrorCode &errorCode) {
760 if(U_FAILURE(errorCode)) {
761 return 0;
762 }
763 int32_t start=index;
764 UBool isEmpty=TRUE;
765 UBool hasOther=FALSE;
766 for(;;) {
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;
776 return 0;
777 }
778 if(!hasOther) {
779 setParseError(parseError, 0); // Missing 'other' keyword in plural/select pattern.
780 errorCode=U_DEFAULT_KEYWORD_MISSING;
781 return 0;
782 }
783 return index;
784 }
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;
790 if(length==1) {
791 setParseError(parseError, start); // Bad plural/select pattern syntax.
792 errorCode=U_PATTERN_SYNTAX_ERROR;
793 return 0;
794 }
795 if(length>Part::MAX_LENGTH) {
796 setParseError(parseError, selectorIndex); // Argument selector too long.
797 errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
798 return 0;
799 }
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
803 } else {
804 index=skipIdentifier(index);
805 int32_t length=index-selectorIndex;
806 if(length==0) {
807 setParseError(parseError, start); // Bad plural/select pattern syntax.
808 errorCode=U_PATTERN_SYNTAX_ERROR;
809 return 0;
810 }
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)
814 ) {
815 // plural offset, not a selector
816 if(!isEmpty) {
817 // Plural argument 'offset:' (if present) must precede key-message pairs.
818 setParseError(parseError, start);
819 errorCode=U_PATTERN_SYNTAX_ERROR;
820 return 0;
821 }
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;
828 return 0;
829 }
830 if((index-valueIndex)>Part::MAX_LENGTH) {
831 setParseError(parseError, valueIndex); // Plural offset value too long.
832 errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
833 return 0;
834 }
835 parseDouble(valueIndex, index, FALSE,
836 parseError, errorCode); // adds ARG_INT or ARG_DOUBLE
837 if(U_FAILURE(errorCode)) {
838 return 0;
839 }
840 isEmpty=FALSE;
841 continue; // no message fragment after the offset
842 } else {
843 // normal selector word
844 if(length>Part::MAX_LENGTH) {
845 setParseError(parseError, selectorIndex); // Argument selector too long.
846 errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
847 return 0;
848 }
849 addPart(UMSGPAT_PART_TYPE_ARG_SELECTOR, selectorIndex, length, 0, errorCode);
850 if(0==msg.compare(selectorIndex, length, kOther, 0, 5)) {
851 hasOther=TRUE;
852 }
853 }
854 }
855 if(U_FAILURE(errorCode)) {
856 return 0;
857 }
858
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;
864 return 0;
865 }
866 index=parseMessage(index, 1, nestingLevel+1, argType, parseError, errorCode);
867 if(U_FAILURE(errorCode)) {
868 return 0;
869 }
870 isEmpty=FALSE;
871 }
872}
873
874int32_t
875MessagePattern::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_.
879 if(start>=limit) {
880 return UMSGPAT_ARG_NAME_NOT_VALID;
881 }
882 int32_t number;
883 // Defer numeric errors until we know there are only digits.
884 UBool badNumber;
885 UChar c=s.charAt(start++);
886 if(c==0x30) {
887 if(start==limit) {
888 return 0;
889 } else {
890 number=0;
891 badNumber=TRUE; // leading zero
892 }
893 } else if(0x31<=c && c<=0x39) {
894 number=c-0x30;
895 badNumber=FALSE;
896 } else {
897 return UMSGPAT_ARG_NAME_NOT_NUMBER;
898 }
899 while(start<limit) {
900 c=s.charAt(start++);
901 if(0x30<=c && c<=0x39) {
902 if(number>=INT32_MAX/10) {
903 badNumber=TRUE; // overflow
904 }
905 number=number*10+(c-0x30);
906 } else {
907 return UMSGPAT_ARG_NAME_NOT_NUMBER;
908 }
909 }
910 // There are only ASCII digits.
911 if(badNumber) {
912 return UMSGPAT_ARG_NAME_NOT_VALID;
913 } else {
914 return number;
915 }
916}
917
918void
919MessagePattern::parseDouble(int32_t start, int32_t limit, UBool allowInfinity,
920 UParseError *parseError, UErrorCode &errorCode) {
921 if(U_FAILURE(errorCode)) {
922 return;
923 }
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
928 int32_t value=0;
929 int32_t isNegative=0; // not boolean so that we can easily add it to value
930 int32_t index=start;
931 UChar c=msg.charAt(index++);
932 if(c==u_minus) {
933 isNegative=1;
934 if(index==limit) {
935 break; // no number
936 }
937 c=msg.charAt(index++);
938 } else if(c==u_plus) {
939 if(index==limit) {
940 break; // no number
941 }
942 c=msg.charAt(index++);
943 }
944 if(c==0x221e) { // infinity
945 if(allowInfinity && index==limit) {
946 double infinity=uprv_getInfinity();
947 addArgDoublePart(
948 isNegative!=0 ? -infinity : infinity,
949 start, limit-start, errorCode);
950 return;
951 } else {
952 break;
953 }
954 }
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
960 }
961 if(index==limit) {
962 addPart(UMSGPAT_PART_TYPE_ARG_INT, start, limit-start,
963 isNegative!=0 ? -value : value, errorCode);
964 return;
965 }
966 c=msg.charAt(index++);
967 }
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
974 }
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
978 }
979 char *end;
980 double numericValue=uprv_strtod(numberChars, &end);
981 if(end!=(numberChars+length)) {
982 break; // parsing error
983 }
984 addArgDoublePart(numericValue, start, length, errorCode);
985 return;
986 }
987 setParseError(parseError, start /*, limit*/); // Bad syntax for numeric value.
988 errorCode=U_PATTERN_SYNTAX_ERROR;
989 return;
990}
991
992int32_t
993MessagePattern::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);
998}
999
1000int32_t
1001MessagePattern::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);
1006}
1007
1008int32_t
1009MessagePattern::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)) {
1015 break;
1016 }
1017 ++index;
1018 }
1019 return index;
1020}
1021
1022UBool
1023MessagePattern::isArgTypeChar(UChar32 c) {
1024 return (u_a<=c && c<=u_z) || (u_A<=c && c<=u_Z);
1025}
1026
1027UBool
1028MessagePattern::isChoice(int32_t index) {
1029 UChar c;
1030 return
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);
1037}
1038
1039UBool
1040MessagePattern::isPlural(int32_t index) {
1041 UChar c;
1042 return
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);
1049}
1050
1051UBool
1052MessagePattern::isSelect(int32_t index) {
1053 UChar c;
1054 return
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);
1061}
1062
1063UBool
1064MessagePattern::inMessageFormatPattern(int32_t nestingLevel) {
1065 return nestingLevel>0 || partsList->a[0].type==UMSGPAT_PART_TYPE_MSG_START;
1066}
1067
1068UBool
1069MessagePattern::inTopLevelChoiceMessage(int32_t nestingLevel, UMessagePatternArgType parentType) {
1070 return
1071 nestingLevel==1 &&
1072 parentType==UMSGPAT_ARG_TYPE_CHOICE &&
1073 partsList->a[0].type!=UMSGPAT_PART_TYPE_MSG_START;
1074}
1075
1076void
1077MessagePattern::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++];
1081 part.type=type;
1082 part.index=index;
1083 part.length=(uint16_t)length;
1084 part.value=(int16_t)value;
1085 part.limitPartIndex=0;
1086 }
1087}
1088
1089void
1090MessagePattern::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);
1095}
1096
1097void
1098MessagePattern::addArgDoublePart(double numericValue, int32_t start, int32_t length,
1099 UErrorCode &errorCode) {
1100 if(U_FAILURE(errorCode)) {
1101 return;
1102 }
1103 int32_t numericIndex=numericValuesLength;
1104 if(numericValuesList==NULL) {
1105 numericValuesList=new MessagePatternDoubleList();
1106 if(numericValuesList==NULL) {
1107 errorCode=U_MEMORY_ALLOCATION_ERROR;
1108 return;
1109 }
1110 } else if(!numericValuesList->ensureCapacityForOneMore(numericValuesLength, errorCode)) {
1111 return;
1112 } else {
1113 if(numericIndex>Part::MAX_VALUE) {
1114 errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
1115 return;
1116 }
1117 }
1118 numericValuesList->a[numericValuesLength++]=numericValue;
1119 addPart(UMSGPAT_PART_TYPE_ARG_DOUBLE, start, length, numericIndex, errorCode);
1120}
1121
1122void
1123MessagePattern::setParseError(UParseError *parseError, int32_t index) {
1124 if(parseError==NULL) {
1125 return;
1126 }
1127 parseError->offset=index;
1128
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])) {
1135 --length;
1136 }
1137 }
1138 msg.extract(index-length, length, parseError->preContext);
1139 parseError->preContext[length]=0;
1140
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])) {
1146 --length;
1147 }
1148 }
1149 msg.extract(index, length, parseError->postContext);
1150 parseError->postContext[length]=0;
1151}
1152
1153UOBJECT_DEFINE_NO_RTTI_IMPLEMENTATION(MessagePattern)
1154
1155// MessageImpl ------------------------------------------------------------- ***
1156
1157void
1158MessageImpl::appendReducedApostrophes(const UnicodeString &s, int32_t start, int32_t limit,
1159 UnicodeString &sb) {
1160 int32_t doubleApos=-1;
1161 for(;;) {
1162 int32_t i=s.indexOf(u_apos, start);
1163 if(i<0 || i>=limit) {
1164 sb.append(s, start, limit-start);
1165 break;
1166 }
1167 if(i==doubleApos) {
1168 // Double apostrophe at start-1 and start==i, append one.
1169 sb.append(u_apos);
1170 ++start;
1171 doubleApos=-1;
1172 } else {
1173 // Append text between apostrophes and skip this one.
1174 sb.append(s, start, i-start);
1175 doubleApos=start=i+1;
1176 }
1177 }
1178}
1179
1180// Ported from second half of ICU4J SelectFormat.format(String).
1181UnicodeString &
1182MessageImpl::appendSubMessageWithoutSkipSyntax(const MessagePattern &msgPattern,
1183 int32_t msgStart,
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);
1198 prevIndex=index;
1199 i=msgPattern.getLimitPartIndex(i);
1200 index=msgPattern.getPart(i).getLimit();
1201 appendReducedApostrophes(msgString, prevIndex, index, result);
1202 prevIndex=index;
1203 }
1204 }
1205}
1206
1207U_NAMESPACE_END
1208
1209#endif // !UCONFIG_NO_FORMATTING