Home | History | Annotate | Download | only in i18n
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html
      3 /*
      4  * Copyright (C) 2015, International Business Machines
      5  * Corporation and others.  All Rights Reserved.
      6  *
      7  * file name: pluralaffix.cpp
      8  */
      9 
     10 #include "unicode/utypes.h"
     11 
     12 #if !UCONFIG_NO_FORMATTING
     13 
     14 #include "cstring.h"
     15 #include "digitaffix.h"
     16 #include "pluralaffix.h"
     17 
     18 U_NAMESPACE_BEGIN
     19 
     20 UBool
     21 PluralAffix::setVariant(
     22         const char *variant, const UnicodeString &value, UErrorCode &status) {
     23     DigitAffix *current = affixes.getMutable(variant, status);
     24     if (U_FAILURE(status)) {
     25         return FALSE;
     26     }
     27     current->remove();
     28     current->append(value);
     29     return TRUE;
     30 }
     31 
     32 void
     33 PluralAffix::remove() {
     34     affixes.clear();
     35 }
     36 
     37 void
     38 PluralAffix::appendUChar(
     39         const UChar value, int32_t fieldId) {
     40     PluralMapBase::Category index = PluralMapBase::NONE;
     41     for (DigitAffix *current = affixes.nextMutable(index);
     42             current != NULL; current = affixes.nextMutable(index)) {
     43         current->appendUChar(value, fieldId);
     44     }
     45 }
     46 
     47 void
     48 PluralAffix::append(
     49         const UnicodeString &value, int32_t fieldId) {
     50     PluralMapBase::Category index = PluralMapBase::NONE;
     51     for (DigitAffix *current = affixes.nextMutable(index);
     52             current != NULL; current = affixes.nextMutable(index)) {
     53         current->append(value, fieldId);
     54     }
     55 }
     56 
     57 void
     58 PluralAffix::append(
     59         const UChar *value, int32_t charCount, int32_t fieldId) {
     60     PluralMapBase::Category index = PluralMapBase::NONE;
     61     for (DigitAffix *current = affixes.nextMutable(index);
     62             current != NULL; current = affixes.nextMutable(index)) {
     63         current->append(value, charCount, fieldId);
     64     }
     65 }
     66 
     67 UBool
     68 PluralAffix::append(
     69         const PluralAffix &rhs, int32_t fieldId, UErrorCode &status) {
     70     if (U_FAILURE(status)) {
     71         return FALSE;
     72     }
     73     PluralMapBase::Category index = PluralMapBase::NONE;
     74     while(rhs.affixes.next(index) != NULL) {
     75         affixes.getMutableWithDefault(index, affixes.getOther(), status);
     76     }
     77     index = PluralMapBase::NONE;
     78     for (DigitAffix *current = affixes.nextMutable(index);
     79             current != NULL; current = affixes.nextMutable(index)) {
     80         current->append(rhs.affixes.get(index).toString(), fieldId);
     81     }
     82     return TRUE;
     83 }
     84 
     85 const DigitAffix &
     86 PluralAffix::getByCategory(const char *category) const {
     87     return affixes.get(category);
     88 }
     89 
     90 const DigitAffix &
     91 PluralAffix::getByCategory(const UnicodeString &category) const {
     92     return affixes.get(category);
     93 }
     94 
     95 UBool
     96 PluralAffix::hasMultipleVariants() const {
     97     // This works because OTHER is guaranteed to be the first enum value
     98     PluralMapBase::Category index = PluralMapBase::OTHER;
     99     return (affixes.next(index) != NULL);
    100 }
    101 
    102 U_NAMESPACE_END
    103 
    104 #endif /* #if !UCONFIG_NO_FORMATTING */
    105