forked from dotnet/coreclr
/
localeNumberData.cpp
393 lines (339 loc) · 10.1 KB
/
localeNumberData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#include <assert.h>
#include <string.h>
#include "locale.hpp"
#include "unicode/decimfmt.h"
#include "unicode/numfmt.h"
#include "unicode/ulocdata.h"
// invariant character definitions used by ICU
#define chCurrencySign ((UChar)0x00A4) // international currency
#define chSpace ((UChar)0x0020) // space
#define chSpace2 ((UChar)0x00A0) // space
#define chPatternDigit ((UChar)0x0023) // '#'
#define chPatternSeparator ((UChar)0x003B) // ';'
#define chPatternMinus ((UChar)0x002D) // '-'
#define chPercent ((UChar)0x0025) // '%'
#define chOpenParen ((UChar)0x0028) // '('
#define chCloseParen ((UChar)0x0029) // ')'
#define ARRAY_LENGTH(array) (sizeof(array)/sizeof(array[0]))
// Enum that corresponds to managed enum CultureData.LocaleNumberData.
// The numeric values of the enum members match their Win32 counterparts.
enum LocaleNumberData : int32_t
{
LanguageId = 0x00000001,
MeasurementSystem = 0x0000000D,
FractionalDigitsCount = 0x00000011,
NegativeNumberFormat = 0x00001010,
MonetaryFractionalDigitsCount = 0x00000019,
PositiveMonetaryNumberFormat = 0x0000001B,
NegativeMonetaryNumberFormat = 0x0000001C,
CalendarType = 0x00001009,
FirstWeekOfYear = 0x0000100D,
ReadingLayout = 0x00000070,
NegativePercentFormat = 0x00000074,
PositivePercentFormat = 0x00000075,
Digit = 0x00000010,
Monetary = 0x00000018
};
/*
Function:
NormalizePattern
Returns a numeric string pattern in a format that we can match against the appropriate managed pattern.
*/
void NormalizePattern(const UnicodeString *srcPattern, UnicodeString *destPattern, bool isNegative)
{
// A srcPattern example: "#,##0.00 C;(#,##0.00 C)" but where C is the international currency symbol (chCurrencySign)
// The positive pattern comes first, then an optional negative pattern separated by a semicolon
// A destPattern example: "(C n)" where C represents the currency symbol, and n is the number
destPattern->remove();
int iStart = 0;
int iEnd = srcPattern->length() - 1;
int32_t iNegativePatternStart = srcPattern->indexOf(chPatternSeparator);
if (iNegativePatternStart >= 0)
{
if (isNegative)
{
iStart = iNegativePatternStart + 1;
}
else
{
iEnd = iNegativePatternStart - 1;
}
}
bool minusAdded = false;
bool digitAdded = false;
bool currencyAdded = false;
bool spaceAdded = false;
for (int i = iStart; i <= iEnd; i++)
{
UChar ch = srcPattern->char32At(i);
switch (ch)
{
case chPatternDigit:
if (!digitAdded)
{
digitAdded = true;
destPattern->append('n');
}
break;
case chCurrencySign:
if (!currencyAdded)
{
currencyAdded = true;
destPattern->append('C');
}
break;
case chSpace:
case chSpace2:
if (!spaceAdded)
{
spaceAdded = true;
destPattern->append(chSpace);
}
else
{
assert(false);
}
break;
case chPatternMinus:
case chOpenParen:
case chCloseParen:
minusAdded = true;
destPattern->append(ch);
break;
case chPercent:
destPattern->append(ch);
break;
}
}
// if there is no negative subpattern, the convention is to prefix the minus sign
if (isNegative && !minusAdded)
{
destPattern->insert(0, chPatternMinus);
}
}
/*
Function:
GetPattern
Determines the pattern from the decimalFormat and returns the matching pattern's index from patterns[].
Returns index -1 if no pattern is found.
*/
int GetPattern(DecimalFormat *decimalFormat, const char* patterns[], int patternsCount, bool isNegative)
{
const int INVALID_FORMAT = -1;
const int MAX_DOTNET_NUMERIC_PATTERN_LENGTH = 6; // example: "(C n)" plus terminator
char charPattern[MAX_DOTNET_NUMERIC_PATTERN_LENGTH] = { 0 };
UnicodeString icuPattern;
decimalFormat->toPattern(icuPattern);
UnicodeString normalizedPattern;
NormalizePattern(&icuPattern, &normalizedPattern, isNegative);
assert(normalizedPattern.length() > 0);
assert(normalizedPattern.length() < MAX_DOTNET_NUMERIC_PATTERN_LENGTH);
if (normalizedPattern.length() == 0 || normalizedPattern.length() >= MAX_DOTNET_NUMERIC_PATTERN_LENGTH)
{
return INVALID_FORMAT;
}
u_UCharsToChars(normalizedPattern.getTerminatedBuffer(), charPattern, normalizedPattern.length() + 1);
for (int i = 0; i < patternsCount; i++)
{
if (strcmp(charPattern, patterns[i]) == 0)
{
return i;
}
};
assert(false); // should have found a valid pattern
return INVALID_FORMAT;
}
/*
Function:
GetCurrencyNegativePattern
Implementation of NumberFormatInfo.CurrencyNegativePattern.
Returns the pattern index.
*/
int GetCurrencyNegativePattern(const Locale &locale)
{
const int DEFAULT_VALUE = 0;
static const char* Patterns[] = { "(Cn)", "-Cn", "C-n", "Cn-", "(nC)", "-nC", "n-C", "nC-", "-n C", "-C n", "n C-", "C n-", "C -n", "n- C", "(C n)", "(n C)" };
UErrorCode status = U_ZERO_ERROR;
LocalPointer<NumberFormat> format(NumberFormat::createInstance(locale, UNUM_CURRENCY, status));
if (U_FAILURE(status))
{
assert(false);
return DEFAULT_VALUE;
}
int value = GetPattern(static_cast<DecimalFormat*>(format.getAlias()), Patterns, ARRAY_LENGTH(Patterns), true);
return (value >= 0) ? value : DEFAULT_VALUE;
}
/*
Function:
GetCurrencyPositivePattern
Implementation of NumberFormatInfo.CurrencyPositivePattern.
Returns the pattern index.
*/
int GetCurrencyPositivePattern(const Locale &locale)
{
const int DEFAULT_VALUE = 0;
static const char* Patterns[] = { "Cn", "nC", "C n", "n C" };
UErrorCode status = U_ZERO_ERROR;
LocalPointer<NumberFormat> format(NumberFormat::createInstance(locale, UNUM_CURRENCY, status));
if (U_FAILURE(status))
{
assert(false);
return DEFAULT_VALUE;
}
int value = GetPattern(static_cast<DecimalFormat*>(format.getAlias()), Patterns, ARRAY_LENGTH(Patterns), false);
return (value >= 0) ? value : DEFAULT_VALUE;
}
/*
Function:
GetNumberNegativePattern
Implementation of NumberFormatInfo.NumberNegativePattern.
Returns the pattern index.
*/
int GetNumberNegativePattern(const Locale &locale)
{
const int DEFAULT_VALUE = 1;
static const char* Patterns[] = { "(n)", "-n", "- n", "n-", "n -" };
UErrorCode status = U_ZERO_ERROR;
LocalPointer<NumberFormat> format(NumberFormat::createInstance(locale, UNUM_DECIMAL, status));
if (U_FAILURE(status))
{
assert(false);
return DEFAULT_VALUE;
}
int value = GetPattern(static_cast<DecimalFormat*>(format.getAlias()), Patterns, ARRAY_LENGTH(Patterns), true);
return (value >= 0) ? value : DEFAULT_VALUE;
}
/*
Function:
GetPercentNegativePattern
Implementation of NumberFormatInfo.PercentNegativePattern.
Returns the pattern index.
*/
int GetPercentNegativePattern(const Locale &locale)
{
const int DEFAULT_VALUE = 0;
static const char* Patterns[] = { "-n %", "-n%", "-%n", "%-n", "%n-", "n-%", "n%-", "-% n", "n %-", "% n-", "% -n", "n- %" };
UErrorCode status = U_ZERO_ERROR;
LocalPointer<NumberFormat> format(NumberFormat::createInstance(locale, UNUM_PERCENT, status));
if (U_FAILURE(status))
{
assert(false);
return DEFAULT_VALUE;
}
int value = GetPattern(static_cast<DecimalFormat*>(format.getAlias()), Patterns, ARRAY_LENGTH(Patterns), true);
return (value >= 0) ? value : DEFAULT_VALUE;
}
/*
Function:
GetPercentPositivePattern
Implementation of NumberFormatInfo.PercentPositivePattern.
Returns the pattern index.
*/
int GetPercentPositivePattern(const Locale &locale)
{
const int DEFAULT_VALUE = 0;
static const char* Patterns[] = { "n %", "n%", "%n", "% n" };
UErrorCode status = U_ZERO_ERROR;
LocalPointer<NumberFormat> format(NumberFormat::createInstance(locale, UNUM_PERCENT, status));
if (U_FAILURE(status))
{
assert(false);
return DEFAULT_VALUE;
}
int value = GetPattern(static_cast<DecimalFormat*>(format.getAlias()), Patterns, ARRAY_LENGTH(Patterns), false);
return (value >= 0) ? value : DEFAULT_VALUE;
}
/*
Function:
GetMeasurementSystem
Obtains the measurement system for the local, determining if US or metric.
Returns 1 for US, 0 otherwise.
*/
UErrorCode GetMeasurementSystem(const char *localeId, int32_t *value)
{
UErrorCode status = U_ZERO_ERROR;
UMeasurementSystem measurementSystem = ulocdata_getMeasurementSystem(localeId, &status);
if (U_SUCCESS(status))
{
*value = (measurementSystem == UMeasurementSystem::UMS_US) ? 1 : 0;
}
return status;
}
/*
PAL Function:
GetLocaleInfoInt
Obtains integer locale information
Returns 1 for success, 0 otherwise
*/
extern "C" int32_t GetLocaleInfoInt(const UChar* localeName, LocaleNumberData localeNumberData, int32_t* value)
{
Locale locale = GetLocale(localeName);
if (locale.isBogus())
{
return UErrorCodeToBool(U_ILLEGAL_ARGUMENT_ERROR);
}
UErrorCode status = U_ZERO_ERROR;
switch (localeNumberData)
{
case LanguageId:
*value = locale.getLCID();
break;
case MeasurementSystem:
status = GetMeasurementSystem(locale.getName(), value);
break;
case FractionalDigitsCount:
{
UNumberFormat* numformat = unum_open(UNUM_DECIMAL, NULL, 0, locale.getName(), NULL, &status);
if (U_SUCCESS(status))
{
*value = unum_getAttribute(numformat, UNUM_MAX_FRACTION_DIGITS);
unum_close(numformat);
}
break;
}
case NegativeNumberFormat:
*value = GetNumberNegativePattern(locale);
break;
case MonetaryFractionalDigitsCount:
{
UNumberFormat* numformat = unum_open(UNUM_CURRENCY, NULL, 0, locale.getName(), NULL, &status);
if (U_SUCCESS(status))
{
*value = unum_getAttribute(numformat, UNUM_MAX_FRACTION_DIGITS);
unum_close(numformat);
}
break;
}
case PositiveMonetaryNumberFormat:
*value = GetCurrencyPositivePattern(locale);
break;
case NegativeMonetaryNumberFormat:
*value = GetCurrencyNegativePattern(locale);
break;
case CalendarType:
*value = 1; //TODO: implement
break;
case FirstWeekOfYear:
*value = 0; //TODO: implement
break;
case ReadingLayout:
*value = 0; //todo: implement
break;
case NegativePercentFormat:
*value = GetPercentNegativePattern(locale);
break;
case PositivePercentFormat:
*value = GetPercentPositivePattern(locale);
break;
default:
status = U_UNSUPPORTED_ERROR;
assert(false);
break;
}
assert(status != U_BUFFER_OVERFLOW_ERROR);
return UErrorCodeToBool(status);
}