Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/upluralrules.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
*****************************************************************************************
5
* Copyright (C) 2010-2012, International Business Machines
6
* Corporation and others. All Rights Reserved.
7
*****************************************************************************************
8
*/
9
10
#include "unicode/utypes.h"
11
12
#if !UCONFIG_NO_FORMATTING
13
14
#include "unicode/upluralrules.h"
15
#include "unicode/plurrule.h"
16
#include "unicode/locid.h"
17
#include "unicode/unistr.h"
18
#include "unicode/unum.h"
19
#include "unicode/numfmt.h"
20
#include "number_decimalquantity.h"
21
22
U_NAMESPACE_USE
23
24
namespace {
25
26
/**
27
 * Given a number and a format, returns the keyword of the first applicable
28
 * rule for the PluralRules object.
29
 * @param rules The plural rules.
30
 * @param obj The numeric object for which the rule should be determined.
31
 * @param fmt The NumberFormat specifying how the number will be formatted
32
 *        (this can affect the plural form, e.g. "1 dollar" vs "1.0 dollars").
33
 * @param status  Input/output parameter. If at entry this indicates a
34
 *                failure status, the method returns immediately; otherwise
35
 *                this is set to indicate the outcome of the call.
36
 * @return The keyword of the selected rule. Undefined in the case of an error.
37
 */
38
0
UnicodeString select(const PluralRules &rules, const Formattable& obj, const NumberFormat& fmt, UErrorCode& status) {
39
0
    if (U_SUCCESS(status)) {
40
0
        const DecimalFormat *decFmt = dynamic_cast<const DecimalFormat *>(&fmt);
41
0
        if (decFmt != NULL) {
42
0
            number::impl::DecimalQuantity dq;
43
0
            decFmt->formatToDecimalQuantity(obj, dq, status);
44
0
            if (U_SUCCESS(status)) {
45
0
                return rules.select(dq);
46
0
            }
47
0
        } else {
48
0
            double number = obj.getDouble(status);
49
0
            if (U_SUCCESS(status)) {
50
0
                return rules.select(number);
51
0
            }
52
0
        }
53
0
    }
54
0
    return UnicodeString();
55
0
}
56
57
}  // namespace
58
59
U_CAPI UPluralRules* U_EXPORT2
60
uplrules_open(const char *locale, UErrorCode *status)
61
0
{
62
0
    return uplrules_openForType(locale, UPLURAL_TYPE_CARDINAL, status);
63
0
}
64
65
U_CAPI UPluralRules* U_EXPORT2
66
uplrules_openForType(const char *locale, UPluralType type, UErrorCode *status)
67
0
{
68
0
    return (UPluralRules*)PluralRules::forLocale(Locale(locale), type, *status);
69
0
}
70
71
U_CAPI void U_EXPORT2
72
uplrules_close(UPluralRules *uplrules)
73
0
{
74
0
    delete (PluralRules*)uplrules;
75
0
}
76
77
U_CAPI int32_t U_EXPORT2
78
uplrules_select(const UPluralRules *uplrules,
79
                double number,
80
                UChar *keyword, int32_t capacity,
81
                UErrorCode *status)
82
0
{
83
0
    if (U_FAILURE(*status)) {
84
0
        return 0;
85
0
    }
86
0
    if (keyword == NULL ? capacity != 0 : capacity < 0) {
87
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
88
0
        return 0;
89
0
    }
90
0
    UnicodeString result = ((PluralRules*)uplrules)->select(number);
91
0
    return result.extract(keyword, capacity, *status);
92
0
}
93
94
U_CAPI int32_t U_EXPORT2
95
uplrules_selectWithFormat(const UPluralRules *uplrules,
96
                          double number,
97
                          const UNumberFormat *fmt,
98
                          UChar *keyword, int32_t capacity,
99
                          UErrorCode *status)
100
0
{
101
0
    if (U_FAILURE(*status)) {
102
0
        return 0;
103
0
    }
104
0
    const PluralRules* plrules = reinterpret_cast<const PluralRules*>(uplrules);
105
0
    const NumberFormat* nf = reinterpret_cast<const NumberFormat*>(fmt);
106
0
    if (plrules == NULL || nf == NULL || ((keyword == NULL)? capacity != 0 : capacity < 0)) {
107
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
108
0
        return 0;
109
0
    }
110
0
    Formattable obj(number);
111
0
    UnicodeString result = select(*plrules, obj, *nf, *status);
112
0
    return result.extract(keyword, capacity, *status);
113
0
}
114
115
U_CAPI UEnumeration* U_EXPORT2
116
uplrules_getKeywords(const UPluralRules *uplrules,
117
                     UErrorCode *status)
118
0
{
119
0
    if (U_FAILURE(*status)) {
120
0
        return NULL;
121
0
    }
122
0
    const PluralRules* plrules = reinterpret_cast<const PluralRules*>(uplrules);
123
0
    if (plrules == NULL) {
124
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
125
0
        return NULL;
126
0
    }
127
0
    StringEnumeration *senum = plrules->getKeywords(*status);
128
0
    if (U_FAILURE(*status)) {
129
0
        return NULL;
130
0
    }
131
0
    if (senum == NULL) {
132
0
        *status = U_MEMORY_ALLOCATION_ERROR;
133
0
        return NULL;
134
0
    }
135
0
    return uenum_openFromStringEnumeration(senum, status);
136
0
}
137
138
#endif /* #if !UCONFIG_NO_FORMATTING */