w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

upluralrules.cpp
Go to the documentation of this file.
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"
21 #include "number_decimalquantity.h"
22 #include "number_utypes.h"
23 #include "numrange_impl.h"
24 
26 
27 namespace {
28 
29 /**
30  * Given a number and a format, returns the keyword of the first applicable
31  * rule for the PluralRules object.
32  * @param rules The plural rules.
33  * @param obj The numeric object for which the rule should be determined.
34  * @param fmt The NumberFormat specifying how the number will be formatted
35  * (this can affect the plural form, e.g. "1 dollar" vs "1.0 dollars").
36  * @param status Input/output parameter. If at entry this indicates a
37  * failure status, the method returns immediately; otherwise
38  * this is set to indicate the outcome of the call.
39  * @return The keyword of the selected rule. Undefined in the case of an error.
40  */
41 UnicodeString select(const PluralRules &rules, const Formattable& obj, const NumberFormat& fmt, UErrorCode& status) {
42  if (U_SUCCESS(status)) {
43  const DecimalFormat *decFmt = dynamic_cast<const DecimalFormat *>(&fmt);
44  if (decFmt != NULL) {
46  decFmt->formatToDecimalQuantity(obj, dq, status);
47  if (U_SUCCESS(status)) {
48  return rules.select(dq);
49  }
50  } else {
51  double number = obj.getDouble(status);
52  if (U_SUCCESS(status)) {
53  return rules.select(number);
54  }
55  }
56  }
57  return UnicodeString();
58 }
59 
60 } // namespace
61 
64 {
66 }
67 
70 {
71  return (UPluralRules*)PluralRules::forLocale(Locale(locale), type, *status);
72 }
73 
74 U_CAPI void U_EXPORT2
76 {
77  delete (PluralRules*)uplrules;
78 }
79 
81 uplrules_select(const UPluralRules *uplrules,
82  double number,
85 {
86  if (U_FAILURE(*status)) {
87  return 0;
88  }
89  if (keyword == NULL ? capacity != 0 : capacity < 0) {
91  return 0;
92  }
93  UnicodeString result = ((PluralRules*)uplrules)->select(number);
94  return result.extract(keyword, capacity, *status);
95 }
96 
99  const UFormattedNumber* number,
102 {
103  if (U_FAILURE(*status)) {
104  return 0;
105  }
106  if (keyword == NULL ? capacity != 0 : capacity < 0) {
108  return 0;
109  }
112  if (U_FAILURE(*status)) {
113  return 0;
114  }
115  UnicodeString result = ((PluralRules*)uplrules)->select(*dq);
116  return result.extract(keyword, capacity, *status);
117 }
118 
121  const UFormattedNumberRange* urange,
124 {
125  if (U_FAILURE(*status)) {
126  return 0;
127  }
128  if (keyword == NULL ? capacity != 0 : capacity < 0) {
130  return 0;
131  }
134  UnicodeString result = ((PluralRules*)uplrules)->select(impl, *status);
135  return result.extract(keyword, capacity, *status);
136 }
137 
140  double number,
141  const UNumberFormat *fmt,
144 {
145  if (U_FAILURE(*status)) {
146  return 0;
147  }
148  const PluralRules* plrules = reinterpret_cast<const PluralRules*>(uplrules);
149  const NumberFormat* nf = reinterpret_cast<const NumberFormat*>(fmt);
150  if (plrules == NULL || nf == NULL || ((keyword == NULL)? capacity != 0 : capacity < 0)) {
152  return 0;
153  }
154  Formattable obj(number);
155  UnicodeString result = select(*plrules, obj, *nf, *status);
156  return result.extract(keyword, capacity, *status);
157 }
158 
162 {
163  if (U_FAILURE(*status)) {
164  return NULL;
165  }
166  const PluralRules* plrules = reinterpret_cast<const PluralRules*>(uplrules);
167  if (plrules == NULL) {
169  return NULL;
170  }
171  StringEnumeration *senum = plrules->getKeywords(*status);
172  if (U_FAILURE(*status)) {
173  return NULL;
174  }
175  if (senum == NULL) {
177  return NULL;
178  }
180 }
181 
182 #endif /* #if !UCONFIG_NO_FORMATTING */
#define type(a)
Definition: aptex-macros.h:171
unsigned char UChar
Definition: bzip2.c:163
#define NULL
Definition: ftobjs.h:61
signed int int32_t
Definition: stdint.h:77
int capacity
Definition: pdfcolor.c:1335
#define U_EXPORT2
Definition: platform.h:844
UnicodeString select(const PluralRules &rules, const Formattable &obj, const NumberFormat &fmt, UErrorCode &status)
const UFormattedNumberRangeData * validateUFormattedNumberRange(const UFormattedNumberRange *uresult, UErrorCode &status)
const DecimalQuantity * validateUFormattedNumberToDecimalQuantity(const UFormattedNumber *uresult, UErrorCode &status)
#define impl
Definition: pts_fax.c:34
#define status
UChar rules[100]
Definition: strsrch.cpp:66
def_key keyword[6]
Definition: t1part.c:256
char fmt[256]
Definition: tex4ht.c:3925
#define U_CAPI
Definition: umachine.h:110
C++ API: Unicode String.
C API: Compatibility APIs for number formatting.
void * UNumberFormat
Definition: unum.h:144
C-compatible API for localized number formatting; not recommended for C++.
struct UFormattedNumber UFormattedNumber
struct UFormattedNumberRange UFormattedNumberRange
C API: Plural rules, select plural keywords for numeric values.
struct UPluralRules UPluralRules
Definition: upluralrules.h:87
UPluralType
Definition: upluralrules.h:58
@ UPLURAL_TYPE_CARDINAL
Definition: upluralrules.h:63
#define uplrules_selectFormatted
Definition: urename.h:1279
#define uplrules_selectWithFormat
Definition: urename.h:1280
#define uplrules_open
Definition: urename.h:1275
#define uplrules_selectForRange
Definition: urename.h:1278
#define uenum_openFromStringEnumeration
Definition: urename.h:930
#define uplrules_openForType
Definition: urename.h:1276
#define uplrules_select
Definition: urename.h:1277
#define uplrules_getKeywords
Definition: urename.h:1274
#define uplrules_close
Definition: urename.h:1273
static const char * locale
Definition: uresb.c:52
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Definition: utypes.h:431
@ U_MEMORY_ALLOCATION_ERROR
Definition: utypes.h:473
@ U_ILLEGAL_ARGUMENT_ERROR
Definition: utypes.h:467
#define U_FAILURE(x)
Definition: utypes.h:735
#define U_SUCCESS(x)
Definition: utypes.h:730