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)  

number_capi.cpp
Go to the documentation of this file.
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 
8 // Allow implicit conversion from char16_t* to UnicodeString for this file:
9 // Helpful in toString methods and elsewhere.
10 #define UNISTR_FROM_STRING_EXPLICIT
11 
12 #include "fphdlimp.h"
13 #include "number_utypes.h"
14 #include "numparse_types.h"
15 #include "formattedval_impl.h"
16 #include "number_decnum.h"
19 
20 using namespace icu;
21 using namespace icu::number;
22 using namespace icu::number::impl;
23 
24 
26 namespace number {
27 namespace impl {
28 
29 /**
30  * Implementation class for UNumberFormatter. Wraps a LocalizedNumberFormatter.
31  */
32 struct UNumberFormatterData : public UMemory,
33  // Magic number as ASCII == "NFR" (NumberFormatteR)
34  public IcuCApiHelper<UNumberFormatter, UNumberFormatterData, 0x4E465200> {
35  LocalizedNumberFormatter fFormatter;
36 };
37 
39 
40 // Magic number as ASCII == "FDN" (FormatteDNumber)
42 
46 
47  FormattedNumber fImpl;
49 };
50 
52  : fImpl(&fData) {
54 }
55 
57  // Disown the data from fImpl so it doesn't get deleted twice
58  fImpl.fData = nullptr;
59 }
60 
61 }
62 }
64 
65 
68  UFormattedNumberImpl,
70  unumf)
71 
72 
74  const UFormattedNumber* uresult, UErrorCode& status) {
76  if (U_FAILURE(status)) {
77  return nullptr;
78  }
79  return &result->fData.quantity;
80 }
81 
82 
83 
85 unumf_openForSkeletonAndLocale(const UChar* skeleton, int32_t skeletonLen, const char* locale,
86  UErrorCode* ec) {
87  auto* impl = new UNumberFormatterData();
88  if (impl == nullptr) {
90  return nullptr;
91  }
92  // Readonly-alias constructor (first argument is whether we are NUL-terminated)
93  UnicodeString skeletonString(skeletonLen == -1, skeleton, skeletonLen);
94  impl->fFormatter = NumberFormatter::forSkeleton(skeletonString, *ec).locale(locale);
95  return impl->exportForC();
96 }
97 
99 unumf_openForSkeletonAndLocaleWithError(const UChar* skeleton, int32_t skeletonLen, const char* locale,
101  auto* impl = new UNumberFormatterData();
102  if (impl == nullptr) {
104  return nullptr;
105  }
106  // Readonly-alias constructor (first argument is whether we are NUL-terminated)
107  UnicodeString skeletonString(skeletonLen == -1, skeleton, skeletonLen);
108  impl->fFormatter = NumberFormatter::forSkeleton(skeletonString, *perror, *ec).locale(locale);
109  return impl->exportForC();
110 }
111 
112 U_CAPI void U_EXPORT2
114  UErrorCode* ec) {
115  const UNumberFormatterData* formatter = UNumberFormatterData::validate(uformatter, *ec);
116  auto* result = UFormattedNumberApiHelper::validate(uresult, *ec);
117  if (U_FAILURE(*ec)) { return; }
118 
119  result->fData.getStringRef().clear();
120  result->fData.quantity.setToLong(value);
121  formatter->fFormatter.formatImpl(&result->fData, *ec);
122 }
123 
124 U_CAPI void U_EXPORT2
125 unumf_formatDouble(const UNumberFormatter* uformatter, double value, UFormattedNumber* uresult,
126  UErrorCode* ec) {
127  const UNumberFormatterData* formatter = UNumberFormatterData::validate(uformatter, *ec);
128  auto* result = UFormattedNumberApiHelper::validate(uresult, *ec);
129  if (U_FAILURE(*ec)) { return; }
130 
131  result->fData.getStringRef().clear();
132  result->fData.quantity.setToDouble(value);
133  formatter->fFormatter.formatImpl(&result->fData, *ec);
134 }
135 
136 U_CAPI void U_EXPORT2
137 unumf_formatDecimal(const UNumberFormatter* uformatter, const char* value, int32_t valueLen,
138  UFormattedNumber* uresult, UErrorCode* ec) {
139  const UNumberFormatterData* formatter = UNumberFormatterData::validate(uformatter, *ec);
140  auto* result = UFormattedNumberApiHelper::validate(uresult, *ec);
141  if (U_FAILURE(*ec)) { return; }
142 
143  result->fData.getStringRef().clear();
144  result->fData.quantity.setToDecNumber({value, valueLen}, *ec);
145  if (U_FAILURE(*ec)) { return; }
146  formatter->fFormatter.formatImpl(&result->fData, *ec);
147 }
148 
150 unumf_resultToString(const UFormattedNumber* uresult, UChar* buffer, int32_t bufferCapacity,
151  UErrorCode* ec) {
152  const auto* result = UFormattedNumberApiHelper::validate(uresult, *ec);
153  if (U_FAILURE(*ec)) { return 0; }
154 
155  if (buffer == nullptr ? bufferCapacity != 0 : bufferCapacity < 0) {
157  return 0;
158  }
159 
160  return result->fData.toTempString(*ec).extract(buffer, bufferCapacity, *ec);
161 }
162 
165  const auto* result = UFormattedNumberApiHelper::validate(uresult, *ec);
166  if (U_FAILURE(*ec)) { return FALSE; }
167 
168  if (ufpos == nullptr) {
170  return FALSE;
171  }
172 
173  FieldPosition fp;
174  fp.setField(ufpos->field);
175  fp.setBeginIndex(ufpos->beginIndex);
176  fp.setEndIndex(ufpos->endIndex);
177  bool retval = result->fData.nextFieldPosition(fp, *ec);
178  ufpos->beginIndex = fp.getBeginIndex();
179  ufpos->endIndex = fp.getEndIndex();
180  // NOTE: MSVC sometimes complains when implicitly converting between bool and UBool
181  return retval ? TRUE : FALSE;
182 }
183 
184 U_CAPI void U_EXPORT2
186  UErrorCode* ec) {
187  const auto* result = UFormattedNumberApiHelper::validate(uresult, *ec);
188  if (U_FAILURE(*ec)) { return; }
189 
190  if (ufpositer == nullptr) {
192  return;
193  }
194 
195  auto* fpi = reinterpret_cast<FieldPositionIterator*>(ufpositer);
196  FieldPositionIteratorHandler fpih(fpi, *ec);
197  result->fData.getAllFieldPositions(fpih, *ec);
198 }
199 
202  const UFormattedNumber* uresult,
203  char* dest,
204  int32_t destCapacity,
205  UErrorCode* ec) {
206  const auto* result = UFormattedNumberApiHelper::validate(uresult, *ec);
207  if (U_FAILURE(*ec)) {
208  return 0;
209  }
210  DecNum decnum;
211  return result->fData.quantity
212  .toDecNum(decnum, *ec)
213  .toCharString(*ec)
214  .extract(dest, destCapacity, *ec);
215 }
216 
217 U_CAPI void U_EXPORT2
219  UErrorCode localStatus = U_ZERO_ERROR;
220  const UNumberFormatterData* impl = UNumberFormatterData::validate(f, localStatus);
221  delete impl;
222 }
223 
224 
225 #endif /* #if !UCONFIG_NO_FORMATTING */
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
void __cdecl perror(char const *_ErrMsg)
@ FALSE
Definition: dd.h:101
@ TRUE
Definition: dd.h:102
#define UPRV_FORMATTED_VALUE_CAPI_NO_IMPLTYPE_AUTO_IMPL(CType, ImplType, HelperType, Prefix)
mpz_t * f
Definition: gen-fib.c:34
#define ec
Definition: gsftopk.c:502
unsigned char UChar
Definition: bzip2.c:163
signed __int64 int64_t
Definition: stdint.h:89
signed int int32_t
Definition: stdint.h:77
#define dest
#define U_EXPORT2
Definition: platform.h:844
IcuCApiHelper< UFormattedNumber, UFormattedNumberImpl, 0x46444E00 > UFormattedNumberApiHelper
Definition: number_capi.cpp:38
const DecimalQuantity * validateUFormattedNumberToDecimalQuantity(const UFormattedNumber *uresult, UErrorCode &status)
union value value
Definition: obx.h:44
#define fp
static pixman_bool_t validate(region_type_t *badreg)
#define impl
Definition: pts_fax.c:34
#define status
int32_t field
Definition: umisc.h:39
int32_t beginIndex
Definition: umisc.h:44
int32_t endIndex
Definition: umisc.h:49
FormattedValue * fFormattedValue
Definition: utils.c:300
LocalizedNumberFormatter fFormatter
Definition: number_capi.cpp:35
int number
Definition: t1part.c:207
struct UFieldPositionIterator UFieldPositionIterator
Definition: ufieldpositer.h:46
int8_t UBool
Definition: umachine.h:269
#define U_CAPI
Definition: umachine.h:110
Definition: obx.h:51
C-compatible API for localized number formatting; not recommended for C++.
struct UNumberFormatter UNumberFormatter
struct UFormattedNumber UFormattedNumber
#define unumf_formatInt
Definition: urename.h:1246
#define unumf_formatDouble
Definition: urename.h:1245
#define unumf_openForSkeletonAndLocale
Definition: urename.h:1247
#define unumf_resultNextFieldPosition
Definition: urename.h:1252
#define unumf_formatDecimal
Definition: urename.h:1244
#define unumf_resultToString
Definition: urename.h:1254
#define unumf_openForSkeletonAndLocaleWithError
Definition: urename.h:1248
#define unumf_close
Definition: urename.h:1242
#define unumf_resultToDecimalNumber
Definition: urename.h:1253
#define unumf_resultGetAllFieldPositions
Definition: urename.h:1251
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
@ U_ZERO_ERROR
Definition: utypes.h:465
#define U_FAILURE(x)
Definition: utypes.h:735