2 *******************************************************************************
3 * Copyright (C) 2009-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
9 * Modification History:
11 * Date Name Description
12 *******************************************************************************
15 #include "unicode/messagepattern.h"
16 #include "unicode/plurfmt.h"
17 #include "unicode/plurrule.h"
18 #include "unicode/utypes.h"
20 #include "messageimpl.h"
21 #include "plurrule_impl.h"
25 #if !UCONFIG_NO_FORMATTING
29 static const UChar OTHER_STRING[] = {
30 0x6F, 0x74, 0x68, 0x65, 0x72, 0 // "other"
33 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PluralFormat)
35 PluralFormat::PluralFormat(UErrorCode& status)
36 : locale(Locale::getDefault()),
43 PluralFormat::PluralFormat(const Locale& loc, UErrorCode& status)
51 PluralFormat::PluralFormat(const PluralRules& rules, UErrorCode& status)
52 : locale(Locale::getDefault()),
59 PluralFormat::PluralFormat(const Locale& loc,
60 const PluralRules& rules,
69 PluralFormat::PluralFormat(const UnicodeString& pat,
71 : locale(Locale::getDefault()),
76 applyPattern(pat, status);
79 PluralFormat::PluralFormat(const Locale& loc,
80 const UnicodeString& pat,
87 applyPattern(pat, status);
90 PluralFormat::PluralFormat(const PluralRules& rules,
91 const UnicodeString& pat,
93 : locale(Locale::getDefault()),
98 applyPattern(pat, status);
101 PluralFormat::PluralFormat(const Locale& loc,
102 const PluralRules& rules,
103 const UnicodeString& pat,
109 init(&rules, status);
110 applyPattern(pat, status);
113 PluralFormat::PluralFormat(const PluralFormat& other)
115 locale(other.locale),
116 msgPattern(other.msgPattern),
118 offset(other.offset) {
123 PluralFormat::copyObjects(const PluralFormat& other) {
124 UErrorCode status = U_ZERO_ERROR;
125 if (numberFormat != NULL) {
128 if (pluralRulesWrapper.pluralRules != NULL) {
129 delete pluralRulesWrapper.pluralRules;
132 if (other.numberFormat == NULL) {
133 numberFormat = NumberFormat::createInstance(locale, status);
135 numberFormat = (NumberFormat*)other.numberFormat->clone();
137 if (other.pluralRulesWrapper.pluralRules == NULL) {
138 pluralRulesWrapper.pluralRules = PluralRules::forLocale(locale, status);
140 pluralRulesWrapper.pluralRules = other.pluralRulesWrapper.pluralRules->clone();
145 PluralFormat::~PluralFormat() {
150 PluralFormat::init(const PluralRules* rules, UErrorCode& status) {
151 if (U_FAILURE(status)) {
156 pluralRulesWrapper.pluralRules = PluralRules::forLocale(locale, status);
158 pluralRulesWrapper.pluralRules = rules->clone();
159 if (pluralRulesWrapper.pluralRules == NULL) {
160 status = U_MEMORY_ALLOCATION_ERROR;
165 numberFormat= NumberFormat::createInstance(locale, status);
169 PluralFormat::applyPattern(const UnicodeString& newPattern, UErrorCode& status) {
170 msgPattern.parsePluralStyle(newPattern, NULL, status);
171 if (U_FAILURE(status)) {
176 offset = msgPattern.getPluralOffset(0);
180 PluralFormat::format(const Formattable& obj,
181 UnicodeString& appendTo,
183 UErrorCode& status) const
185 if (U_FAILURE(status)) return appendTo;
187 if (obj.isNumeric()) {
188 return format(obj.getDouble(), appendTo, pos, status);
190 status = U_ILLEGAL_ARGUMENT_ERROR;
196 PluralFormat::format(int32_t number, UErrorCode& status) const {
197 FieldPosition fpos(0);
198 UnicodeString result;
199 return format(number, result, fpos, status);
203 PluralFormat::format(double number, UErrorCode& status) const {
204 FieldPosition fpos(0);
205 UnicodeString result;
206 return format(number, result, fpos, status);
211 PluralFormat::format(int32_t number,
212 UnicodeString& appendTo,
214 UErrorCode& status) const {
215 return format((double)number, appendTo, pos, status);
219 PluralFormat::format(double number,
220 UnicodeString& appendTo,
222 UErrorCode& status) const {
223 if (U_FAILURE(status)) {
226 if (msgPattern.countParts() == 0) {
227 return numberFormat->format(number, appendTo, pos);
229 // Get the appropriate sub-message.
230 int32_t partIndex = findSubMessage(msgPattern, 0, pluralRulesWrapper, number, status);
231 // Replace syntactic # signs in the top level of this sub-message
232 // (not in nested arguments) with the formatted number-offset.
233 const UnicodeString& pattern = msgPattern.getPatternString();
235 int32_t prevIndex = msgPattern.getPart(partIndex).getLimit();
237 const MessagePattern::Part& part = msgPattern.getPart(++partIndex);
238 const UMessagePatternPartType type = part.getType();
239 int32_t index = part.getIndex();
240 if (type == UMSGPAT_PART_TYPE_MSG_LIMIT) {
241 return appendTo.append(pattern, prevIndex, index - prevIndex);
242 } else if ((type == UMSGPAT_PART_TYPE_REPLACE_NUMBER) ||
243 (type == UMSGPAT_PART_TYPE_SKIP_SYNTAX && MessageImpl::jdkAposMode(msgPattern))) {
244 appendTo.append(pattern, prevIndex, index - prevIndex);
245 if (type == UMSGPAT_PART_TYPE_REPLACE_NUMBER) {
246 numberFormat->format(number, appendTo);
248 prevIndex = part.getLimit();
249 } else if (type == UMSGPAT_PART_TYPE_ARG_START) {
250 appendTo.append(pattern, prevIndex, index - prevIndex);
252 partIndex = msgPattern.getLimitPartIndex(partIndex);
253 index = msgPattern.getPart(partIndex).getLimit();
254 MessageImpl::appendReducedApostrophes(pattern, prevIndex, index, appendTo);
261 PluralFormat::toPattern(UnicodeString& appendTo) {
262 if (0 == msgPattern.countParts()) {
263 appendTo.setToBogus();
265 appendTo.append(msgPattern.getPatternString());
271 PluralFormat::setLocale(const Locale& loc, UErrorCode& status) {
272 if (U_FAILURE(status)) {
280 pluralRulesWrapper.reset();
285 PluralFormat::setNumberFormat(const NumberFormat* format, UErrorCode& status) {
286 if (U_FAILURE(status)) {
289 NumberFormat* nf = (NumberFormat*)format->clone();
294 status = U_MEMORY_ALLOCATION_ERROR;
299 PluralFormat::clone() const
301 return new PluralFormat(*this);
306 PluralFormat::operator=(const PluralFormat& other) {
307 if (this != &other) {
308 locale = other.locale;
309 msgPattern = other.msgPattern;
310 offset = other.offset;
318 PluralFormat::operator==(const Format& other) const {
319 if (this == &other) {
322 if (!Format::operator==(other)) {
325 const PluralFormat& o = (const PluralFormat&)other;
327 locale == o.locale &&
328 msgPattern == o.msgPattern && // implies same offset
329 (numberFormat == NULL) == (o.numberFormat == NULL) &&
330 (numberFormat == NULL || *numberFormat == *o.numberFormat) &&
331 (pluralRulesWrapper.pluralRules == NULL) == (o.pluralRulesWrapper.pluralRules == NULL) &&
332 (pluralRulesWrapper.pluralRules == NULL ||
333 *pluralRulesWrapper.pluralRules == *o.pluralRulesWrapper.pluralRules);
337 PluralFormat::operator!=(const Format& other) const {
338 return !operator==(other);
342 PluralFormat::parseObject(const UnicodeString& /*source*/,
343 Formattable& /*result*/,
344 ParsePosition& pos) const
346 // Parsing not supported.
347 pos.setErrorIndex(pos.getIndex());
350 int32_t PluralFormat::findSubMessage(const MessagePattern& pattern, int32_t partIndex,
351 const PluralSelector& selector, double number, UErrorCode& ec) {
355 int32_t count=pattern.countParts();
357 const MessagePattern::Part* part=&pattern.getPart(partIndex);
358 if (MessagePattern::Part::hasNumericValue(part->getType())) {
359 offset=pattern.getNumericValue(*part);
364 // The keyword is empty until we need to match against non-explicit, not-"other" value.
365 // Then we get the keyword from the selector.
366 // (In other words, we never call the selector if we match against an explicit value,
367 // or if the only non-explicit keyword is "other".)
368 UnicodeString keyword;
369 UnicodeString other(FALSE, OTHER_STRING, 5);
370 // When we find a match, we set msgStart>0 and also set this boolean to true
371 // to avoid matching the keyword again (duplicates are allowed)
372 // while we continue to look for an explicit-value match.
373 UBool haveKeywordMatch=FALSE;
374 // msgStart is 0 until we find any appropriate sub-message.
375 // We remember the first "other" sub-message if we have not seen any
376 // appropriate sub-message before.
377 // We remember the first matching-keyword sub-message if we have not seen
378 // one of those before.
379 // (The parser allows [does not check for] duplicate keywords.
380 // We just have to make sure to take the first one.)
381 // We avoid matching the keyword twice by also setting haveKeywordMatch=true
382 // at the first keyword match.
383 // We keep going until we find an explicit-value match or reach the end of the plural style.
385 // Iterate over (ARG_SELECTOR [ARG_INT|ARG_DOUBLE] message) tuples
386 // until ARG_LIMIT or end of plural-only pattern.
388 part=&pattern.getPart(partIndex++);
389 const UMessagePatternPartType type = part->getType();
390 if(type==UMSGPAT_PART_TYPE_ARG_LIMIT) {
393 U_ASSERT (type==UMSGPAT_PART_TYPE_ARG_SELECTOR);
394 // part is an ARG_SELECTOR followed by an optional explicit value, and then a message
395 if(MessagePattern::Part::hasNumericValue(pattern.getPartType(partIndex))) {
396 // explicit value like "=2"
397 part=&pattern.getPart(partIndex++);
398 if(number==pattern.getNumericValue(*part)) {
399 // matches explicit value
402 } else if(!haveKeywordMatch) {
403 // plural keyword like "few" or "other"
404 // Compare "other" first and call the selector if this is not "other".
405 if(pattern.partSubstringMatches(*part, other)) {
408 if(0 == keyword.compare(other)) {
409 // This is the first "other" sub-message,
410 // and the selected keyword is also "other".
411 // Do not match "other" again.
412 haveKeywordMatch=TRUE;
416 if(keyword.isEmpty()) {
417 keyword=selector.select(number-offset, ec);
418 if(msgStart!=0 && (0 == keyword.compare(other))) {
419 // We have already seen an "other" sub-message.
420 // Do not match "other" again.
421 haveKeywordMatch=TRUE;
422 // Skip keyword matching but do getLimitPartIndex().
425 if(!haveKeywordMatch && pattern.partSubstringMatches(*part, keyword)) {
428 // Do not match this keyword again.
429 haveKeywordMatch=TRUE;
433 partIndex=pattern.getLimitPartIndex(partIndex);
434 } while(++partIndex<count);
438 PluralFormat::PluralSelectorAdapter::~PluralSelectorAdapter() {
442 UnicodeString PluralFormat::PluralSelectorAdapter::select(double number,
443 UErrorCode& /*ec*/) const {
444 return pluralRules->select(number);
447 void PluralFormat::PluralSelectorAdapter::reset() {
456 #endif /* #if !UCONFIG_NO_FORMATTING */