annotate modules/javafx.web/src/main/native/Source/ThirdParty/icu/source/i18n/number_capi.cpp @ 11038:20a8447c71c6

8207159: Update ICU to version 62.1 Reviewed-by: mbilla, kcr, ghb
author arajkumar
date Fri, 24 Aug 2018 15:06:40 +0530
parents
children
rev   line source
arajkumar@11038 1 // © 2018 and later: Unicode, Inc. and others.
arajkumar@11038 2 // License & terms of use: http://www.unicode.org/copyright.html
arajkumar@11038 3
arajkumar@11038 4 #include "unicode/utypes.h"
arajkumar@11038 5
arajkumar@11038 6 #if !UCONFIG_NO_FORMATTING
arajkumar@11038 7
arajkumar@11038 8 // Allow implicit conversion from char16_t* to UnicodeString for this file:
arajkumar@11038 9 // Helpful in toString methods and elsewhere.
arajkumar@11038 10 #define UNISTR_FROM_STRING_EXPLICIT
arajkumar@11038 11
arajkumar@11038 12 #include "fphdlimp.h"
arajkumar@11038 13 #include "number_utypes.h"
arajkumar@11038 14 #include "numparse_types.h"
arajkumar@11038 15 #include "unicode/numberformatter.h"
arajkumar@11038 16 #include "unicode/unumberformatter.h"
arajkumar@11038 17
arajkumar@11038 18 using namespace icu;
arajkumar@11038 19 using namespace icu::number;
arajkumar@11038 20 using namespace icu::number::impl;
arajkumar@11038 21
arajkumar@11038 22
arajkumar@11038 23 //////////////////////////////////
arajkumar@11038 24 /// C API CONVERSION FUNCTIONS ///
arajkumar@11038 25 //////////////////////////////////
arajkumar@11038 26
arajkumar@11038 27 UNumberFormatterData* UNumberFormatterData::validate(UNumberFormatter* input, UErrorCode& status) {
arajkumar@11038 28 auto* constInput = static_cast<const UNumberFormatter*>(input);
arajkumar@11038 29 auto* validated = validate(constInput, status);
arajkumar@11038 30 return const_cast<UNumberFormatterData*>(validated);
arajkumar@11038 31 }
arajkumar@11038 32
arajkumar@11038 33 const UNumberFormatterData*
arajkumar@11038 34 UNumberFormatterData::validate(const UNumberFormatter* input, UErrorCode& status) {
arajkumar@11038 35 if (U_FAILURE(status)) {
arajkumar@11038 36 return nullptr;
arajkumar@11038 37 }
arajkumar@11038 38 if (input == nullptr) {
arajkumar@11038 39 status = U_ILLEGAL_ARGUMENT_ERROR;
arajkumar@11038 40 return nullptr;
arajkumar@11038 41 }
arajkumar@11038 42 auto* impl = reinterpret_cast<const UNumberFormatterData*>(input);
arajkumar@11038 43 if (impl->fMagic != UNumberFormatterData::kMagic) {
arajkumar@11038 44 status = U_INVALID_FORMAT_ERROR;
arajkumar@11038 45 return nullptr;
arajkumar@11038 46 }
arajkumar@11038 47 return impl;
arajkumar@11038 48 }
arajkumar@11038 49
arajkumar@11038 50 UNumberFormatter* UNumberFormatterData::exportForC() {
arajkumar@11038 51 return reinterpret_cast<UNumberFormatter*>(this);
arajkumar@11038 52 }
arajkumar@11038 53
arajkumar@11038 54 UFormattedNumberData* UFormattedNumberData::validate(UFormattedNumber* input, UErrorCode& status) {
arajkumar@11038 55 auto* constInput = static_cast<const UFormattedNumber*>(input);
arajkumar@11038 56 auto* validated = validate(constInput, status);
arajkumar@11038 57 return const_cast<UFormattedNumberData*>(validated);
arajkumar@11038 58 }
arajkumar@11038 59
arajkumar@11038 60 const UFormattedNumberData*
arajkumar@11038 61 UFormattedNumberData::validate(const UFormattedNumber* input, UErrorCode& status) {
arajkumar@11038 62 if (U_FAILURE(status)) {
arajkumar@11038 63 return nullptr;
arajkumar@11038 64 }
arajkumar@11038 65 if (input == nullptr) {
arajkumar@11038 66 status = U_ILLEGAL_ARGUMENT_ERROR;
arajkumar@11038 67 return nullptr;
arajkumar@11038 68 }
arajkumar@11038 69 auto* impl = reinterpret_cast<const UFormattedNumberData*>(input);
arajkumar@11038 70 if (impl->fMagic != UFormattedNumberData::kMagic) {
arajkumar@11038 71 status = U_INVALID_FORMAT_ERROR;
arajkumar@11038 72 return nullptr;
arajkumar@11038 73 }
arajkumar@11038 74 return impl;
arajkumar@11038 75 }
arajkumar@11038 76
arajkumar@11038 77 UFormattedNumber* UFormattedNumberData::exportForC() {
arajkumar@11038 78 return reinterpret_cast<UFormattedNumber*>(this);
arajkumar@11038 79 }
arajkumar@11038 80
arajkumar@11038 81 /////////////////////////////////////
arajkumar@11038 82 /// END CAPI CONVERSION FUNCTIONS ///
arajkumar@11038 83 /////////////////////////////////////
arajkumar@11038 84
arajkumar@11038 85
arajkumar@11038 86 U_CAPI UNumberFormatter* U_EXPORT2
arajkumar@11038 87 unumf_openForSkeletonAndLocale(const UChar* skeleton, int32_t skeletonLen, const char* locale,
arajkumar@11038 88 UErrorCode* ec) {
arajkumar@11038 89 auto* impl = new UNumberFormatterData();
arajkumar@11038 90 if (impl == nullptr) {
arajkumar@11038 91 *ec = U_MEMORY_ALLOCATION_ERROR;
arajkumar@11038 92 return nullptr;
arajkumar@11038 93 }
arajkumar@11038 94 // Readonly-alias constructor (first argument is whether we are NUL-terminated)
arajkumar@11038 95 UnicodeString skeletonString(skeletonLen == -1, skeleton, skeletonLen);
arajkumar@11038 96 impl->fFormatter = NumberFormatter::forSkeleton(skeletonString, *ec).locale(locale);
arajkumar@11038 97 return impl->exportForC();
arajkumar@11038 98 }
arajkumar@11038 99
arajkumar@11038 100 U_CAPI UFormattedNumber* U_EXPORT2
arajkumar@11038 101 unumf_openResult(UErrorCode* ec) {
arajkumar@11038 102 auto* impl = new UFormattedNumberData();
arajkumar@11038 103 if (impl == nullptr) {
arajkumar@11038 104 *ec = U_MEMORY_ALLOCATION_ERROR;
arajkumar@11038 105 return nullptr;
arajkumar@11038 106 }
arajkumar@11038 107 return impl->exportForC();
arajkumar@11038 108 }
arajkumar@11038 109
arajkumar@11038 110 U_CAPI void U_EXPORT2
arajkumar@11038 111 unumf_formatInt(const UNumberFormatter* uformatter, int64_t value, UFormattedNumber* uresult,
arajkumar@11038 112 UErrorCode* ec) {
arajkumar@11038 113 const UNumberFormatterData* formatter = UNumberFormatterData::validate(uformatter, *ec);
arajkumar@11038 114 UFormattedNumberData* result = UFormattedNumberData::validate(uresult, *ec);
arajkumar@11038 115 if (U_FAILURE(*ec)) { return; }
arajkumar@11038 116
arajkumar@11038 117 result->string.clear();
arajkumar@11038 118 result->quantity.setToLong(value);
arajkumar@11038 119 formatter->fFormatter.formatImpl(result, *ec);
arajkumar@11038 120 }
arajkumar@11038 121
arajkumar@11038 122 U_CAPI void U_EXPORT2
arajkumar@11038 123 unumf_formatDouble(const UNumberFormatter* uformatter, double value, UFormattedNumber* uresult,
arajkumar@11038 124 UErrorCode* ec) {
arajkumar@11038 125 const UNumberFormatterData* formatter = UNumberFormatterData::validate(uformatter, *ec);
arajkumar@11038 126 UFormattedNumberData* result = UFormattedNumberData::validate(uresult, *ec);
arajkumar@11038 127 if (U_FAILURE(*ec)) { return; }
arajkumar@11038 128
arajkumar@11038 129 result->string.clear();
arajkumar@11038 130 result->quantity.setToDouble(value);
arajkumar@11038 131 formatter->fFormatter.formatImpl(result, *ec);
arajkumar@11038 132 }
arajkumar@11038 133
arajkumar@11038 134 U_CAPI void U_EXPORT2
arajkumar@11038 135 unumf_formatDecimal(const UNumberFormatter* uformatter, const char* value, int32_t valueLen,
arajkumar@11038 136 UFormattedNumber* uresult, UErrorCode* ec) {
arajkumar@11038 137 const UNumberFormatterData* formatter = UNumberFormatterData::validate(uformatter, *ec);
arajkumar@11038 138 UFormattedNumberData* result = UFormattedNumberData::validate(uresult, *ec);
arajkumar@11038 139 if (U_FAILURE(*ec)) { return; }
arajkumar@11038 140
arajkumar@11038 141 result->string.clear();
arajkumar@11038 142 result->quantity.setToDecNumber({value, valueLen}, *ec);
arajkumar@11038 143 if (U_FAILURE(*ec)) { return; }
arajkumar@11038 144 formatter->fFormatter.formatImpl(result, *ec);
arajkumar@11038 145 }
arajkumar@11038 146
arajkumar@11038 147 U_CAPI int32_t U_EXPORT2
arajkumar@11038 148 unumf_resultToString(const UFormattedNumber* uresult, UChar* buffer, int32_t bufferCapacity,
arajkumar@11038 149 UErrorCode* ec) {
arajkumar@11038 150 const UFormattedNumberData* result = UFormattedNumberData::validate(uresult, *ec);
arajkumar@11038 151 if (U_FAILURE(*ec)) { return 0; }
arajkumar@11038 152
arajkumar@11038 153 if (buffer == nullptr ? bufferCapacity != 0 : bufferCapacity < 0) {
arajkumar@11038 154 *ec = U_ILLEGAL_ARGUMENT_ERROR;
arajkumar@11038 155 return 0;
arajkumar@11038 156 }
arajkumar@11038 157
arajkumar@11038 158 return result->string.toTempUnicodeString().extract(buffer, bufferCapacity, *ec);
arajkumar@11038 159 }
arajkumar@11038 160
arajkumar@11038 161 U_CAPI UBool U_EXPORT2
arajkumar@11038 162 unumf_resultNextFieldPosition(const UFormattedNumber* uresult, UFieldPosition* ufpos, UErrorCode* ec) {
arajkumar@11038 163 const UFormattedNumberData* result = UFormattedNumberData::validate(uresult, *ec);
arajkumar@11038 164 if (U_FAILURE(*ec)) { return FALSE; }
arajkumar@11038 165
arajkumar@11038 166 if (ufpos == nullptr) {
arajkumar@11038 167 *ec = U_ILLEGAL_ARGUMENT_ERROR;
arajkumar@11038 168 return FALSE;
arajkumar@11038 169 }
arajkumar@11038 170
arajkumar@11038 171 FieldPosition fp;
arajkumar@11038 172 fp.setField(ufpos->field);
arajkumar@11038 173 fp.setBeginIndex(ufpos->beginIndex);
arajkumar@11038 174 fp.setEndIndex(ufpos->endIndex);
arajkumar@11038 175 bool retval = result->string.nextFieldPosition(fp, *ec);
arajkumar@11038 176 ufpos->beginIndex = fp.getBeginIndex();
arajkumar@11038 177 ufpos->endIndex = fp.getEndIndex();
arajkumar@11038 178 // NOTE: MSVC sometimes complains when implicitly converting between bool and UBool
arajkumar@11038 179 return retval ? TRUE : FALSE;
arajkumar@11038 180 }
arajkumar@11038 181
arajkumar@11038 182 U_CAPI void U_EXPORT2
arajkumar@11038 183 unumf_resultGetAllFieldPositions(const UFormattedNumber* uresult, UFieldPositionIterator* ufpositer,
arajkumar@11038 184 UErrorCode* ec) {
arajkumar@11038 185 const UFormattedNumberData* result = UFormattedNumberData::validate(uresult, *ec);
arajkumar@11038 186 if (U_FAILURE(*ec)) { return; }
arajkumar@11038 187
arajkumar@11038 188 if (ufpositer == nullptr) {
arajkumar@11038 189 *ec = U_ILLEGAL_ARGUMENT_ERROR;
arajkumar@11038 190 return;
arajkumar@11038 191 }
arajkumar@11038 192
arajkumar@11038 193 auto* fpi = reinterpret_cast<FieldPositionIterator*>(ufpositer);
arajkumar@11038 194 FieldPositionIteratorHandler fpih(fpi, *ec);
arajkumar@11038 195 result->string.getAllFieldPositions(fpih, *ec);
arajkumar@11038 196 }
arajkumar@11038 197
arajkumar@11038 198 U_CAPI void U_EXPORT2
arajkumar@11038 199 unumf_closeResult(UFormattedNumber* uresult) {
arajkumar@11038 200 UErrorCode localStatus = U_ZERO_ERROR;
arajkumar@11038 201 const UFormattedNumberData* impl = UFormattedNumberData::validate(uresult, localStatus);
arajkumar@11038 202 delete impl;
arajkumar@11038 203 }
arajkumar@11038 204
arajkumar@11038 205 U_CAPI void U_EXPORT2
arajkumar@11038 206 unumf_close(UNumberFormatter* f) {
arajkumar@11038 207 UErrorCode localStatus = U_ZERO_ERROR;
arajkumar@11038 208 const UNumberFormatterData* impl = UNumberFormatterData::validate(f, localStatus);
arajkumar@11038 209 delete impl;
arajkumar@11038 210 }
arajkumar@11038 211
arajkumar@11038 212
arajkumar@11038 213 #endif /* #if !UCONFIG_NO_FORMATTING */
arajkumar@11038 214
arajkumar@11038 215
arajkumar@11038 216
arajkumar@11038 217
arajkumar@11038 218
arajkumar@11038 219
arajkumar@11038 220
arajkumar@11038 221
arajkumar@11038 222
arajkumar@11038 223
arajkumar@11038 224
arajkumar@11038 225
arajkumar@11038 226
arajkumar@11038 227
arajkumar@11038 228
arajkumar@11038 229
arajkumar@11038 230
arajkumar@11038 231
arajkumar@11038 232
arajkumar@11038 233
arajkumar@11038 234
arajkumar@11038 235
arajkumar@11038 236
arajkumar@11038 237
arajkumar@11038 238
arajkumar@11038 239
arajkumar@11038 240