Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 787327a

Browse files
committed
Move off Locale instance methods
To prepare for removing icu::Locale in favor of just using the id directly, remove all the uses of Locale methods except for .getName(). We now use GetLocale to create a Locale but then turn it into a char* for all the helper methods. After this change, we can update GetLocale to do locale parsing into a char buffer and remove all the locale.getName() calls with just `locale'.
1 parent 29ecd4c commit 787327a

File tree

3 files changed

+138
-93
lines changed

3 files changed

+138
-93
lines changed

src/corefx/System.Globalization.Native/calendarData.cpp

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ GetMonthDayPattern
249249
250250
Gets the Month-Day DateTime pattern for the specified locale.
251251
*/
252-
CalendarDataResult GetMonthDayPattern(Locale& locale, UChar* sMonthDay, int32_t stringCapacity)
252+
CalendarDataResult GetMonthDayPattern(const char* locale, UChar* sMonthDay, int32_t stringCapacity)
253253
{
254254
UErrorCode err = U_ZERO_ERROR;
255-
UDateTimePatternGenerator* pGenerator = udatpg_open(locale.getName(), &err);
255+
UDateTimePatternGenerator* pGenerator = udatpg_open(locale, &err);
256256
UDateTimePatternGeneratorHolder generatorHolder(pGenerator, err);
257257

258258
if (U_FAILURE(err))
@@ -270,10 +270,10 @@ GetNativeCalendarName
270270
Gets the native calendar name.
271271
*/
272272
CalendarDataResult
273-
GetNativeCalendarName(Locale& locale, CalendarId calendarId, UChar* nativeName, int32_t stringCapacity)
273+
GetNativeCalendarName(const char* locale, CalendarId calendarId, UChar* nativeName, int32_t stringCapacity)
274274
{
275275
UErrorCode err = U_ZERO_ERROR;
276-
ULocaleDisplayNames* pDisplayNames = uldn_open(locale.getName(), ULDN_STANDARD_NAMES, &err);
276+
ULocaleDisplayNames* pDisplayNames = uldn_open(locale, ULDN_STANDARD_NAMES, &err);
277277
ULocaleDisplayNamesHolder displayNamesHolder(pDisplayNames, err);
278278

279279
uldn_keyValueDisplayName(pDisplayNames, "calendar", GetCalendarName(calendarId), nativeName, stringCapacity, &err);
@@ -298,9 +298,9 @@ extern "C" CalendarDataResult GetCalendarInfo(
298298
switch (dataType)
299299
{
300300
case NativeName:
301-
return GetNativeCalendarName(locale, calendarId, result, resultCapacity);
301+
return GetNativeCalendarName(locale.getName(), calendarId, result, resultCapacity);
302302
case MonthDay:
303-
return GetMonthDayPattern(locale, result, resultCapacity);
303+
return GetMonthDayPattern(locale.getName(), result, resultCapacity);
304304
default:
305305
assert(false);
306306
return UnknownError;
@@ -314,13 +314,13 @@ InvokeCallbackForDatePattern
314314
Gets the ICU date pattern for the specified locale and EStyle and invokes the
315315
callback with the result.
316316
*/
317-
bool InvokeCallbackForDatePattern(Locale& locale,
317+
bool InvokeCallbackForDatePattern(const char* locale,
318318
UDateFormatStyle style,
319319
EnumCalendarInfoCallback callback,
320320
const void* context)
321321
{
322322
UErrorCode err = U_ZERO_ERROR;
323-
UDateFormat* pFormat = udat_open(UDAT_NONE, style, locale.getName(), nullptr, 0, nullptr, 0, &err);
323+
UDateFormat* pFormat = udat_open(UDAT_NONE, style, locale, nullptr, 0, nullptr, 0, &err);
324324
UDateFormatHolder formatHolder(pFormat, err);
325325

326326
if (U_FAILURE(err))
@@ -353,13 +353,13 @@ InvokeCallbackForDateTimePattern
353353
Gets the DateTime pattern for the specified skeleton and invokes the callback
354354
with the retrieved value.
355355
*/
356-
bool InvokeCallbackForDateTimePattern(Locale& locale,
356+
bool InvokeCallbackForDateTimePattern(const char* locale,
357357
const UChar* patternSkeleton,
358358
EnumCalendarInfoCallback callback,
359359
const void* context)
360360
{
361361
UErrorCode err = U_ZERO_ERROR;
362-
UDateTimePatternGenerator* pGenerator = udatpg_open(locale.getName(), &err);
362+
UDateTimePatternGenerator* pGenerator = udatpg_open(locale, &err);
363363
UDateTimePatternGeneratorHolder generatorHolder(pGenerator, err);
364364

365365
if (U_FAILURE(err))
@@ -394,22 +394,22 @@ EnumSymbols
394394
Enumerates of of the symbols of a type for a locale and calendar and invokes a callback
395395
for each value.
396396
*/
397-
bool EnumSymbols(Locale& locale,
397+
bool EnumSymbols(const char* locale,
398398
CalendarId calendarId,
399399
UDateFormatSymbolType type,
400400
int32_t startIndex,
401401
EnumCalendarInfoCallback callback,
402402
const void* context)
403403
{
404404
UErrorCode err = U_ZERO_ERROR;
405-
UDateFormat* pFormat = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, locale.getName(), nullptr, 0, nullptr, 0, &err);
405+
UDateFormat* pFormat = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, locale, nullptr, 0, nullptr, 0, &err);
406406
UDateFormatHolder formatHolder(pFormat, err);
407407

408408
if (U_FAILURE(err))
409409
return false;
410410

411411
char localeWithCalendarName[ULOC_FULLNAME_CAPACITY];
412-
strncpy(localeWithCalendarName, locale.getName(), ULOC_FULLNAME_CAPACITY);
412+
strncpy(localeWithCalendarName, locale, ULOC_FULLNAME_CAPACITY);
413413
uloc_setKeywordValue("calendar", GetCalendarName(calendarId), localeWithCalendarName, ULOC_FULLNAME_CAPACITY, &err);
414414

415415
if (U_FAILURE(err))
@@ -471,7 +471,10 @@ EnumAbbrevEraNames
471471
Enumerates all the abbreviated era names of the specified locale and calendar, invoking the
472472
callback function for each era name.
473473
*/
474-
bool EnumAbbrevEraNames(Locale& locale, CalendarId calendarId, EnumCalendarInfoCallback callback, const void* context)
474+
bool EnumAbbrevEraNames(const char* locale,
475+
CalendarId calendarId,
476+
EnumCalendarInfoCallback callback,
477+
const void* context)
475478
{
476479
// The C-API for ICU provides no way to get at the abbreviated era names for a calendar (so we can't use EnumSymbols
477480
// here). Instead we will try to walk the ICU resource tables directly and fall back to regular era names if can't
@@ -482,7 +485,7 @@ bool EnumAbbrevEraNames(Locale& locale, CalendarId calendarId, EnumCalendarInfoC
482485
char* localeNamePtr = localeNameBuf;
483486
char* parentNamePtr = parentNameBuf;
484487

485-
strncpy(localeNamePtr, locale.getName(), ULOC_FULLNAME_CAPACITY);
488+
strncpy(localeNamePtr, locale, ULOC_FULLNAME_CAPACITY);
486489

487490
while (true)
488491
{
@@ -564,37 +567,38 @@ extern "C" int32_t EnumCalendarInfo(EnumCalendarInfoCallback callback,
564567
// ShortDates to map kShort and kMedium in ICU, but also adding the "yMd"
565568
// skeleton as well, as this
566569
// closely matches what is used on Windows
567-
return InvokeCallbackForDateTimePattern(locale, UDAT_YEAR_NUM_MONTH_DAY_UCHAR, callback, context) &&
568-
InvokeCallbackForDatePattern(locale, UDAT_SHORT, callback, context) &&
569-
InvokeCallbackForDatePattern(locale, UDAT_MEDIUM, callback, context);
570+
return InvokeCallbackForDateTimePattern(
571+
locale.getName(), UDAT_YEAR_NUM_MONTH_DAY_UCHAR, callback, context) &&
572+
InvokeCallbackForDatePattern(locale.getName(), UDAT_SHORT, callback, context) &&
573+
InvokeCallbackForDatePattern(locale.getName(), UDAT_MEDIUM, callback, context);
570574
case LongDates:
571575
// LongDates map to kFull and kLong in ICU.
572-
return InvokeCallbackForDatePattern(locale, UDAT_FULL, callback, context) &&
573-
InvokeCallbackForDatePattern(locale, UDAT_LONG, callback, context);
576+
return InvokeCallbackForDatePattern(locale.getName(), UDAT_FULL, callback, context) &&
577+
InvokeCallbackForDatePattern(locale.getName(), UDAT_LONG, callback, context);
574578
case YearMonths:
575-
return InvokeCallbackForDateTimePattern(locale, UDAT_YEAR_MONTH_UCHAR, callback, context);
579+
return InvokeCallbackForDateTimePattern(locale.getName(), UDAT_YEAR_MONTH_UCHAR, callback, context);
576580
case DayNames:
577-
return EnumSymbols(locale, calendarId, UDAT_STANDALONE_WEEKDAYS, 1, callback, context);
581+
return EnumSymbols(locale.getName(), calendarId, UDAT_STANDALONE_WEEKDAYS, 1, callback, context);
578582
case AbbrevDayNames:
579-
return EnumSymbols(locale, calendarId, UDAT_STANDALONE_SHORT_WEEKDAYS, 1, callback, context);
583+
return EnumSymbols(locale.getName(), calendarId, UDAT_STANDALONE_SHORT_WEEKDAYS, 1, callback, context);
580584
case MonthNames:
581-
return EnumSymbols(locale, calendarId, UDAT_STANDALONE_MONTHS, 0, callback, context);
585+
return EnumSymbols(locale.getName(), calendarId, UDAT_STANDALONE_MONTHS, 0, callback, context);
582586
case AbbrevMonthNames:
583-
return EnumSymbols(locale, calendarId, UDAT_STANDALONE_SHORT_MONTHS, 0, callback, context);
587+
return EnumSymbols(locale.getName(), calendarId, UDAT_STANDALONE_SHORT_MONTHS, 0, callback, context);
584588
case SuperShortDayNames:
585589
#ifdef HAVE_DTWIDTHTYPE_SHORT
586-
return EnumSymbols(locale, calendarId, UDAT_STANDALONE_SHORTER_WEEKDAYS, 1, callback, context);
590+
return EnumSymbols(locale.getName(), calendarId, UDAT_STANDALONE_SHORTER_WEEKDAYS, 1, callback, context);
587591
#else
588-
return EnumSymbols(locale, calendarId, UDAT_STANDALONE_NARROW_WEEKDAYS, 1, callback, context);
592+
return EnumSymbols(locale.getName(), calendarId, UDAT_STANDALONE_NARROW_WEEKDAYS, 1, callback, context);
589593
#endif
590594
case MonthGenitiveNames:
591-
return EnumSymbols(locale, calendarId, UDAT_MONTHS, 0, callback, context);
595+
return EnumSymbols(locale.getName(), calendarId, UDAT_MONTHS, 0, callback, context);
592596
case AbbrevMonthGenitiveNames:
593-
return EnumSymbols(locale, calendarId, UDAT_SHORT_MONTHS, 0, callback, context);
597+
return EnumSymbols(locale.getName(), calendarId, UDAT_SHORT_MONTHS, 0, callback, context);
594598
case EraNames:
595-
return EnumSymbols(locale, calendarId, UDAT_ERAS, 0, callback, context);
599+
return EnumSymbols(locale.getName(), calendarId, UDAT_ERAS, 0, callback, context);
596600
case AbbrevEraNames:
597-
return EnumAbbrevEraNames(locale, calendarId, callback, context);
601+
return EnumAbbrevEraNames(locale.getName(), calendarId, callback, context);
598602
default:
599603
assert(false);
600604
return false;

src/corefx/System.Globalization.Native/localeNumberData.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ GetCurrencyNegativePattern
214214
Implementation of NumberFormatInfo.CurrencyNegativePattern.
215215
Returns the pattern index.
216216
*/
217-
int GetCurrencyNegativePattern(const Locale& locale)
217+
int GetCurrencyNegativePattern(const char* locale)
218218
{
219219
const int DEFAULT_VALUE = 0;
220220
static const char* Patterns[] = {"(Cn)",
@@ -235,7 +235,7 @@ int GetCurrencyNegativePattern(const Locale& locale)
235235
"(n C)"};
236236
UErrorCode status = U_ZERO_ERROR;
237237

238-
UNumberFormat* pFormat = unum_open(UNUM_CURRENCY, nullptr, 0, locale.getName(), nullptr, &status);
238+
UNumberFormat* pFormat = unum_open(UNUM_CURRENCY, nullptr, 0, locale, nullptr, &status);
239239
UNumberFormatHolder formatHolder(pFormat, status);
240240

241241
assert(U_SUCCESS(status));
@@ -259,13 +259,13 @@ GetCurrencyPositivePattern
259259
Implementation of NumberFormatInfo.CurrencyPositivePattern.
260260
Returns the pattern index.
261261
*/
262-
int GetCurrencyPositivePattern(const Locale& locale)
262+
int GetCurrencyPositivePattern(const char* locale)
263263
{
264264
const int DEFAULT_VALUE = 0;
265265
static const char* Patterns[] = {"Cn", "nC", "C n", "n C"};
266266
UErrorCode status = U_ZERO_ERROR;
267267

268-
UNumberFormat* pFormat = unum_open(UNUM_CURRENCY, nullptr, 0, locale.getName(), nullptr, &status);
268+
UNumberFormat* pFormat = unum_open(UNUM_CURRENCY, nullptr, 0, locale, nullptr, &status);
269269
UNumberFormatHolder formatHolder(pFormat, status);
270270

271271
assert(U_SUCCESS(status));
@@ -289,13 +289,13 @@ GetNumberNegativePattern
289289
Implementation of NumberFormatInfo.NumberNegativePattern.
290290
Returns the pattern index.
291291
*/
292-
int GetNumberNegativePattern(const Locale& locale)
292+
int GetNumberNegativePattern(const char* locale)
293293
{
294294
const int DEFAULT_VALUE = 1;
295295
static const char* Patterns[] = {"(n)", "-n", "- n", "n-", "n -"};
296296
UErrorCode status = U_ZERO_ERROR;
297297

298-
UNumberFormat* pFormat = unum_open(UNUM_DECIMAL, nullptr, 0, locale.getName(), nullptr, &status);
298+
UNumberFormat* pFormat = unum_open(UNUM_DECIMAL, nullptr, 0, locale, nullptr, &status);
299299
UNumberFormatHolder formatHolder(pFormat, status);
300300

301301
assert(U_SUCCESS(status));
@@ -319,14 +319,14 @@ GetPercentNegativePattern
319319
Implementation of NumberFormatInfo.PercentNegativePattern.
320320
Returns the pattern index.
321321
*/
322-
int GetPercentNegativePattern(const Locale& locale)
322+
int GetPercentNegativePattern(const char* locale)
323323
{
324324
const int DEFAULT_VALUE = 0;
325325
static const char* Patterns[] = {
326326
"-n %", "-n%", "-%n", "%-n", "%n-", "n-%", "n%-", "-% n", "n %-", "% n-", "% -n", "n- %"};
327327
UErrorCode status = U_ZERO_ERROR;
328328

329-
UNumberFormat* pFormat = unum_open(UNUM_PERCENT, nullptr, 0, locale.getName(), nullptr, &status);
329+
UNumberFormat* pFormat = unum_open(UNUM_PERCENT, nullptr, 0, locale, nullptr, &status);
330330
UNumberFormatHolder formatHolder(pFormat, status);
331331

332332
assert(U_SUCCESS(status));
@@ -350,13 +350,13 @@ GetPercentPositivePattern
350350
Implementation of NumberFormatInfo.PercentPositivePattern.
351351
Returns the pattern index.
352352
*/
353-
int GetPercentPositivePattern(const Locale& locale)
353+
int GetPercentPositivePattern(const char* locale)
354354
{
355355
const int DEFAULT_VALUE = 0;
356356
static const char* Patterns[] = {"n %", "n%", "%n", "% n"};
357357
UErrorCode status = U_ZERO_ERROR;
358358

359-
UNumberFormat* pFormat = unum_open(UNUM_PERCENT, nullptr, 0, locale.getName(), nullptr, &status);
359+
UNumberFormat* pFormat = unum_open(UNUM_PERCENT, nullptr, 0, locale, nullptr, &status);
360360
UNumberFormatHolder formatHolder(pFormat, status);
361361

362362
assert(U_SUCCESS(status));
@@ -380,11 +380,11 @@ GetMeasurementSystem
380380
Obtains the measurement system for the local, determining if US or metric.
381381
Returns 1 for US, 0 otherwise.
382382
*/
383-
UErrorCode GetMeasurementSystem(const char* localeId, int32_t* value)
383+
UErrorCode GetMeasurementSystem(const char* locale, int32_t* value)
384384
{
385385
UErrorCode status = U_ZERO_ERROR;
386386

387-
UMeasurementSystem measurementSystem = ulocdata_getMeasurementSystem(localeId, &status);
387+
UMeasurementSystem measurementSystem = ulocdata_getMeasurementSystem(locale, &status);
388388
if (U_SUCCESS(status))
389389
{
390390
*value = (measurementSystem == UMeasurementSystem::UMS_US) ? 1 : 0;
@@ -413,7 +413,7 @@ extern "C" int32_t GetLocaleInfoInt(const UChar* localeName, LocaleNumberData lo
413413
switch (localeNumberData)
414414
{
415415
case LanguageId:
416-
*value = locale.getLCID();
416+
*value = uloc_getLCID(locale.getName());
417417
break;
418418
case MeasurementSystem:
419419
status = GetMeasurementSystem(locale.getName(), value);
@@ -429,7 +429,7 @@ extern "C" int32_t GetLocaleInfoInt(const UChar* localeName, LocaleNumberData lo
429429
break;
430430
}
431431
case NegativeNumberFormat:
432-
*value = GetNumberNegativePattern(locale);
432+
*value = GetNumberNegativePattern(locale.getName());
433433
break;
434434
case MonetaryFractionalDigitsCount:
435435
{
@@ -442,10 +442,10 @@ extern "C" int32_t GetLocaleInfoInt(const UChar* localeName, LocaleNumberData lo
442442
break;
443443
}
444444
case PositiveMonetaryNumberFormat:
445-
*value = GetCurrencyPositivePattern(locale);
445+
*value = GetCurrencyPositivePattern(locale.getName());
446446
break;
447447
case NegativeMonetaryNumberFormat:
448-
*value = GetCurrencyNegativePattern(locale);
448+
*value = GetCurrencyNegativePattern(locale.getName());
449449
break;
450450
case FirstWeekOfYear:
451451
{
@@ -483,7 +483,7 @@ extern "C" int32_t GetLocaleInfoInt(const UChar* localeName, LocaleNumberData lo
483483
// 0 - Left to right (such as en-US)
484484
// 1 - Right to left (such as arabic locales)
485485
ULayoutType orientation = uloc_getCharacterOrientation(locale.getName(), &status);
486-
// alternative implementation in ICU 54+ is Locale.isRightToLeft() which
486+
// alternative implementation in ICU 54+ is uloc_isRightToLeft() which
487487
// also supports script tags in locale
488488
if (U_SUCCESS(status))
489489
{
@@ -503,10 +503,10 @@ extern "C" int32_t GetLocaleInfoInt(const UChar* localeName, LocaleNumberData lo
503503
break;
504504
}
505505
case NegativePercentFormat:
506-
*value = GetPercentNegativePattern(locale);
506+
*value = GetPercentNegativePattern(locale.getName());
507507
break;
508508
case PositivePercentFormat:
509-
*value = GetPercentPositivePattern(locale);
509+
*value = GetPercentPositivePattern(locale.getName());
510510
break;
511511
default:
512512
status = U_UNSUPPORTED_ERROR;

0 commit comments

Comments
 (0)