]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/msgfmt.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / msgfmt.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
729e4ab9
A
3/********************************************************************
4 * COPYRIGHT:
2ca993e8 5 * Copyright (c) 1997-2015, International Business Machines Corporation and
729e4ab9
A
6 * others. All Rights Reserved.
7 ********************************************************************
8 *
9 * File MSGFMT.CPP
10 *
11 * Modification History:
12 *
13 * Date Name Description
14 * 02/19/97 aliu Converted from java.
15 * 03/20/97 helena Finished first cut of implementation.
16 * 04/10/97 aliu Made to work on AIX. Added stoi to replace wtoi.
17 * 06/11/97 helena Fixed addPattern to take the pattern correctly.
18 * 06/17/97 helena Fixed the getPattern to return the correct pattern.
19 * 07/09/97 helena Made ParsePosition into a class.
20 * 02/22/99 stephen Removed character literals for EBCDIC safety
21 * 11/01/09 kirtig Added SelectFormat
22 ********************************************************************/
b75a7d8f
A
23
24#include "unicode/utypes.h"
25
26#if !UCONFIG_NO_FORMATTING
27
4388f060 28#include "unicode/appendable.h"
b75a7d8f 29#include "unicode/choicfmt.h"
4388f060
A
30#include "unicode/datefmt.h"
31#include "unicode/decimfmt.h"
32#include "unicode/localpointer.h"
33#include "unicode/msgfmt.h"
0f5d89e8 34#include "unicode/numberformatter.h"
46f4442e 35#include "unicode/plurfmt.h"
4388f060 36#include "unicode/rbnf.h"
729e4ab9 37#include "unicode/selfmt.h"
4388f060 38#include "unicode/smpdtfmt.h"
73c04bcf 39#include "unicode/umsg.h"
4388f060 40#include "unicode/ustring.h"
b75a7d8f 41#include "cmemory.h"
4388f060
A
42#include "patternprops.h"
43#include "messageimpl.h"
46f4442e 44#include "msgfmt_impl.h"
57a6839d 45#include "plurrule_impl.h"
b75a7d8f 46#include "uassert.h"
4388f060
A
47#include "uelement.h"
48#include "uhash.h"
46f4442e 49#include "ustrfmt.h"
4388f060 50#include "util.h"
46f4442e 51#include "uvector.h"
3d1f044b 52#include "number_decimalquantity.h"
b75a7d8f
A
53
54// *****************************************************************************
55// class MessageFormat
56// *****************************************************************************
57
b75a7d8f 58#define SINGLE_QUOTE ((UChar)0x0027)
4388f060 59#define COMMA ((UChar)0x002C)
b75a7d8f
A
60#define LEFT_CURLY_BRACE ((UChar)0x007B)
61#define RIGHT_CURLY_BRACE ((UChar)0x007D)
62
63//---------------------------------------
64// static data
65
66static const UChar ID_NUMBER[] = {
67 0x6E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0 /* "number" */
68};
69static const UChar ID_DATE[] = {
70 0x64, 0x61, 0x74, 0x65, 0 /* "date" */
71};
72static const UChar ID_TIME[] = {
73 0x74, 0x69, 0x6D, 0x65, 0 /* "time" */
74};
374ca955
A
75static const UChar ID_SPELLOUT[] = {
76 0x73, 0x70, 0x65, 0x6c, 0x6c, 0x6f, 0x75, 0x74, 0 /* "spellout" */
77};
78static const UChar ID_ORDINAL[] = {
79 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x6c, 0 /* "ordinal" */
80};
81static const UChar ID_DURATION[] = {
82 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0 /* "duration" */
83};
b75a7d8f
A
84
85// MessageFormat Type List Number, Date, Time or Choice
86static const UChar * const TYPE_IDS[] = {
b75a7d8f 87 ID_NUMBER,
729e4ab9 88 ID_DATE,
b75a7d8f 89 ID_TIME,
374ca955
A
90 ID_SPELLOUT,
91 ID_ORDINAL,
92 ID_DURATION,
93 NULL,
b75a7d8f 94};
729e4ab9 95
4388f060
A
96static const UChar ID_EMPTY[] = {
97 0 /* empty string, used for default so that null can mark end of list */
98};
b75a7d8f
A
99static const UChar ID_CURRENCY[] = {
100 0x63, 0x75, 0x72, 0x72, 0x65, 0x6E, 0x63, 0x79, 0 /* "currency" */
101};
102static const UChar ID_PERCENT[] = {
103 0x70, 0x65, 0x72, 0x63, 0x65, 0x6E, 0x74, 0 /* "percent" */
104};
105static const UChar ID_INTEGER[] = {
106 0x69, 0x6E, 0x74, 0x65, 0x67, 0x65, 0x72, 0 /* "integer" */
107};
108
109// NumberFormat modifier list, default, currency, percent or integer
110static const UChar * const NUMBER_STYLE_IDS[] = {
374ca955 111 ID_EMPTY,
b75a7d8f
A
112 ID_CURRENCY,
113 ID_PERCENT,
114 ID_INTEGER,
115 NULL,
116};
374ca955 117
b75a7d8f
A
118static const UChar ID_SHORT[] = {
119 0x73, 0x68, 0x6F, 0x72, 0x74, 0 /* "short" */
120};
121static const UChar ID_MEDIUM[] = {
122 0x6D, 0x65, 0x64, 0x69, 0x75, 0x6D, 0 /* "medium" */
123};
124static const UChar ID_LONG[] = {
125 0x6C, 0x6F, 0x6E, 0x67, 0 /* "long" */
126};
127static const UChar ID_FULL[] = {
128 0x66, 0x75, 0x6C, 0x6C, 0 /* "full" */
129};
130
131// DateFormat modifier list, default, short, medium, long or full
132static const UChar * const DATE_STYLE_IDS[] = {
374ca955 133 ID_EMPTY,
b75a7d8f
A
134 ID_SHORT,
135 ID_MEDIUM,
136 ID_LONG,
374ca955
A
137 ID_FULL,
138 NULL,
b75a7d8f 139};
729e4ab9 140
4388f060
A
141static const icu::DateFormat::EStyle DATE_STYLES[] = {
142 icu::DateFormat::kDefault,
143 icu::DateFormat::kShort,
144 icu::DateFormat::kMedium,
145 icu::DateFormat::kLong,
146 icu::DateFormat::kFull,
b75a7d8f
A
147};
148
b75a7d8f
A
149static const int32_t DEFAULT_INITIAL_CAPACITY = 10;
150
4388f060
A
151static const UChar NULL_STRING[] = {
152 0x6E, 0x75, 0x6C, 0x6C, 0 // "null"
153};
154
155static const UChar OTHER_STRING[] = {
156 0x6F, 0x74, 0x68, 0x65, 0x72, 0 // "other"
157};
158
159U_CDECL_BEGIN
160static UBool U_CALLCONV equalFormatsForHash(const UHashTok key1,
161 const UHashTok key2) {
162 return icu::MessageFormat::equalFormats(key1.pointer, key2.pointer);
163}
164
165U_CDECL_END
166
b75a7d8f
A
167U_NAMESPACE_BEGIN
168
169// -------------------------------------
374ca955 170UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MessageFormat)
46f4442e 171UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FormatNameEnumeration)
b75a7d8f
A
172
173//--------------------------------------------------------------------
174
b75a7d8f
A
175/**
176 * Convert an integer value to a string and append the result to
177 * the given UnicodeString.
178 */
179static UnicodeString& itos(int32_t i, UnicodeString& appendTo) {
180 UChar temp[16];
181 uprv_itou(temp,16,i,10,0); // 10 == radix
4388f060 182 appendTo.append(temp, -1);
b75a7d8f
A
183 return appendTo;
184}
185
4388f060
A
186
187// AppendableWrapper: encapsulates the result of formatting, keeping track
188// of the string and its length.
189class AppendableWrapper : public UMemory {
46f4442e 190public:
4388f060 191 AppendableWrapper(Appendable& appendable) : app(appendable), len(0) {
46f4442e 192 }
4388f060
A
193 void append(const UnicodeString& s) {
194 app.appendString(s.getBuffer(), s.length());
195 len += s.length();
46f4442e 196 }
4388f060
A
197 void append(const UChar* s, const int32_t sLength) {
198 app.appendString(s, sLength);
199 len += sLength;
200 }
201 void append(const UnicodeString& s, int32_t start, int32_t length) {
202 append(s.tempSubString(start, length));
203 }
204 void formatAndAppend(const Format* formatter, const Formattable& arg, UErrorCode& ec) {
205 UnicodeString s;
206 formatter->format(arg, s, ec);
207 if (U_SUCCESS(ec)) {
208 append(s);
209 }
210 }
57a6839d
A
211 void formatAndAppend(const Format* formatter, const Formattable& arg,
212 const UnicodeString &argString, UErrorCode& ec) {
213 if (!argString.isEmpty()) {
214 if (U_SUCCESS(ec)) {
215 append(argString);
216 }
217 } else {
218 formatAndAppend(formatter, arg, ec);
219 }
220 }
4388f060
A
221 int32_t length() {
222 return len;
46f4442e 223 }
4388f060
A
224private:
225 Appendable& app;
226 int32_t len;
46f4442e
A
227};
228
4388f060 229
b75a7d8f
A
230// -------------------------------------
231// Creates a MessageFormat instance based on the pattern.
232
233MessageFormat::MessageFormat(const UnicodeString& pattern,
234 UErrorCode& success)
235: fLocale(Locale::getDefault()), // Uses the default locale
4388f060 236 msgPattern(success),
b75a7d8f
A
237 formatAliases(NULL),
238 formatAliasesCapacity(0),
b75a7d8f
A
239 argTypes(NULL),
240 argTypeCount(0),
241 argTypeCapacity(0),
4388f060 242 hasArgTypeConflicts(FALSE),
b75a7d8f 243 defaultNumberFormat(NULL),
4388f060
A
244 defaultDateFormat(NULL),
245 cachedFormatters(NULL),
246 customFormatArgStarts(NULL),
57a6839d
A
247 pluralProvider(*this, UPLURAL_TYPE_CARDINAL),
248 ordinalProvider(*this, UPLURAL_TYPE_ORDINAL)
b75a7d8f 249{
374ca955 250 setLocaleIDs(fLocale.getName(), fLocale.getName());
4388f060 251 applyPattern(pattern, success);
b75a7d8f 252}
729e4ab9 253
b75a7d8f
A
254MessageFormat::MessageFormat(const UnicodeString& pattern,
255 const Locale& newLocale,
256 UErrorCode& success)
257: fLocale(newLocale),
4388f060 258 msgPattern(success),
b75a7d8f
A
259 formatAliases(NULL),
260 formatAliasesCapacity(0),
b75a7d8f
A
261 argTypes(NULL),
262 argTypeCount(0),
263 argTypeCapacity(0),
4388f060 264 hasArgTypeConflicts(FALSE),
b75a7d8f 265 defaultNumberFormat(NULL),
4388f060
A
266 defaultDateFormat(NULL),
267 cachedFormatters(NULL),
268 customFormatArgStarts(NULL),
57a6839d
A
269 pluralProvider(*this, UPLURAL_TYPE_CARDINAL),
270 ordinalProvider(*this, UPLURAL_TYPE_ORDINAL)
b75a7d8f 271{
374ca955 272 setLocaleIDs(fLocale.getName(), fLocale.getName());
4388f060 273 applyPattern(pattern, success);
b75a7d8f
A
274}
275
276MessageFormat::MessageFormat(const UnicodeString& pattern,
277 const Locale& newLocale,
278 UParseError& parseError,
279 UErrorCode& success)
280: fLocale(newLocale),
4388f060 281 msgPattern(success),
b75a7d8f
A
282 formatAliases(NULL),
283 formatAliasesCapacity(0),
b75a7d8f
A
284 argTypes(NULL),
285 argTypeCount(0),
286 argTypeCapacity(0),
4388f060 287 hasArgTypeConflicts(FALSE),
b75a7d8f 288 defaultNumberFormat(NULL),
4388f060
A
289 defaultDateFormat(NULL),
290 cachedFormatters(NULL),
291 customFormatArgStarts(NULL),
57a6839d
A
292 pluralProvider(*this, UPLURAL_TYPE_CARDINAL),
293 ordinalProvider(*this, UPLURAL_TYPE_ORDINAL)
b75a7d8f 294{
374ca955 295 setLocaleIDs(fLocale.getName(), fLocale.getName());
4388f060 296 applyPattern(pattern, parseError, success);
b75a7d8f
A
297}
298
299MessageFormat::MessageFormat(const MessageFormat& that)
4388f060
A
300:
301 Format(that),
302 fLocale(that.fLocale),
303 msgPattern(that.msgPattern),
b75a7d8f
A
304 formatAliases(NULL),
305 formatAliasesCapacity(0),
b75a7d8f
A
306 argTypes(NULL),
307 argTypeCount(0),
308 argTypeCapacity(0),
4388f060 309 hasArgTypeConflicts(that.hasArgTypeConflicts),
b75a7d8f 310 defaultNumberFormat(NULL),
4388f060
A
311 defaultDateFormat(NULL),
312 cachedFormatters(NULL),
313 customFormatArgStarts(NULL),
57a6839d
A
314 pluralProvider(*this, UPLURAL_TYPE_CARDINAL),
315 ordinalProvider(*this, UPLURAL_TYPE_ORDINAL)
b75a7d8f 316{
4388f060
A
317 // This will take care of creating the hash tables (since they are NULL).
318 UErrorCode ec = U_ZERO_ERROR;
319 copyObjects(that, ec);
320 if (U_FAILURE(ec)) {
321 resetPattern();
322 }
b75a7d8f
A
323}
324
325MessageFormat::~MessageFormat()
326{
4388f060
A
327 uhash_close(cachedFormatters);
328 uhash_close(customFormatArgStarts);
b75a7d8f
A
329
330 uprv_free(argTypes);
b75a7d8f 331 uprv_free(formatAliases);
b75a7d8f
A
332 delete defaultNumberFormat;
333 delete defaultDateFormat;
334}
335
336//--------------------------------------------------------------------
337// Variable-size array management
338
b75a7d8f
A
339/**
340 * Allocate argTypes[] to at least the given capacity and return
341 * TRUE if successful. If not, leave argTypes[] unchanged.
342 *
343 * If argTypes is NULL, allocate it. If it is not NULL, enlarge it
344 * if necessary to be at least as large as specified.
345 */
4388f060
A
346UBool MessageFormat::allocateArgTypes(int32_t capacity, UErrorCode& status) {
347 if (U_FAILURE(status)) {
348 return FALSE;
349 }
350 if (argTypeCapacity >= capacity) {
351 return TRUE;
352 }
353 if (capacity < DEFAULT_INITIAL_CAPACITY) {
354 capacity = DEFAULT_INITIAL_CAPACITY;
355 } else if (capacity < 2*argTypeCapacity) {
356 capacity = 2*argTypeCapacity;
357 }
358 Formattable::Type* a = (Formattable::Type*)
b75a7d8f 359 uprv_realloc(argTypes, sizeof(*argTypes) * capacity);
4388f060
A
360 if (a == NULL) {
361 status = U_MEMORY_ALLOCATION_ERROR;
362 return FALSE;
b75a7d8f 363 }
4388f060
A
364 argTypes = a;
365 argTypeCapacity = capacity;
b75a7d8f
A
366 return TRUE;
367}
368
369// -------------------------------------
370// assignment operator
371
372const MessageFormat&
373MessageFormat::operator=(const MessageFormat& that)
374{
4388f060 375 if (this != &that) {
b75a7d8f
A
376 // Calls the super class for assignment first.
377 Format::operator=(that);
378
b75a7d8f 379 setLocale(that.fLocale);
4388f060
A
380 msgPattern = that.msgPattern;
381 hasArgTypeConflicts = that.hasArgTypeConflicts;
729e4ab9 382
4388f060
A
383 UErrorCode ec = U_ZERO_ERROR;
384 copyObjects(that, ec);
385 if (U_FAILURE(ec)) {
386 resetPattern();
b75a7d8f 387 }
b75a7d8f
A
388 }
389 return *this;
390}
391
392UBool
729e4ab9 393MessageFormat::operator==(const Format& rhs) const
b75a7d8f
A
394{
395 if (this == &rhs) return TRUE;
729e4ab9 396
b75a7d8f
A
397 MessageFormat& that = (MessageFormat&)rhs;
398
399 // Check class ID before checking MessageFormat members
400 if (!Format::operator==(rhs) ||
4388f060
A
401 msgPattern != that.msgPattern ||
402 fLocale != that.fLocale) {
b75a7d8f
A
403 return FALSE;
404 }
405
4388f060
A
406 // Compare hashtables.
407 if ((customFormatArgStarts == NULL) != (that.customFormatArgStarts == NULL)) {
408 return FALSE;
409 }
410 if (customFormatArgStarts == NULL) {
411 return TRUE;
412 }
413
414 UErrorCode ec = U_ZERO_ERROR;
415 const int32_t count = uhash_count(customFormatArgStarts);
416 const int32_t rhs_count = uhash_count(that.customFormatArgStarts);
417 if (count != rhs_count) {
418 return FALSE;
419 }
b331163b 420 int32_t idx = 0, rhs_idx = 0, pos = UHASH_FIRST, rhs_pos = UHASH_FIRST;
4388f060
A
421 for (; idx < count && rhs_idx < rhs_count && U_SUCCESS(ec); ++idx, ++rhs_idx) {
422 const UHashElement* cur = uhash_nextElement(customFormatArgStarts, &pos);
423 const UHashElement* rhs_cur = uhash_nextElement(that.customFormatArgStarts, &rhs_pos);
424 if (cur->key.integer != rhs_cur->key.integer) {
425 return FALSE;
426 }
427 const Format* format = (const Format*)uhash_iget(cachedFormatters, cur->key.integer);
428 const Format* rhs_format = (const Format*)uhash_iget(that.cachedFormatters, rhs_cur->key.integer);
429 if (*format != *rhs_format) {
b75a7d8f
A
430 return FALSE;
431 }
432 }
b75a7d8f
A
433 return TRUE;
434}
435
436// -------------------------------------
437// Creates a copy of this MessageFormat, the caller owns the copy.
729e4ab9 438
b75a7d8f
A
439Format*
440MessageFormat::clone() const
441{
442 return new MessageFormat(*this);
443}
729e4ab9 444
b75a7d8f
A
445// -------------------------------------
446// Sets the locale of this MessageFormat object to theLocale.
729e4ab9 447
b75a7d8f
A
448void
449MessageFormat::setLocale(const Locale& theLocale)
450{
451 if (fLocale != theLocale) {
452 delete defaultNumberFormat;
453 defaultNumberFormat = NULL;
454 delete defaultDateFormat;
455 defaultDateFormat = NULL;
4388f060
A
456 fLocale = theLocale;
457 setLocaleIDs(fLocale.getName(), fLocale.getName());
57a6839d
A
458 pluralProvider.reset();
459 ordinalProvider.reset();
b75a7d8f 460 }
b75a7d8f 461}
729e4ab9 462
b75a7d8f
A
463// -------------------------------------
464// Gets the locale of this MessageFormat object.
729e4ab9 465
b75a7d8f
A
466const Locale&
467MessageFormat::getLocale() const
468{
469 return fLocale;
470}
471
b75a7d8f 472void
729e4ab9 473MessageFormat::applyPattern(const UnicodeString& newPattern,
b75a7d8f
A
474 UErrorCode& status)
475{
476 UParseError parseError;
477 applyPattern(newPattern,parseError,status);
478}
479
480
481// -------------------------------------
482// Applies the new pattern and returns an error if the pattern
483// is not correct.
484void
729e4ab9 485MessageFormat::applyPattern(const UnicodeString& pattern,
b75a7d8f
A
486 UParseError& parseError,
487 UErrorCode& ec)
729e4ab9 488{
b75a7d8f
A
489 if(U_FAILURE(ec)) {
490 return;
491 }
4388f060
A
492 msgPattern.parse(pattern, &parseError, ec);
493 cacheExplicitFormats(ec);
b75a7d8f 494
4388f060
A
495 if (U_FAILURE(ec)) {
496 resetPattern();
b75a7d8f 497 }
4388f060
A
498}
499
500void MessageFormat::resetPattern() {
501 msgPattern.clear();
502 uhash_close(cachedFormatters);
503 cachedFormatters = NULL;
504 uhash_close(customFormatArgStarts);
505 customFormatArgStarts = NULL;
506 argTypeCount = 0;
507 hasArgTypeConflicts = FALSE;
508}
b75a7d8f 509
4388f060
A
510void
511MessageFormat::applyPattern(const UnicodeString& pattern,
512 UMessagePatternApostropheMode aposMode,
513 UParseError* parseError,
514 UErrorCode& status) {
515 if (aposMode != msgPattern.getApostropheMode()) {
516 msgPattern.clearPatternAndSetApostropheMode(aposMode);
b75a7d8f 517 }
4388f060 518 applyPattern(pattern, *parseError, status);
b75a7d8f 519}
4388f060 520
b75a7d8f 521// -------------------------------------
729e4ab9 522// Converts this MessageFormat instance to a pattern.
b75a7d8f
A
523
524UnicodeString&
525MessageFormat::toPattern(UnicodeString& appendTo) const {
4388f060
A
526 if ((customFormatArgStarts != NULL && 0 != uhash_count(customFormatArgStarts)) ||
527 0 == msgPattern.countParts()
528 ) {
529 appendTo.setToBogus();
530 return appendTo;
531 }
532 return appendTo.append(msgPattern.getPatternString());
533}
534
535int32_t MessageFormat::nextTopLevelArgStart(int32_t partIndex) const {
536 if (partIndex != 0) {
537 partIndex = msgPattern.getLimitPartIndex(partIndex);
538 }
539 for (;;) {
540 UMessagePatternPartType type = msgPattern.getPartType(++partIndex);
541 if (type == UMSGPAT_PART_TYPE_ARG_START) {
542 return partIndex;
46f4442e 543 }
4388f060
A
544 if (type == UMSGPAT_PART_TYPE_MSG_LIMIT) {
545 return -1;
729e4ab9 546 }
4388f060
A
547 }
548}
729e4ab9 549
4388f060
A
550void MessageFormat::setArgStartFormat(int32_t argStart,
551 Format* formatter,
552 UErrorCode& status) {
553 if (U_FAILURE(status)) {
554 delete formatter;
57a6839d 555 return;
4388f060
A
556 }
557 if (cachedFormatters == NULL) {
558 cachedFormatters=uhash_open(uhash_hashLong, uhash_compareLong,
559 equalFormatsForHash, &status);
560 if (U_FAILURE(status)) {
561 delete formatter;
562 return;
729e4ab9 563 }
4388f060
A
564 uhash_setValueDeleter(cachedFormatters, uprv_deleteUObject);
565 }
566 if (formatter == NULL) {
567 formatter = new DummyFormat();
568 }
569 uhash_iput(cachedFormatters, argStart, formatter, &status);
570}
729e4ab9 571
4388f060
A
572
573UBool MessageFormat::argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber) {
574 const MessagePattern::Part& part = msgPattern.getPart(partIndex);
575 return part.getType() == UMSGPAT_PART_TYPE_ARG_NAME ?
576 msgPattern.partSubstringMatches(part, argName) :
577 part.getValue() == argNumber; // ARG_NUMBER
578}
579
580// Sets a custom formatter for a MessagePattern ARG_START part index.
581// "Custom" formatters are provided by the user via setFormat() or similar APIs.
582void MessageFormat::setCustomArgStartFormat(int32_t argStart,
583 Format* formatter,
584 UErrorCode& status) {
585 setArgStartFormat(argStart, formatter, status);
586 if (customFormatArgStarts == NULL) {
587 customFormatArgStarts=uhash_open(uhash_hashLong, uhash_compareLong,
588 NULL, &status);
589 }
590 uhash_iputi(customFormatArgStarts, argStart, 1, &status);
591}
592
593Format* MessageFormat::getCachedFormatter(int32_t argumentNumber) const {
594 if (cachedFormatters == NULL) {
595 return NULL;
596 }
597 void* ptr = uhash_iget(cachedFormatters, argumentNumber);
598 if (ptr != NULL && dynamic_cast<DummyFormat*>((Format*)ptr) == NULL) {
599 return (Format*) ptr;
600 } else {
601 // Not cached, or a DummyFormat representing setFormat(NULL).
602 return NULL;
b75a7d8f 603 }
b75a7d8f 604}
729e4ab9 605
b75a7d8f
A
606// -------------------------------------
607// Adopts the new formats array and updates the array count.
608// This MessageFormat instance owns the new formats.
b75a7d8f
A
609void
610MessageFormat::adoptFormats(Format** newFormats,
611 int32_t count) {
612 if (newFormats == NULL || count < 0) {
613 return;
614 }
4388f060
A
615 // Throw away any cached formatters.
616 if (cachedFormatters != NULL) {
617 uhash_removeAll(cachedFormatters);
618 }
619 if (customFormatArgStarts != NULL) {
620 uhash_removeAll(customFormatArgStarts);
621 }
729e4ab9 622
4388f060
A
623 int32_t formatNumber = 0;
624 UErrorCode status = U_ZERO_ERROR;
625 for (int32_t partIndex = 0;
626 formatNumber < count && U_SUCCESS(status) &&
627 (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
628 setCustomArgStartFormat(partIndex, newFormats[formatNumber], status);
629 ++formatNumber;
630 }
631 // Delete those that didn't get used (if any).
632 for (; formatNumber < count; ++formatNumber) {
633 delete newFormats[formatNumber];
b75a7d8f
A
634 }
635
729e4ab9 636}
b75a7d8f
A
637
638// -------------------------------------
639// Sets the new formats array and updates the array count.
640// This MessageFormat instance maks a copy of the new formats.
729e4ab9 641
b75a7d8f
A
642void
643MessageFormat::setFormats(const Format** newFormats,
644 int32_t count) {
645 if (newFormats == NULL || count < 0) {
646 return;
647 }
4388f060
A
648 // Throw away any cached formatters.
649 if (cachedFormatters != NULL) {
650 uhash_removeAll(cachedFormatters);
651 }
652 if (customFormatArgStarts != NULL) {
653 uhash_removeAll(customFormatArgStarts);
b75a7d8f
A
654 }
655
4388f060
A
656 UErrorCode status = U_ZERO_ERROR;
657 int32_t formatNumber = 0;
658 for (int32_t partIndex = 0;
659 formatNumber < count && U_SUCCESS(status) && (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
660 Format* newFormat = NULL;
661 if (newFormats[formatNumber] != NULL) {
662 newFormat = newFormats[formatNumber]->clone();
663 if (newFormat == NULL) {
664 status = U_MEMORY_ALLOCATION_ERROR;
665 }
666 }
667 setCustomArgStartFormat(partIndex, newFormat, status);
668 ++formatNumber;
669 }
670 if (U_FAILURE(status)) {
671 resetPattern();
672 }
729e4ab9
A
673}
674
b75a7d8f 675// -------------------------------------
46f4442e
A
676// Adopt a single format by format number.
677// Do nothing if the format number is not less than the array count.
729e4ab9 678
b75a7d8f
A
679void
680MessageFormat::adoptFormat(int32_t n, Format *newFormat) {
4388f060
A
681 LocalPointer<Format> p(newFormat);
682 if (n >= 0) {
683 int32_t formatNumber = 0;
684 for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
685 if (n == formatNumber) {
686 UErrorCode status = U_ZERO_ERROR;
687 setCustomArgStartFormat(partIndex, p.orphan(), status);
688 return;
689 }
690 ++formatNumber;
691 }
b75a7d8f
A
692 }
693}
694
46f4442e
A
695// -------------------------------------
696// Adopt a single format by format name.
697// Do nothing if there is no match of formatName.
698void
729e4ab9 699MessageFormat::adoptFormat(const UnicodeString& formatName,
46f4442e
A
700 Format* formatToAdopt,
701 UErrorCode& status) {
4388f060
A
702 LocalPointer<Format> p(formatToAdopt);
703 if (U_FAILURE(status)) {
704 return;
705 }
706 int32_t argNumber = MessagePattern::validateArgumentName(formatName);
707 if (argNumber < UMSGPAT_ARG_NAME_NOT_NUMBER) {
708 status = U_ILLEGAL_ARGUMENT_ERROR;
46f4442e
A
709 return;
710 }
4388f060
A
711 for (int32_t partIndex = 0;
712 (partIndex = nextTopLevelArgStart(partIndex)) >= 0 && U_SUCCESS(status);
713 ) {
714 if (argNameMatches(partIndex + 1, formatName, argNumber)) {
715 Format* f;
716 if (p.isValid()) {
717 f = p.orphan();
718 } else if (formatToAdopt == NULL) {
719 f = NULL;
46f4442e 720 } else {
4388f060
A
721 f = formatToAdopt->clone();
722 if (f == NULL) {
723 status = U_MEMORY_ALLOCATION_ERROR;
724 return;
725 }
46f4442e 726 }
4388f060 727 setCustomArgStartFormat(partIndex, f, status);
46f4442e
A
728 }
729 }
730}
731
b75a7d8f
A
732// -------------------------------------
733// Set a single format.
46f4442e 734// Do nothing if the variable is not less than the array count.
b75a7d8f
A
735void
736MessageFormat::setFormat(int32_t n, const Format& newFormat) {
4388f060
A
737
738 if (n >= 0) {
739 int32_t formatNumber = 0;
740 for (int32_t partIndex = 0;
741 (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
742 if (n == formatNumber) {
743 Format* new_format = newFormat.clone();
744 if (new_format) {
745 UErrorCode status = U_ZERO_ERROR;
746 setCustomArgStartFormat(partIndex, new_format, status);
747 }
748 return;
749 }
750 ++formatNumber;
b75a7d8f
A
751 }
752 }
753}
46f4442e
A
754
755// -------------------------------------
756// Get a single format by format name.
757// Do nothing if the variable is not less than the array count.
758Format *
759MessageFormat::getFormat(const UnicodeString& formatName, UErrorCode& status) {
4388f060 760 if (U_FAILURE(status) || cachedFormatters == NULL) return NULL;
46f4442e 761
4388f060
A
762 int32_t argNumber = MessagePattern::validateArgumentName(formatName);
763 if (argNumber < UMSGPAT_ARG_NAME_NOT_NUMBER) {
764 status = U_ILLEGAL_ARGUMENT_ERROR;
765 return NULL;
46f4442e 766 }
4388f060
A
767 for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
768 if (argNameMatches(partIndex + 1, formatName, argNumber)) {
769 return getCachedFormatter(partIndex);
46f4442e
A
770 }
771 }
772 return NULL;
773}
774
775// -------------------------------------
776// Set a single format by format name
777// Do nothing if the variable is not less than the array count.
778void
779MessageFormat::setFormat(const UnicodeString& formatName,
780 const Format& newFormat,
781 UErrorCode& status) {
4388f060
A
782 if (U_FAILURE(status)) return;
783
784 int32_t argNumber = MessagePattern::validateArgumentName(formatName);
785 if (argNumber < UMSGPAT_ARG_NAME_NOT_NUMBER) {
786 status = U_ILLEGAL_ARGUMENT_ERROR;
46f4442e
A
787 return;
788 }
4388f060
A
789 for (int32_t partIndex = 0;
790 (partIndex = nextTopLevelArgStart(partIndex)) >= 0 && U_SUCCESS(status);
791 ) {
792 if (argNameMatches(partIndex + 1, formatName, argNumber)) {
b331163b
A
793 Format* new_format = newFormat.clone();
794 if (new_format == NULL) {
795 status = U_MEMORY_ALLOCATION_ERROR;
796 return;
46f4442e 797 }
b331163b 798 setCustomArgStartFormat(partIndex, new_format, status);
46f4442e
A
799 }
800 }
801}
802
b75a7d8f
A
803// -------------------------------------
804// Gets the format array.
b75a7d8f
A
805const Format**
806MessageFormat::getFormats(int32_t& cnt) const
807{
808 // This old API returns an array (which we hold) of Format*
809 // pointers. The array is valid up to the next call to any
810 // method on this object. We construct and resize an array
811 // on demand that contains aliases to the subformats[i].format
812 // pointers.
3d1f044b
A
813
814 // Get total required capacity first (it's refreshed on each call).
815 int32_t totalCapacity = 0;
816 for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0; ++totalCapacity) {};
817
4388f060 818 MessageFormat* t = const_cast<MessageFormat*> (this);
b75a7d8f 819 cnt = 0;
3d1f044b
A
820 if (formatAliases == nullptr) {
821 t->formatAliasesCapacity = totalCapacity;
b75a7d8f
A
822 Format** a = (Format**)
823 uprv_malloc(sizeof(Format*) * formatAliasesCapacity);
3d1f044b 824 if (a == nullptr) {
4388f060 825 t->formatAliasesCapacity = 0;
3d1f044b 826 return nullptr;
b75a7d8f 827 }
729e4ab9 828 t->formatAliases = a;
3d1f044b 829 } else if (totalCapacity > formatAliasesCapacity) {
b75a7d8f 830 Format** a = (Format**)
3d1f044b
A
831 uprv_realloc(formatAliases, sizeof(Format*) * totalCapacity);
832 if (a == nullptr) {
4388f060 833 t->formatAliasesCapacity = 0;
3d1f044b 834 return nullptr;
b75a7d8f
A
835 }
836 t->formatAliases = a;
3d1f044b 837 t->formatAliasesCapacity = totalCapacity;
b75a7d8f 838 }
4388f060
A
839
840 for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
841 t->formatAliases[cnt++] = getCachedFormatter(partIndex);
b75a7d8f 842 }
4388f060 843
b75a7d8f
A
844 return (const Format**)formatAliases;
845}
729e4ab9 846
46f4442e 847
4388f060
A
848UnicodeString MessageFormat::getArgName(int32_t partIndex) {
849 const MessagePattern::Part& part = msgPattern.getPart(partIndex);
57a6839d 850 return msgPattern.getSubstring(part);
4388f060
A
851}
852
46f4442e
A
853StringEnumeration*
854MessageFormat::getFormatNames(UErrorCode& status) {
855 if (U_FAILURE(status)) return NULL;
729e4ab9 856
46f4442e
A
857 UVector *fFormatNames = new UVector(status);
858 if (U_FAILURE(status)) {
859 status = U_MEMORY_ALLOCATION_ERROR;
860 return NULL;
861 }
4388f060
A
862 fFormatNames->setDeleter(uprv_deleteUObject);
863
864 for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
51004dcb 865 fFormatNames->addElement(new UnicodeString(getArgName(partIndex + 1)), status);
46f4442e
A
866 }
867
868 StringEnumeration* nameEnumerator = new FormatNameEnumeration(fFormatNames, status);
869 return nameEnumerator;
870}
871
b75a7d8f
A
872// -------------------------------------
873// Formats the source Formattable array and copy into the result buffer.
874// Ignore the FieldPosition result for error checking.
729e4ab9 875
b75a7d8f
A
876UnicodeString&
877MessageFormat::format(const Formattable* source,
729e4ab9
A
878 int32_t cnt,
879 UnicodeString& appendTo,
880 FieldPosition& ignore,
b75a7d8f
A
881 UErrorCode& success) const
882{
4388f060 883 return format(source, NULL, cnt, appendTo, &ignore, success);
b75a7d8f 884}
729e4ab9 885
b75a7d8f
A
886// -------------------------------------
887// Internally creates a MessageFormat instance based on the
729e4ab9 888// pattern and formats the arguments Formattable array and
b75a7d8f 889// copy into the appendTo buffer.
729e4ab9 890
b75a7d8f
A
891UnicodeString&
892MessageFormat::format( const UnicodeString& pattern,
893 const Formattable* arguments,
894 int32_t cnt,
729e4ab9 895 UnicodeString& appendTo,
b75a7d8f
A
896 UErrorCode& success)
897{
898 MessageFormat temp(pattern, success);
4388f060 899 return temp.format(arguments, NULL, cnt, appendTo, NULL, success);
b75a7d8f 900}
729e4ab9 901
b75a7d8f 902// -------------------------------------
729e4ab9 903// Formats the source Formattable object and copy into the
b75a7d8f
A
904// appendTo buffer. The Formattable object must be an array
905// of Formattable instances, returns error otherwise.
729e4ab9 906
b75a7d8f 907UnicodeString&
729e4ab9
A
908MessageFormat::format(const Formattable& source,
909 UnicodeString& appendTo,
910 FieldPosition& ignore,
b75a7d8f
A
911 UErrorCode& success) const
912{
729e4ab9 913 if (U_FAILURE(success))
b75a7d8f
A
914 return appendTo;
915 if (source.getType() != Formattable::kArray) {
916 success = U_ILLEGAL_ARGUMENT_ERROR;
917 return appendTo;
918 }
4388f060 919 int32_t cnt;
b75a7d8f 920 const Formattable* tmpPtr = source.getArray(cnt);
4388f060 921 return format(tmpPtr, NULL, cnt, appendTo, &ignore, success);
b75a7d8f 922}
46f4442e 923
46f4442e
A
924UnicodeString&
925MessageFormat::format(const UnicodeString* argumentNames,
926 const Formattable* arguments,
927 int32_t count,
928 UnicodeString& appendTo,
929 UErrorCode& success) const {
4388f060 930 return format(arguments, argumentNames, count, appendTo, NULL, success);
46f4442e
A
931}
932
4388f060
A
933// Does linear search to find the match for an ArgName.
934const Formattable* MessageFormat::getArgFromListByName(const Formattable* arguments,
935 const UnicodeString *argumentNames,
936 int32_t cnt, UnicodeString& name) const {
937 for (int32_t i = 0; i < cnt; ++i) {
938 if (0 == argumentNames[i].compare(name)) {
939 return arguments + i;
940 }
941 }
942 return NULL;
46f4442e
A
943}
944
b75a7d8f
A
945
946UnicodeString&
46f4442e
A
947MessageFormat::format(const Formattable* arguments,
948 const UnicodeString *argumentNames,
729e4ab9
A
949 int32_t cnt,
950 UnicodeString& appendTo,
4388f060
A
951 FieldPosition* pos,
952 UErrorCode& status) const {
953 if (U_FAILURE(status)) {
b75a7d8f
A
954 return appendTo;
955 }
729e4ab9 956
4388f060
A
957 UnicodeStringAppendable usapp(appendTo);
958 AppendableWrapper app(usapp);
57a6839d 959 format(0, NULL, arguments, argumentNames, cnt, app, pos, status);
4388f060
A
960 return appendTo;
961}
962
57a6839d
A
963namespace {
964
965/**
966 * Mutable input/output values for the PluralSelectorProvider.
967 * Separate so that it is possible to make MessageFormat Freezable.
968 */
969class PluralSelectorContext {
970public:
971 PluralSelectorContext(int32_t start, const UnicodeString &name,
972 const Formattable &num, double off, UErrorCode &errorCode)
973 : startIndex(start), argName(name), offset(off),
974 numberArgIndex(-1), formatter(NULL), forReplaceNumber(FALSE) {
975 // number needs to be set even when select() is not called.
976 // Keep it as a Number/Formattable:
977 // For format() methods, and to preserve information (e.g., BigDecimal).
978 if(off == 0) {
979 number = num;
980 } else {
981 number = num.getDouble(errorCode) - off;
982 }
983 }
984
985 // Input values for plural selection with decimals.
986 int32_t startIndex;
987 const UnicodeString &argName;
988 /** argument number - plural offset */
989 Formattable number;
990 double offset;
991 // Output values for plural selection with decimals.
992 /** -1 if REPLACE_NUMBER, 0 arg not found, >0 ARG_START index */
993 int32_t numberArgIndex;
994 const Format *formatter;
995 /** formatted argument number - plural offset */
996 UnicodeString numberString;
997 /** TRUE if number-offset was formatted with the stock number formatter */
998 UBool forReplaceNumber;
999};
1000
1001} // namespace
1002
4388f060
A
1003// if argumentNames is NULL, this means arguments is a numeric array.
1004// arguments can not be NULL.
57a6839d
A
1005// We use const void *plNumber rather than const PluralSelectorContext *pluralNumber
1006// so that we need not declare the PluralSelectorContext in the public header file.
1007void MessageFormat::format(int32_t msgStart, const void *plNumber,
4388f060
A
1008 const Formattable* arguments,
1009 const UnicodeString *argumentNames,
1010 int32_t cnt,
1011 AppendableWrapper& appendTo,
1012 FieldPosition* ignore,
1013 UErrorCode& success) const {
1014 if (U_FAILURE(success)) {
1015 return;
46f4442e 1016 }
729e4ab9 1017
4388f060
A
1018 const UnicodeString& msgString = msgPattern.getPatternString();
1019 int32_t prevIndex = msgPattern.getPart(msgStart).getLimit();
1020 for (int32_t i = msgStart + 1; U_SUCCESS(success) ; ++i) {
1021 const MessagePattern::Part* part = &msgPattern.getPart(i);
1022 const UMessagePatternPartType type = part->getType();
1023 int32_t index = part->getIndex();
1024 appendTo.append(msgString, prevIndex, index - prevIndex);
1025 if (type == UMSGPAT_PART_TYPE_MSG_LIMIT) {
1026 return;
46f4442e 1027 }
4388f060
A
1028 prevIndex = part->getLimit();
1029 if (type == UMSGPAT_PART_TYPE_REPLACE_NUMBER) {
57a6839d
A
1030 const PluralSelectorContext &pluralNumber =
1031 *static_cast<const PluralSelectorContext *>(plNumber);
1032 if(pluralNumber.forReplaceNumber) {
1033 // number-offset was already formatted.
1034 appendTo.formatAndAppend(pluralNumber.formatter,
1035 pluralNumber.number, pluralNumber.numberString, success);
1036 } else {
1037 const NumberFormat* nf = getDefaultNumberFormat(success);
1038 appendTo.formatAndAppend(nf, pluralNumber.number, success);
1039 }
4388f060
A
1040 continue;
1041 }
1042 if (type != UMSGPAT_PART_TYPE_ARG_START) {
1043 continue;
1044 }
1045 int32_t argLimit = msgPattern.getLimitPartIndex(i);
1046 UMessagePatternArgType argType = part->getArgType();
1047 part = &msgPattern.getPart(++i);
1048 const Formattable* arg;
57a6839d
A
1049 UBool noArg = FALSE;
1050 UnicodeString argName = msgPattern.getSubstring(*part);
4388f060
A
1051 if (argumentNames == NULL) {
1052 int32_t argNumber = part->getValue(); // ARG_NUMBER
1053 if (0 <= argNumber && argNumber < cnt) {
1054 arg = arguments + argNumber;
1055 } else {
1056 arg = NULL;
57a6839d 1057 noArg = TRUE;
46f4442e 1058 }
4388f060 1059 } else {
57a6839d 1060 arg = getArgFromListByName(arguments, argumentNames, cnt, argName);
4388f060 1061 if (arg == NULL) {
57a6839d 1062 noArg = TRUE;
46f4442e 1063 }
b75a7d8f 1064 }
4388f060
A
1065 ++i;
1066 int32_t prevDestLength = appendTo.length();
1067 const Format* formatter = NULL;
57a6839d
A
1068 if (noArg) {
1069 appendTo.append(
1070 UnicodeString(LEFT_CURLY_BRACE).append(argName).append(RIGHT_CURLY_BRACE));
4388f060
A
1071 } else if (arg == NULL) {
1072 appendTo.append(NULL_STRING, 4);
57a6839d
A
1073 } else if(plNumber!=NULL &&
1074 static_cast<const PluralSelectorContext *>(plNumber)->numberArgIndex==(i-2)) {
1075 const PluralSelectorContext &pluralNumber =
1076 *static_cast<const PluralSelectorContext *>(plNumber);
1077 if(pluralNumber.offset == 0) {
1078 // The number was already formatted with this formatter.
1079 appendTo.formatAndAppend(pluralNumber.formatter, pluralNumber.number,
1080 pluralNumber.numberString, success);
1081 } else {
1082 // Do not use the formatted (number-offset) string for a named argument
1083 // that formats the number without subtracting the offset.
1084 appendTo.formatAndAppend(pluralNumber.formatter, *arg, success);
1085 }
3d1f044b 1086 } else if ((formatter = getCachedFormatter(i -2)) != 0) {
4388f060
A
1087 // Handles all ArgType.SIMPLE, and formatters from setFormat() and its siblings.
1088 if (dynamic_cast<const ChoiceFormat*>(formatter) ||
1089 dynamic_cast<const PluralFormat*>(formatter) ||
1090 dynamic_cast<const SelectFormat*>(formatter)) {
1091 // We only handle nested formats here if they were provided via
1092 // setFormat() or its siblings. Otherwise they are not cached and instead
1093 // handled below according to argType.
1094 UnicodeString subMsgString;
1095 formatter->format(*arg, subMsgString, success);
1096 if (subMsgString.indexOf(LEFT_CURLY_BRACE) >= 0 ||
1097 (subMsgString.indexOf(SINGLE_QUOTE) >= 0 && !MessageImpl::jdkAposMode(msgPattern))
1098 ) {
1099 MessageFormat subMsgFormat(subMsgString, fLocale, success);
57a6839d 1100 subMsgFormat.format(0, NULL, arguments, argumentNames, cnt, appendTo, ignore, success);
4388f060
A
1101 } else {
1102 appendTo.append(subMsgString);
b75a7d8f 1103 }
4388f060
A
1104 } else {
1105 appendTo.formatAndAppend(formatter, *arg, success);
b75a7d8f 1106 }
4388f060
A
1107 } else if (argType == UMSGPAT_ARG_TYPE_NONE || (cachedFormatters && uhash_iget(cachedFormatters, i - 2))) {
1108 // We arrive here if getCachedFormatter returned NULL, but there was actually an element in the hash table.
1109 // This can only happen if the hash table contained a DummyFormat, so the if statement above is a check
1110 // for the hash table containind DummyFormat.
1111 if (arg->isNumeric()) {
1112 const NumberFormat* nf = getDefaultNumberFormat(success);
1113 appendTo.formatAndAppend(nf, *arg, success);
1114 } else if (arg->getType() == Formattable::kDate) {
1115 const DateFormat* df = getDefaultDateFormat(success);
1116 appendTo.formatAndAppend(df, *arg, success);
1117 } else {
1118 appendTo.append(arg->getString(success));
1119 }
1120 } else if (argType == UMSGPAT_ARG_TYPE_CHOICE) {
1121 if (!arg->isNumeric()) {
1122 success = U_ILLEGAL_ARGUMENT_ERROR;
1123 return;
b75a7d8f 1124 }
4388f060
A
1125 // We must use the Formattable::getDouble() variant with the UErrorCode parameter
1126 // because only this one converts non-double numeric types to double.
1127 const double number = arg->getDouble(success);
1128 int32_t subMsgStart = ChoiceFormat::findSubMessage(msgPattern, i, number);
57a6839d 1129 formatComplexSubMessage(subMsgStart, NULL, arguments, argumentNames,
4388f060 1130 cnt, appendTo, success);
51004dcb 1131 } else if (UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType)) {
4388f060
A
1132 if (!arg->isNumeric()) {
1133 success = U_ILLEGAL_ARGUMENT_ERROR;
1134 return;
1135 }
57a6839d 1136 const PluralSelectorProvider &selector =
51004dcb 1137 argType == UMSGPAT_ARG_TYPE_PLURAL ? pluralProvider : ordinalProvider;
4388f060
A
1138 // We must use the Formattable::getDouble() variant with the UErrorCode parameter
1139 // because only this one converts non-double numeric types to double.
4388f060 1140 double offset = msgPattern.getPluralOffset(i);
57a6839d
A
1141 PluralSelectorContext context(i, argName, *arg, offset, success);
1142 int32_t subMsgStart = PluralFormat::findSubMessage(
1143 msgPattern, i, selector, &context, arg->getDouble(success), success);
1144 formatComplexSubMessage(subMsgStart, &context, arguments, argumentNames,
4388f060
A
1145 cnt, appendTo, success);
1146 } else if (argType == UMSGPAT_ARG_TYPE_SELECT) {
1147 int32_t subMsgStart = SelectFormat::findSubMessage(msgPattern, i, arg->getString(success), success);
57a6839d 1148 formatComplexSubMessage(subMsgStart, NULL, arguments, argumentNames,
4388f060
A
1149 cnt, appendTo, success);
1150 } else {
1151 // This should never happen.
1152 success = U_INTERNAL_PROGRAM_ERROR;
1153 return;
b75a7d8f 1154 }
4388f060
A
1155 ignore = updateMetaData(appendTo, prevDestLength, ignore, arg);
1156 prevIndex = msgPattern.getPart(argLimit).getLimit();
1157 i = argLimit;
1158 }
1159}
374ca955 1160
4388f060
A
1161
1162void MessageFormat::formatComplexSubMessage(int32_t msgStart,
57a6839d 1163 const void *plNumber,
4388f060
A
1164 const Formattable* arguments,
1165 const UnicodeString *argumentNames,
1166 int32_t cnt,
1167 AppendableWrapper& appendTo,
1168 UErrorCode& success) const {
1169 if (U_FAILURE(success)) {
1170 return;
1171 }
1172
1173 if (!MessageImpl::jdkAposMode(msgPattern)) {
57a6839d 1174 format(msgStart, plNumber, arguments, argumentNames, cnt, appendTo, NULL, success);
4388f060
A
1175 return;
1176 }
1177
1178 // JDK compatibility mode: (see JDK MessageFormat.format() API docs)
1179 // - remove SKIP_SYNTAX; that is, remove half of the apostrophes
1180 // - if the result string contains an open curly brace '{' then
1181 // instantiate a temporary MessageFormat object and format again;
1182 // otherwise just append the result string
1183 const UnicodeString& msgString = msgPattern.getPatternString();
1184 UnicodeString sb;
1185 int32_t prevIndex = msgPattern.getPart(msgStart).getLimit();
1186 for (int32_t i = msgStart;;) {
1187 const MessagePattern::Part& part = msgPattern.getPart(++i);
1188 const UMessagePatternPartType type = part.getType();
1189 int32_t index = part.getIndex();
1190 if (type == UMSGPAT_PART_TYPE_MSG_LIMIT) {
1191 sb.append(msgString, prevIndex, index - prevIndex);
1192 break;
1193 } else if (type == UMSGPAT_PART_TYPE_REPLACE_NUMBER || type == UMSGPAT_PART_TYPE_SKIP_SYNTAX) {
1194 sb.append(msgString, prevIndex, index - prevIndex);
1195 if (type == UMSGPAT_PART_TYPE_REPLACE_NUMBER) {
57a6839d
A
1196 const PluralSelectorContext &pluralNumber =
1197 *static_cast<const PluralSelectorContext *>(plNumber);
1198 if(pluralNumber.forReplaceNumber) {
1199 // number-offset was already formatted.
1200 sb.append(pluralNumber.numberString);
1201 } else {
1202 const NumberFormat* nf = getDefaultNumberFormat(success);
1203 sb.append(nf->format(pluralNumber.number, sb, success));
1204 }
b75a7d8f 1205 }
4388f060
A
1206 prevIndex = part.getLimit();
1207 } else if (type == UMSGPAT_PART_TYPE_ARG_START) {
1208 sb.append(msgString, prevIndex, index - prevIndex);
1209 prevIndex = index;
1210 i = msgPattern.getLimitPartIndex(i);
1211 index = msgPattern.getPart(i).getLimit();
1212 MessageImpl::appendReducedApostrophes(msgString, prevIndex, index, sb);
1213 prevIndex = index;
1214 }
1215 }
1216 if (sb.indexOf(LEFT_CURLY_BRACE) >= 0) {
1217 UnicodeString emptyPattern; // gcc 3.3.3 fails with "UnicodeString()" as the first parameter.
1218 MessageFormat subMsgFormat(emptyPattern, fLocale, success);
1219 subMsgFormat.applyPattern(sb, UMSGPAT_APOS_DOUBLE_REQUIRED, NULL, success);
57a6839d 1220 subMsgFormat.format(0, NULL, arguments, argumentNames, cnt, appendTo, NULL, success);
4388f060
A
1221 } else {
1222 appendTo.append(sb);
1223 }
1224}
1225
1226
1227UnicodeString MessageFormat::getLiteralStringUntilNextArgument(int32_t from) const {
1228 const UnicodeString& msgString=msgPattern.getPatternString();
1229 int32_t prevIndex=msgPattern.getPart(from).getLimit();
1230 UnicodeString b;
1231 for (int32_t i = from + 1; ; ++i) {
1232 const MessagePattern::Part& part = msgPattern.getPart(i);
1233 const UMessagePatternPartType type=part.getType();
1234 int32_t index=part.getIndex();
1235 b.append(msgString, prevIndex, index - prevIndex);
1236 if(type==UMSGPAT_PART_TYPE_ARG_START || type==UMSGPAT_PART_TYPE_MSG_LIMIT) {
1237 return b;
1238 }
1239 // Unexpected Part "part" in parsed message.
1240 U_ASSERT(type==UMSGPAT_PART_TYPE_SKIP_SYNTAX || type==UMSGPAT_PART_TYPE_INSERT_CHAR);
1241 prevIndex=part.getLimit();
1242 }
1243}
1244
1245
1246FieldPosition* MessageFormat::updateMetaData(AppendableWrapper& /*dest*/, int32_t /*prevLength*/,
1247 FieldPosition* /*fp*/, const Formattable* /*argId*/) const {
1248 // Unlike in Java, there are no field attributes defined for MessageFormat. Do nothing.
1249 return NULL;
1250 /*
1251 if (fp != NULL && Field.ARGUMENT.equals(fp.getFieldAttribute())) {
1252 fp->setBeginIndex(prevLength);
1253 fp->setEndIndex(dest.get_length());
1254 return NULL;
1255 }
1256 return fp;
1257 */
1258}
1259
57a6839d
A
1260int32_t
1261MessageFormat::findOtherSubMessage(int32_t partIndex) const {
1262 int32_t count=msgPattern.countParts();
1263 const MessagePattern::Part *part = &msgPattern.getPart(partIndex);
1264 if(MessagePattern::Part::hasNumericValue(part->getType())) {
1265 ++partIndex;
1266 }
1267 // Iterate over (ARG_SELECTOR [ARG_INT|ARG_DOUBLE] message) tuples
1268 // until ARG_LIMIT or end of plural-only pattern.
1269 UnicodeString other(FALSE, OTHER_STRING, 5);
1270 do {
1271 part=&msgPattern.getPart(partIndex++);
1272 UMessagePatternPartType type=part->getType();
1273 if(type==UMSGPAT_PART_TYPE_ARG_LIMIT) {
1274 break;
1275 }
1276 U_ASSERT(type==UMSGPAT_PART_TYPE_ARG_SELECTOR);
1277 // part is an ARG_SELECTOR followed by an optional explicit value, and then a message
1278 if(msgPattern.partSubstringMatches(*part, other)) {
1279 return partIndex;
1280 }
1281 if(MessagePattern::Part::hasNumericValue(msgPattern.getPartType(partIndex))) {
1282 ++partIndex; // skip the numeric-value part of "=1" etc.
1283 }
1284 partIndex=msgPattern.getLimitPartIndex(partIndex);
1285 } while(++partIndex<count);
1286 return 0;
1287}
1288
1289int32_t
1290MessageFormat::findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) const {
1291 for(int32_t i=msgStart+1;; ++i) {
1292 const MessagePattern::Part &part=msgPattern.getPart(i);
1293 UMessagePatternPartType type=part.getType();
1294 if(type==UMSGPAT_PART_TYPE_MSG_LIMIT) {
1295 return 0;
1296 }
1297 if(type==UMSGPAT_PART_TYPE_REPLACE_NUMBER) {
1298 return -1;
1299 }
1300 if(type==UMSGPAT_PART_TYPE_ARG_START) {
1301 UMessagePatternArgType argType=part.getArgType();
1302 if(!argName.isEmpty() && (argType==UMSGPAT_ARG_TYPE_NONE || argType==UMSGPAT_ARG_TYPE_SIMPLE)) {
1303 // ARG_NUMBER or ARG_NAME
1304 if(msgPattern.partSubstringMatches(msgPattern.getPart(i+1), argName)) {
1305 return i;
1306 }
1307 }
1308 i=msgPattern.getLimitPartIndex(i);
1309 }
1310 }
1311}
1312
4388f060
A
1313void MessageFormat::copyObjects(const MessageFormat& that, UErrorCode& ec) {
1314 // Deep copy pointer fields.
1315 // We need not copy the formatAliases because they are re-filled
1316 // in each getFormats() call.
1317 // The defaultNumberFormat, defaultDateFormat and pluralProvider.rules
1318 // also get created on demand.
1319 argTypeCount = that.argTypeCount;
1320 if (argTypeCount > 0) {
1321 if (!allocateArgTypes(argTypeCount, ec)) {
1322 return;
1323 }
1324 uprv_memcpy(argTypes, that.argTypes, argTypeCount * sizeof(argTypes[0]));
1325 }
1326 if (cachedFormatters != NULL) {
1327 uhash_removeAll(cachedFormatters);
1328 }
1329 if (customFormatArgStarts != NULL) {
1330 uhash_removeAll(customFormatArgStarts);
1331 }
1332 if (that.cachedFormatters) {
1333 if (cachedFormatters == NULL) {
1334 cachedFormatters=uhash_open(uhash_hashLong, uhash_compareLong,
1335 equalFormatsForHash, &ec);
1336 if (U_FAILURE(ec)) {
1337 return;
b75a7d8f 1338 }
4388f060 1339 uhash_setValueDeleter(cachedFormatters, uprv_deleteUObject);
b75a7d8f 1340 }
4388f060
A
1341
1342 const int32_t count = uhash_count(that.cachedFormatters);
1343 int32_t pos, idx;
b331163b 1344 for (idx = 0, pos = UHASH_FIRST; idx < count && U_SUCCESS(ec); ++idx) {
4388f060
A
1345 const UHashElement* cur = uhash_nextElement(that.cachedFormatters, &pos);
1346 Format* newFormat = ((Format*)(cur->value.pointer))->clone();
1347 if (newFormat) {
1348 uhash_iput(cachedFormatters, cur->key.integer, newFormat, &ec);
1349 } else {
1350 ec = U_MEMORY_ALLOCATION_ERROR;
1351 return;
b75a7d8f 1352 }
b75a7d8f 1353 }
4388f060
A
1354 }
1355 if (that.customFormatArgStarts) {
1356 if (customFormatArgStarts == NULL) {
1357 customFormatArgStarts=uhash_open(uhash_hashLong, uhash_compareLong,
1358 NULL, &ec);
b75a7d8f 1359 }
4388f060
A
1360 const int32_t count = uhash_count(that.customFormatArgStarts);
1361 int32_t pos, idx;
b331163b 1362 for (idx = 0, pos = UHASH_FIRST; idx < count && U_SUCCESS(ec); ++idx) {
4388f060
A
1363 const UHashElement* cur = uhash_nextElement(that.customFormatArgStarts, &pos);
1364 uhash_iputi(customFormatArgStarts, cur->key.integer, cur->value.integer, &ec);
b75a7d8f
A
1365 }
1366 }
b75a7d8f
A
1367}
1368
1369
b75a7d8f 1370Formattable*
4388f060
A
1371MessageFormat::parse(int32_t msgStart,
1372 const UnicodeString& source,
b75a7d8f 1373 ParsePosition& pos,
4388f060
A
1374 int32_t& count,
1375 UErrorCode& ec) const {
1376 count = 0;
1377 if (U_FAILURE(ec)) {
1378 pos.setErrorIndex(pos.getIndex());
1379 return NULL;
1380 }
1381 // parse() does not work with named arguments.
1382 if (msgPattern.hasNamedArguments()) {
1383 ec = U_ARGUMENT_TYPE_MISMATCH;
1384 pos.setErrorIndex(pos.getIndex());
1385 return NULL;
1386 }
1387 LocalArray<Formattable> resultArray(new Formattable[argTypeCount ? argTypeCount : 1]);
1388 const UnicodeString& msgString=msgPattern.getPatternString();
1389 int32_t prevIndex=msgPattern.getPart(msgStart).getLimit();
b75a7d8f 1390 int32_t sourceOffset = pos.getIndex();
4388f060
A
1391 ParsePosition tempStatus(0);
1392
1393 for(int32_t i=msgStart+1; ; ++i) {
1394 UBool haveArgResult = FALSE;
1395 const MessagePattern::Part* part=&msgPattern.getPart(i);
1396 const UMessagePatternPartType type=part->getType();
1397 int32_t index=part->getIndex();
1398 // Make sure the literal string matches.
1399 int32_t len = index - prevIndex;
1400 if (len == 0 || (0 == msgString.compare(prevIndex, len, source, sourceOffset, len))) {
b75a7d8f 1401 sourceOffset += len;
4388f060
A
1402 prevIndex += len;
1403 } else {
1404 pos.setErrorIndex(sourceOffset);
1405 return NULL; // leave index as is to signal error
729e4ab9 1406 }
4388f060
A
1407 if(type==UMSGPAT_PART_TYPE_MSG_LIMIT) {
1408 // Things went well! Done.
1409 pos.setIndex(sourceOffset);
1410 return resultArray.orphan();
b75a7d8f 1411 }
4388f060
A
1412 if(type==UMSGPAT_PART_TYPE_SKIP_SYNTAX || type==UMSGPAT_PART_TYPE_INSERT_CHAR) {
1413 prevIndex=part->getLimit();
1414 continue;
1415 }
1416 // We do not support parsing Plural formats. (No REPLACE_NUMBER here.)
1417 // Unexpected Part "part" in parsed message.
1418 U_ASSERT(type==UMSGPAT_PART_TYPE_ARG_START);
1419 int32_t argLimit=msgPattern.getLimitPartIndex(i);
1420
1421 UMessagePatternArgType argType=part->getArgType();
1422 part=&msgPattern.getPart(++i);
1423 int32_t argNumber = part->getValue(); // ARG_NUMBER
1424 UnicodeString key;
1425 ++i;
1426 const Format* formatter = NULL;
1427 Formattable& argResult = resultArray[argNumber];
1428
1429 if(cachedFormatters!=NULL && (formatter = getCachedFormatter(i - 2))!=NULL) {
1430 // Just parse using the formatter.
1431 tempStatus.setIndex(sourceOffset);
1432 formatter->parseObject(source, argResult, tempStatus);
1433 if (tempStatus.getIndex() == sourceOffset) {
1434 pos.setErrorIndex(sourceOffset);
1435 return NULL; // leave index as is to signal error
1436 }
1437 sourceOffset = tempStatus.getIndex();
1438 haveArgResult = TRUE;
1439 } else if(
1440 argType==UMSGPAT_ARG_TYPE_NONE || (cachedFormatters && uhash_iget(cachedFormatters, i -2))) {
1441 // We arrive here if getCachedFormatter returned NULL, but there was actually an element in the hash table.
1442 // This can only happen if the hash table contained a DummyFormat, so the if statement above is a check
1443 // for the hash table containind DummyFormat.
1444
1445 // Match as a string.
b75a7d8f
A
1446 // if at end, use longest possible match
1447 // otherwise uses first match to intervening string
1448 // does NOT recursively try all possibilities
4388f060 1449 UnicodeString stringAfterArgument = getLiteralStringUntilNextArgument(argLimit);
b75a7d8f 1450 int32_t next;
4388f060
A
1451 if (!stringAfterArgument.isEmpty()) {
1452 next = source.indexOf(stringAfterArgument, sourceOffset);
1453 } else {
b75a7d8f
A
1454 next = source.length();
1455 }
b75a7d8f 1456 if (next < 0) {
4388f060
A
1457 pos.setErrorIndex(sourceOffset);
1458 return NULL; // leave index as is to signal error
1459 } else {
1460 UnicodeString strValue(source.tempSubString(sourceOffset, next - sourceOffset));
1461 UnicodeString compValue;
1462 compValue.append(LEFT_CURLY_BRACE);
1463 itos(argNumber, compValue);
1464 compValue.append(RIGHT_CURLY_BRACE);
1465 if (0 != strValue.compare(compValue)) {
1466 argResult.setString(strValue);
1467 haveArgResult = TRUE;
b75a7d8f
A
1468 }
1469 sourceOffset = next;
1470 }
4388f060
A
1471 } else if(argType==UMSGPAT_ARG_TYPE_CHOICE) {
1472 tempStatus.setIndex(sourceOffset);
1473 double choiceResult = ChoiceFormat::parseArgument(msgPattern, i, source, tempStatus);
1474 if (tempStatus.getIndex() == sourceOffset) {
1475 pos.setErrorIndex(sourceOffset);
1476 return NULL; // leave index as is to signal error
b75a7d8f 1477 }
4388f060
A
1478 argResult.setDouble(choiceResult);
1479 haveArgResult = TRUE;
1480 sourceOffset = tempStatus.getIndex();
51004dcb 1481 } else if(UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) || argType==UMSGPAT_ARG_TYPE_SELECT) {
4388f060
A
1482 // Parsing not supported.
1483 ec = U_UNSUPPORTED_ERROR;
1484 return NULL;
1485 } else {
1486 // This should never happen.
1487 ec = U_INTERNAL_PROGRAM_ERROR;
1488 return NULL;
b75a7d8f 1489 }
4388f060
A
1490 if (haveArgResult && count <= argNumber) {
1491 count = argNumber + 1;
1492 }
1493 prevIndex=msgPattern.getPart(argLimit).getLimit();
1494 i=argLimit;
b75a7d8f 1495 }
4388f060
A
1496}
1497// -------------------------------------
1498// Parses the source pattern and returns the Formattable objects array,
1499// the array count and the ending parse position. The caller of this method
1500// owns the array.
b75a7d8f 1501
4388f060
A
1502Formattable*
1503MessageFormat::parse(const UnicodeString& source,
1504 ParsePosition& pos,
1505 int32_t& count) const {
1506 UErrorCode ec = U_ZERO_ERROR;
1507 return parse(0, source, pos, count, ec);
b75a7d8f 1508}
729e4ab9 1509
b75a7d8f 1510// -------------------------------------
729e4ab9
A
1511// Parses the source string and returns the array of
1512// Formattable objects and the array count. The caller
b75a7d8f 1513// owns the returned array.
729e4ab9 1514
b75a7d8f 1515Formattable*
729e4ab9 1516MessageFormat::parse(const UnicodeString& source,
b75a7d8f
A
1517 int32_t& cnt,
1518 UErrorCode& success) const
1519{
4388f060 1520 if (msgPattern.hasNamedArguments()) {
46f4442e 1521 success = U_ARGUMENT_TYPE_MISMATCH;
729e4ab9 1522 return NULL;
46f4442e 1523 }
b75a7d8f
A
1524 ParsePosition status(0);
1525 // Calls the actual implementation method and starts
1526 // from zero offset of the source text.
1527 Formattable* result = parse(source, status, cnt);
1528 if (status.getIndex() == 0) {
1529 success = U_MESSAGE_PARSE_ERROR;
1530 delete[] result;
1531 return NULL;
1532 }
1533 return result;
1534}
729e4ab9 1535
b75a7d8f
A
1536// -------------------------------------
1537// Parses the source text and copy into the result buffer.
729e4ab9 1538
b75a7d8f
A
1539void
1540MessageFormat::parseObject( const UnicodeString& source,
1541 Formattable& result,
1542 ParsePosition& status) const
1543{
1544 int32_t cnt = 0;
1545 Formattable* tmpResult = parse(source, status, cnt);
729e4ab9 1546 if (tmpResult != NULL)
b75a7d8f
A
1547 result.adoptArray(tmpResult, cnt);
1548}
729e4ab9
A
1549
1550UnicodeString
73c04bcf 1551MessageFormat::autoQuoteApostrophe(const UnicodeString& pattern, UErrorCode& status) {
4388f060
A
1552 UnicodeString result;
1553 if (U_SUCCESS(status)) {
1554 int32_t plen = pattern.length();
1555 const UChar* pat = pattern.getBuffer();
1556 int32_t blen = plen * 2 + 1; // space for null termination, convenience
1557 UChar* buf = result.getBuffer(blen);
1558 if (buf == NULL) {
1559 status = U_MEMORY_ALLOCATION_ERROR;
1560 } else {
1561 int32_t len = umsg_autoQuoteApostrophe(pat, plen, buf, blen, &status);
1562 result.releaseBuffer(U_SUCCESS(status) ? len : 0);
1563 }
1564 }
1565 if (U_FAILURE(status)) {
1566 result.setToBogus();
73c04bcf 1567 }
4388f060 1568 return result;
73c04bcf
A
1569}
1570
b75a7d8f 1571// -------------------------------------
374ca955
A
1572
1573static Format* makeRBNF(URBNFRuleSetTag tag, const Locale& locale, const UnicodeString& defaultRuleSet, UErrorCode& ec) {
1574 RuleBasedNumberFormat* fmt = new RuleBasedNumberFormat(tag, locale, ec);
46f4442e
A
1575 if (fmt == NULL) {
1576 ec = U_MEMORY_ALLOCATION_ERROR;
1577 } else if (U_SUCCESS(ec) && defaultRuleSet.length() > 0) {
1578 UErrorCode localStatus = U_ZERO_ERROR; // ignore unrecognized default rule set
1579 fmt->setDefaultRuleSet(defaultRuleSet, localStatus);
374ca955
A
1580 }
1581 return fmt;
1582}
729e4ab9 1583
4388f060
A
1584void MessageFormat::cacheExplicitFormats(UErrorCode& status) {
1585 if (U_FAILURE(status)) {
b75a7d8f
A
1586 return;
1587 }
1588
4388f060
A
1589 if (cachedFormatters != NULL) {
1590 uhash_removeAll(cachedFormatters);
46f4442e 1591 }
4388f060
A
1592 if (customFormatArgStarts != NULL) {
1593 uhash_removeAll(customFormatArgStarts);
1594 }
1595
1596 // The last two "parts" can at most be ARG_LIMIT and MSG_LIMIT
1597 // which we need not examine.
1598 int32_t limit = msgPattern.countParts() - 2;
1599 argTypeCount = 0;
1600 // We also need not look at the first two "parts"
1601 // (at most MSG_START and ARG_START) in this loop.
1602 // We determine the argTypeCount first so that we can allocateArgTypes
1603 // so that the next loop can set argTypes[argNumber].
1604 // (This is for the C API which needs the argTypes to read its va_arg list.)
1605 for (int32_t i = 2; i < limit && U_SUCCESS(status); ++i) {
1606 const MessagePattern::Part& part = msgPattern.getPart(i);
1607 if (part.getType() == UMSGPAT_PART_TYPE_ARG_NUMBER) {
1608 const int argNumber = part.getValue();
1609 if (argNumber >= argTypeCount) {
1610 argTypeCount = argNumber + 1;
1611 }
46f4442e 1612 }
b75a7d8f 1613 }
4388f060
A
1614 if (!allocateArgTypes(argTypeCount, status)) {
1615 return;
1616 }
1617 // Set all argTypes to kObject, as a "none" value, for lack of any better value.
1618 // We never use kObject for real arguments.
51004dcb 1619 // We use it as "no argument yet" for the check for hasArgTypeConflicts.
4388f060
A
1620 for (int32_t i = 0; i < argTypeCount; ++i) {
1621 argTypes[i] = Formattable::kObject;
1622 }
1623 hasArgTypeConflicts = FALSE;
b75a7d8f 1624
4388f060
A
1625 // This loop starts at part index 1 because we do need to examine
1626 // ARG_START parts. (But we can ignore the MSG_START.)
1627 for (int32_t i = 1; i < limit && U_SUCCESS(status); ++i) {
1628 const MessagePattern::Part* part = &msgPattern.getPart(i);
1629 if (part->getType() != UMSGPAT_PART_TYPE_ARG_START) {
1630 continue;
1631 }
1632 UMessagePatternArgType argType = part->getArgType();
b75a7d8f 1633
4388f060
A
1634 int32_t argNumber = -1;
1635 part = &msgPattern.getPart(i + 1);
1636 if (part->getType() == UMSGPAT_PART_TYPE_ARG_NUMBER) {
1637 argNumber = part->getValue();
1638 }
1639 Formattable::Type formattableType;
b75a7d8f 1640
4388f060
A
1641 switch (argType) {
1642 case UMSGPAT_ARG_TYPE_NONE:
1643 formattableType = Formattable::kString;
1644 break;
1645 case UMSGPAT_ARG_TYPE_SIMPLE: {
1646 int32_t index = i;
1647 i += 2;
1648 UnicodeString explicitType = msgPattern.getSubstring(msgPattern.getPart(i++));
1649 UnicodeString style;
1650 if ((part = &msgPattern.getPart(i))->getType() == UMSGPAT_PART_TYPE_ARG_STYLE) {
1651 style = msgPattern.getSubstring(*part);
1652 ++i;
1653 }
1654 UParseError parseError;
1655 Format* formatter = createAppropriateFormat(explicitType, style, formattableType, parseError, status);
1656 setArgStartFormat(index, formatter, status);
1657 break;
1658 }
1659 case UMSGPAT_ARG_TYPE_CHOICE:
1660 case UMSGPAT_ARG_TYPE_PLURAL:
51004dcb 1661 case UMSGPAT_ARG_TYPE_SELECTORDINAL:
4388f060
A
1662 formattableType = Formattable::kDouble;
1663 break;
1664 case UMSGPAT_ARG_TYPE_SELECT:
1665 formattableType = Formattable::kString;
1666 break;
1667 default:
1668 status = U_INTERNAL_PROGRAM_ERROR; // Should be unreachable.
1669 formattableType = Formattable::kString;
1670 break;
1671 }
1672 if (argNumber != -1) {
1673 if (argTypes[argNumber] != Formattable::kObject && argTypes[argNumber] != formattableType) {
1674 hasArgTypeConflicts = TRUE;
1675 }
1676 argTypes[argNumber] = formattableType;
1677 }
1678 }
1679}
b75a7d8f 1680
4388f060
A
1681Format* MessageFormat::createAppropriateFormat(UnicodeString& type, UnicodeString& style,
1682 Formattable::Type& formattableType, UParseError& parseError,
1683 UErrorCode& ec) {
1684 if (U_FAILURE(ec)) {
1685 return NULL;
1686 }
1687 Format* fmt = NULL;
1688 int32_t typeID, styleID;
1689 DateFormat::EStyle date_style;
3d1f044b 1690 int32_t firstNonSpace;
4388f060
A
1691
1692 switch (typeID = findKeyword(type, TYPE_IDS)) {
1693 case 0: // number
1694 formattableType = Formattable::kDouble;
1695 switch (findKeyword(style, NUMBER_STYLE_IDS)) {
b75a7d8f
A
1696 case 0: // default
1697 fmt = NumberFormat::createInstance(fLocale, ec);
1698 break;
1699 case 1: // currency
1700 fmt = NumberFormat::createCurrencyInstance(fLocale, ec);
1701 break;
1702 case 2: // percent
1703 fmt = NumberFormat::createPercentInstance(fLocale, ec);
1704 break;
1705 case 3: // integer
4388f060 1706 formattableType = Formattable::kLong;
b75a7d8f
A
1707 fmt = createIntegerFormat(fLocale, ec);
1708 break;
0f5d89e8 1709 default: // pattern or skeleton
3d1f044b
A
1710 firstNonSpace = PatternProps::skipWhiteSpace(style, 0);
1711 if (style.compare(firstNonSpace, 2, u"::", 0, 2) == 0) {
0f5d89e8 1712 // Skeleton
3d1f044b 1713 UnicodeString skeleton = style.tempSubString(firstNonSpace + 2);
0f5d89e8
A
1714 fmt = number::NumberFormatter::forSkeleton(skeleton, ec).locale(fLocale).toFormat(ec);
1715 } else {
1716 // Pattern
1717 fmt = NumberFormat::createInstance(fLocale, ec);
1718 if (fmt) {
3d1f044b
A
1719 auto* decfmt = dynamic_cast<DecimalFormat*>(fmt);
1720 if (decfmt != nullptr) {
0f5d89e8
A
1721 decfmt->applyPattern(style, parseError, ec);
1722 }
729e4ab9 1723 }
b75a7d8f
A
1724 }
1725 break;
1726 }
1727 break;
1728
4388f060
A
1729 case 1: // date
1730 case 2: // time
1731 formattableType = Formattable::kDate;
3d1f044b
A
1732 firstNonSpace = PatternProps::skipWhiteSpace(style, 0);
1733 if (style.compare(firstNonSpace, 2, u"::", 0, 2) == 0) {
1734 // Skeleton
1735 UnicodeString skeleton = style.tempSubString(firstNonSpace + 2);
1736 fmt = DateFormat::createInstanceForSkeleton(skeleton, fLocale, ec);
b75a7d8f 1737 } else {
3d1f044b
A
1738 // Pattern
1739 styleID = findKeyword(style, DATE_STYLE_IDS);
1740 date_style = (styleID >= 0) ? DATE_STYLES[styleID] : DateFormat::kDefault;
b75a7d8f 1741
3d1f044b
A
1742 if (typeID == 1) {
1743 fmt = DateFormat::createDateInstance(date_style, fLocale);
1744 } else {
1745 fmt = DateFormat::createTimeInstance(date_style, fLocale);
1746 }
1747
1748 if (styleID < 0 && fmt != NULL) {
1749 SimpleDateFormat* sdtfmt = dynamic_cast<SimpleDateFormat*>(fmt);
1750 if (sdtfmt != NULL) {
1751 sdtfmt->applyPattern(style);
1752 }
729e4ab9 1753 }
b75a7d8f
A
1754 }
1755 break;
1756
4388f060
A
1757 case 3: // spellout
1758 formattableType = Formattable::kDouble;
1759 fmt = makeRBNF(URBNF_SPELLOUT, fLocale, style, ec);
374ca955 1760 break;
4388f060
A
1761 case 4: // ordinal
1762 formattableType = Formattable::kDouble;
1763 fmt = makeRBNF(URBNF_ORDINAL, fLocale, style, ec);
374ca955 1764 break;
4388f060
A
1765 case 5: // duration
1766 formattableType = Formattable::kDouble;
1767 fmt = makeRBNF(URBNF_DURATION, fLocale, style, ec);
46f4442e 1768 break;
b75a7d8f 1769 default:
4388f060 1770 formattableType = Formattable::kString;
b75a7d8f
A
1771 ec = U_ILLEGAL_ARGUMENT_ERROR;
1772 break;
1773 }
1774
4388f060 1775 return fmt;
b75a7d8f 1776}
729e4ab9 1777
4388f060
A
1778
1779//-------------------------------------
729e4ab9
A
1780// Finds the string, s, in the string array, list.
1781int32_t MessageFormat::findKeyword(const UnicodeString& s,
b75a7d8f
A
1782 const UChar * const *list)
1783{
4388f060 1784 if (s.isEmpty()) {
374ca955 1785 return 0; // default
4388f060 1786 }
b75a7d8f 1787
4388f060
A
1788 int32_t length = s.length();
1789 const UChar *ps = PatternProps::trimWhiteSpace(s.getBuffer(), length);
1790 UnicodeString buffer(FALSE, ps, length);
b75a7d8f
A
1791 // Trims the space characters and turns all characters
1792 // in s to lower case.
4388f060 1793 buffer.toLower("");
374ca955
A
1794 for (int32_t i = 0; list[i]; ++i) {
1795 if (!buffer.compare(list[i], u_strlen(list[i]))) {
b75a7d8f 1796 return i;
374ca955 1797 }
b75a7d8f
A
1798 }
1799 return -1;
1800}
729e4ab9 1801
b75a7d8f
A
1802/**
1803 * Convenience method that ought to be in NumberFormat
1804 */
729e4ab9 1805NumberFormat*
b75a7d8f
A
1806MessageFormat::createIntegerFormat(const Locale& locale, UErrorCode& status) const {
1807 NumberFormat *temp = NumberFormat::createInstance(locale, status);
729e4ab9
A
1808 DecimalFormat *temp2;
1809 if (temp != NULL && (temp2 = dynamic_cast<DecimalFormat*>(temp)) != NULL) {
b75a7d8f
A
1810 temp2->setMaximumFractionDigits(0);
1811 temp2->setDecimalSeparatorAlwaysShown(FALSE);
1812 temp2->setParseIntegerOnly(TRUE);
1813 }
1814
1815 return temp;
1816}
1817
1818/**
1819 * Return the default number format. Used to format a numeric
1820 * argument when subformats[i].format is NULL. Returns NULL
1821 * on failure.
1822 *
1823 * Semantically const but may modify *this.
1824 */
1825const NumberFormat* MessageFormat::getDefaultNumberFormat(UErrorCode& ec) const {
1826 if (defaultNumberFormat == NULL) {
1827 MessageFormat* t = (MessageFormat*) this;
1828 t->defaultNumberFormat = NumberFormat::createInstance(fLocale, ec);
729e4ab9 1829 if (U_FAILURE(ec)) {
b75a7d8f
A
1830 delete t->defaultNumberFormat;
1831 t->defaultNumberFormat = NULL;
1832 } else if (t->defaultNumberFormat == NULL) {
1833 ec = U_MEMORY_ALLOCATION_ERROR;
1834 }
1835 }
1836 return defaultNumberFormat;
1837}
1838
1839/**
1840 * Return the default date format. Used to format a date
1841 * argument when subformats[i].format is NULL. Returns NULL
1842 * on failure.
1843 *
1844 * Semantically const but may modify *this.
1845 */
1846const DateFormat* MessageFormat::getDefaultDateFormat(UErrorCode& ec) const {
1847 if (defaultDateFormat == NULL) {
1848 MessageFormat* t = (MessageFormat*) this;
1849 t->defaultDateFormat = DateFormat::createDateTimeInstance(DateFormat::kShort, DateFormat::kShort, fLocale);
1850 if (t->defaultDateFormat == NULL) {
1851 ec = U_MEMORY_ALLOCATION_ERROR;
1852 }
1853 }
1854 return defaultDateFormat;
1855}
1856
46f4442e
A
1857UBool
1858MessageFormat::usesNamedArguments() const {
4388f060 1859 return msgPattern.hasNamedArguments();
46f4442e
A
1860}
1861
4388f060
A
1862int32_t
1863MessageFormat::getArgTypeCount() const {
1864 return argTypeCount;
1865}
1866
1867UBool MessageFormat::equalFormats(const void* left, const void* right) {
1868 return *(const Format*)left==*(const Format*)right;
1869}
1870
1871
1872UBool MessageFormat::DummyFormat::operator==(const Format&) const {
46f4442e
A
1873 return TRUE;
1874}
1875
4388f060
A
1876Format* MessageFormat::DummyFormat::clone() const {
1877 return new DummyFormat();
729e4ab9
A
1878}
1879
51004dcb
A
1880UnicodeString& MessageFormat::DummyFormat::format(const Formattable&,
1881 UnicodeString& appendTo,
1882 UErrorCode& status) const {
1883 if (U_SUCCESS(status)) {
1884 status = U_UNSUPPORTED_ERROR;
1885 }
1886 return appendTo;
1887}
1888
4388f060
A
1889UnicodeString& MessageFormat::DummyFormat::format(const Formattable&,
1890 UnicodeString& appendTo,
1891 FieldPosition&,
1892 UErrorCode& status) const {
1893 if (U_SUCCESS(status)) {
1894 status = U_UNSUPPORTED_ERROR;
1895 }
1896 return appendTo;
1897}
1898
51004dcb
A
1899UnicodeString& MessageFormat::DummyFormat::format(const Formattable&,
1900 UnicodeString& appendTo,
1901 FieldPositionIterator*,
1902 UErrorCode& status) const {
1903 if (U_SUCCESS(status)) {
1904 status = U_UNSUPPORTED_ERROR;
1905 }
1906 return appendTo;
1907}
1908
4388f060
A
1909void MessageFormat::DummyFormat::parseObject(const UnicodeString&,
1910 Formattable&,
1911 ParsePosition& ) const {
1912}
1913
1914
46f4442e
A
1915FormatNameEnumeration::FormatNameEnumeration(UVector *fNameList, UErrorCode& /*status*/) {
1916 pos=0;
1917 fFormatNames = fNameList;
1918}
1919
1920const UnicodeString*
1921FormatNameEnumeration::snext(UErrorCode& status) {
1922 if (U_SUCCESS(status) && pos < fFormatNames->size()) {
1923 return (const UnicodeString*)fFormatNames->elementAt(pos++);
1924 }
1925 return NULL;
1926}
1927
1928void
1929FormatNameEnumeration::reset(UErrorCode& /*status*/) {
1930 pos=0;
1931}
1932
1933int32_t
1934FormatNameEnumeration::count(UErrorCode& /*status*/) const {
4388f060 1935 return (fFormatNames==NULL) ? 0 : fFormatNames->size();
46f4442e
A
1936}
1937
1938FormatNameEnumeration::~FormatNameEnumeration() {
4388f060
A
1939 delete fFormatNames;
1940}
1941
57a6839d
A
1942MessageFormat::PluralSelectorProvider::PluralSelectorProvider(const MessageFormat &mf, UPluralType t)
1943 : msgFormat(mf), rules(NULL), type(t) {
4388f060
A
1944}
1945
1946MessageFormat::PluralSelectorProvider::~PluralSelectorProvider() {
4388f060
A
1947 delete rules;
1948}
1949
57a6839d
A
1950UnicodeString MessageFormat::PluralSelectorProvider::select(void *ctx, double number,
1951 UErrorCode& ec) const {
4388f060
A
1952 if (U_FAILURE(ec)) {
1953 return UnicodeString(FALSE, OTHER_STRING, 5);
1954 }
1955 MessageFormat::PluralSelectorProvider* t = const_cast<MessageFormat::PluralSelectorProvider*>(this);
1956 if(rules == NULL) {
57a6839d 1957 t->rules = PluralRules::forLocale(msgFormat.fLocale, type, ec);
4388f060
A
1958 if (U_FAILURE(ec)) {
1959 return UnicodeString(FALSE, OTHER_STRING, 5);
46f4442e
A
1960 }
1961 }
57a6839d
A
1962 // Select a sub-message according to how the number is formatted,
1963 // which is specified in the selected sub-message.
1964 // We avoid this circle by looking at how
1965 // the number is formatted in the "other" sub-message
1966 // which must always be present and usually contains the number.
1967 // Message authors should be consistent across sub-messages.
1968 PluralSelectorContext &context = *static_cast<PluralSelectorContext *>(ctx);
1969 int32_t otherIndex = msgFormat.findOtherSubMessage(context.startIndex);
1970 context.numberArgIndex = msgFormat.findFirstPluralNumberArg(otherIndex, context.argName);
1971 if(context.numberArgIndex > 0 && msgFormat.cachedFormatters != NULL) {
1972 context.formatter =
1973 (const Format*)uhash_iget(msgFormat.cachedFormatters, context.numberArgIndex);
1974 }
1975 if(context.formatter == NULL) {
1976 context.formatter = msgFormat.getDefaultNumberFormat(ec);
1977 context.forReplaceNumber = TRUE;
1978 }
0f5d89e8
A
1979 if (context.number.getDouble(ec) != number) {
1980 ec = U_INTERNAL_PROGRAM_ERROR;
1981 return UnicodeString(FALSE, OTHER_STRING, 5);
1982 }
57a6839d 1983 context.formatter->format(context.number, context.numberString, ec);
3d1f044b 1984 auto* decFmt = dynamic_cast<const DecimalFormat *>(context.formatter);
57a6839d 1985 if(decFmt != NULL) {
3d1f044b
A
1986 number::impl::DecimalQuantity dq;
1987 decFmt->formatToDecimalQuantity(context.number, dq, ec);
2ca993e8
A
1988 if (U_FAILURE(ec)) {
1989 return UnicodeString(FALSE, OTHER_STRING, 5);
1990 }
3d1f044b 1991 return rules->select(dq);
57a6839d
A
1992 } else {
1993 return rules->select(number);
1994 }
46f4442e 1995}
4388f060 1996
57a6839d 1997void MessageFormat::PluralSelectorProvider::reset() {
4388f060
A
1998 delete rules;
1999 rules = NULL;
2000}
2001
2002
b75a7d8f
A
2003U_NAMESPACE_END
2004
2005#endif /* #if !UCONFIG_NO_FORMATTING */
2006
2007//eof