X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/2ca993e82fb37b597a3c73ecd1586a139a6579c5..4f1e1a09ce4daed860e35d359ce2fceccb0764e8:/icuSources/i18n/winnmfmt.cpp diff --git a/icuSources/i18n/winnmfmt.cpp b/icuSources/i18n/winnmfmt.cpp index 23610732..5637a0f4 100644 --- a/icuSources/i18n/winnmfmt.cpp +++ b/icuSources/i18n/winnmfmt.cpp @@ -1,3 +1,5 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************** * Copyright (C) 2005-2016, International Business Machines @@ -26,7 +28,9 @@ #include "uassert.h" #include "locmap.h" +#ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN +#endif # define VC_EXTRALEAN # define NOUSER # define NOSERVICE @@ -56,43 +60,43 @@ UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32NumberFormat) * end in ";0" then the return value should be multiplied by 10. * (e.g. "3" => 30, "3;2" => 320) */ -static UINT getGrouping(const char *grouping) +static UINT getGrouping(const wchar_t *grouping) { UINT g = 0; - const char *s; + const wchar_t *s; - for (s = grouping; *s != '\0'; s += 1) { - if (*s > '0' && *s < '9') { - g = g * 10 + (*s - '0'); - } else if (*s != ';') { + for (s = grouping; *s != L'\0'; s += 1) { + if (*s > L'0' && *s < L'9') { + g = g * 10 + (*s - L'0'); + } else if (*s != L';') { break; } } - if (*s != '0') { + if (*s != L'0') { g *= 10; } return g; } -static void getNumberFormat(NUMBERFMTW *fmt, int32_t lcid) +static void getNumberFormat(NUMBERFMTW *fmt, const wchar_t *windowsLocaleName) { - char buf[10]; + wchar_t buf[10]; - GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_IDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT)); - GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT)); + GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_IDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT)); + GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT)); - GetLocaleInfoA(lcid, LOCALE_SGROUPING, buf, 10); + GetLocaleInfoEx(windowsLocaleName, LOCALE_SGROUPING, (LPWSTR)buf, 10); fmt->Grouping = getGrouping(buf); - fmt->lpDecimalSep = NEW_ARRAY(UChar, 6); - GetLocaleInfoW(lcid, LOCALE_SDECIMAL, fmt->lpDecimalSep, 6); + fmt->lpDecimalSep = NEW_ARRAY(wchar_t, 6); + GetLocaleInfoEx(windowsLocaleName, LOCALE_SDECIMAL, fmt->lpDecimalSep, 6); - fmt->lpThousandSep = NEW_ARRAY(UChar, 6); - GetLocaleInfoW(lcid, LOCALE_STHOUSAND, fmt->lpThousandSep, 6); + fmt->lpThousandSep = NEW_ARRAY(wchar_t, 6); + GetLocaleInfoEx(windowsLocaleName, LOCALE_STHOUSAND, fmt->lpThousandSep, 6); - GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_INEGNUMBER, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT)); + GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_INEGNUMBER, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT)); } static void freeNumberFormat(NUMBERFMTW *fmt) @@ -103,27 +107,27 @@ static void freeNumberFormat(NUMBERFMTW *fmt) } } -static void getCurrencyFormat(CURRENCYFMTW *fmt, int32_t lcid) +static void getCurrencyFormat(CURRENCYFMTW *fmt, const wchar_t *windowsLocaleName) { - char buf[10]; + wchar_t buf[10]; - GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ICURRDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT)); - GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT)); + GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_ICURRDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT)); + GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT)); - GetLocaleInfoA(lcid, LOCALE_SMONGROUPING, buf, sizeof(buf)); + GetLocaleInfoEx(windowsLocaleName, LOCALE_SMONGROUPING, (LPWSTR)buf, sizeof(buf)); fmt->Grouping = getGrouping(buf); - fmt->lpDecimalSep = NEW_ARRAY(UChar, 6); - GetLocaleInfoW(lcid, LOCALE_SMONDECIMALSEP, fmt->lpDecimalSep, 6); + fmt->lpDecimalSep = NEW_ARRAY(wchar_t, 6); + GetLocaleInfoEx(windowsLocaleName, LOCALE_SMONDECIMALSEP, fmt->lpDecimalSep, 6); - fmt->lpThousandSep = NEW_ARRAY(UChar, 6); - GetLocaleInfoW(lcid, LOCALE_SMONTHOUSANDSEP, fmt->lpThousandSep, 6); + fmt->lpThousandSep = NEW_ARRAY(wchar_t, 6); + GetLocaleInfoEx(windowsLocaleName, LOCALE_SMONTHOUSANDSEP, fmt->lpThousandSep, 6); - GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_INEGCURR, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT)); - GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ICURRENCY, (LPWSTR) &fmt->PositiveOrder, sizeof(UINT)); + GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_INEGCURR, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT)); + GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_ICURRENCY, (LPWSTR) &fmt->PositiveOrder, sizeof(UINT)); - fmt->lpCurrencySymbol = NEW_ARRAY(UChar, 8); - GetLocaleInfoW(lcid, LOCALE_SCURRENCY, (LPWSTR) fmt->lpCurrencySymbol, 8); + fmt->lpCurrencySymbol = NEW_ARRAY(wchar_t, 8); + GetLocaleInfoEx(windowsLocaleName, LOCALE_SCURRENCY, (LPWSTR) fmt->lpCurrencySymbol, 8); } static void freeCurrencyFormat(CURRENCYFMTW *fmt) @@ -135,12 +139,84 @@ static void freeCurrencyFormat(CURRENCYFMTW *fmt) } } +// TODO: This is copied in both winnmfmt.cpp and windtfmt.cpp, but really should +// be factored out into a common helper for both. +static UErrorCode GetEquivalentWindowsLocaleName(const Locale& locale, UnicodeString** buffer) +{ + UErrorCode status = U_ZERO_ERROR; + char asciiBCP47Tag[LOCALE_NAME_MAX_LENGTH] = {}; + + // Convert from names like "en_CA" and "de_DE@collation=phonebook" to "en-CA" and "de-DE-u-co-phonebk". + (void) uloc_toLanguageTag(locale.getName(), asciiBCP47Tag, UPRV_LENGTHOF(asciiBCP47Tag), FALSE, &status); + + if (U_SUCCESS(status)) + { + // Need it to be UTF-16, not 8-bit + // TODO: This seems like a good thing for a helper + wchar_t bcp47Tag[LOCALE_NAME_MAX_LENGTH] = {}; + int32_t i; + for (i = 0; i < UPRV_LENGTHOF(bcp47Tag); i++) + { + if (asciiBCP47Tag[i] == '\0') + { + break; + } + else + { + // normally just copy the character + bcp47Tag[i] = static_cast(asciiBCP47Tag[i]); + } + } + + // Ensure it's null terminated + if (i < (UPRV_LENGTHOF(bcp47Tag) - 1)) + { + bcp47Tag[i] = L'\0'; + } + else + { + // Ran out of room. + bcp47Tag[UPRV_LENGTHOF(bcp47Tag) - 1] = L'\0'; + } + + + wchar_t windowsLocaleName[LOCALE_NAME_MAX_LENGTH] = {}; + + // Note: On Windows versions below 10, there is no support for locale name aliases. + // This means that it will fail for locales where ICU has a completely different + // name (like ku vs ckb), and it will also not work for alternate sort locale + // names like "de-DE-u-co-phonebk". + + // TODO: We could add some sort of exception table for cases like ku vs ckb. + + int length = ResolveLocaleName(bcp47Tag, windowsLocaleName, UPRV_LENGTHOF(windowsLocaleName)); + + if (length > 0) + { + *buffer = new UnicodeString(windowsLocaleName); + } + else + { + status = U_UNSUPPORTED_ERROR; + } + } + return status; +} + Win32NumberFormat::Win32NumberFormat(const Locale &locale, UBool currency, UErrorCode &status) - : NumberFormat(), fCurrency(currency), fFormatInfo(NULL), fFractionDigitsSet(FALSE) + : NumberFormat(), fCurrency(currency), fFormatInfo(NULL), fFractionDigitsSet(FALSE), fWindowsLocaleName(nullptr) { if (!U_FAILURE(status)) { fLCID = locale.getLCID(); + GetEquivalentWindowsLocaleName(locale, &fWindowsLocaleName); + // Note: In the previous code, it would look up the LCID for the locale, and if + // the locale was not recognized then it would get an LCID of 0, which is a + // synonym for LOCALE_USER_DEFAULT on Windows. + // If the above method fails, then fWindowsLocaleName will remain as nullptr, and + // then we will pass nullptr to API GetLocaleInfoEx, which is the same as passing + // LOCALE_USER_DEFAULT. + // Resolve actual locale to be used later UErrorCode tmpsts = U_ZERO_ERROR; char tmpLocID[ULOC_FULLNAME_CAPACITY]; @@ -150,12 +226,19 @@ Win32NumberFormat::Win32NumberFormat(const Locale &locale, UBool currency, UErro fLocale = Locale((const char*)tmpLocID); } + const wchar_t *localeName = nullptr; + + if (fWindowsLocaleName != nullptr) + { + localeName = reinterpret_cast(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer())); + } + fFormatInfo = (FormatInfo*)uprv_malloc(sizeof(FormatInfo)); if (fCurrency) { - getCurrencyFormat(&fFormatInfo->currency, fLCID); + getCurrencyFormat(&fFormatInfo->currency, localeName); } else { - getNumberFormat(&fFormatInfo->number, fLCID); + getNumberFormat(&fFormatInfo->number, localeName); } } } @@ -180,6 +263,7 @@ Win32NumberFormat::~Win32NumberFormat() uprv_free(fFormatInfo); } + delete fWindowsLocaleName; } Win32NumberFormat &Win32NumberFormat::operator=(const Win32NumberFormat &other) @@ -190,13 +274,21 @@ Win32NumberFormat &Win32NumberFormat::operator=(const Win32NumberFormat &other) this->fLocale = other.fLocale; this->fLCID = other.fLCID; this->fFractionDigitsSet = other.fFractionDigitsSet; + this->fWindowsLocaleName = other.fWindowsLocaleName == NULL ? NULL : new UnicodeString(*other.fWindowsLocaleName); + + const wchar_t *localeName = nullptr; + + if (fWindowsLocaleName != nullptr) + { + localeName = reinterpret_cast(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer())); + } if (fCurrency) { freeCurrencyFormat(&fFormatInfo->currency); - getCurrencyFormat(&fFormatInfo->currency, fLCID); + getCurrencyFormat(&fFormatInfo->currency, localeName); } else { freeNumberFormat(&fFormatInfo->number); - getNumberFormat(&fFormatInfo->number, fLCID); + getNumberFormat(&fFormatInfo->number, localeName); } return *this; @@ -207,17 +299,17 @@ Format *Win32NumberFormat::clone(void) const return new Win32NumberFormat(*this); } -UnicodeString& Win32NumberFormat::format(double number, UnicodeString& appendTo, FieldPosition& pos) const +UnicodeString& Win32NumberFormat::format(double number, UnicodeString& appendTo, FieldPosition& /* pos */) const { return format(getMaximumFractionDigits(), appendTo, L"%.16f", number); } -UnicodeString& Win32NumberFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const +UnicodeString& Win32NumberFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& /* pos */) const { return format(getMinimumFractionDigits(), appendTo, L"%I32d", number); } -UnicodeString& Win32NumberFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const +UnicodeString& Win32NumberFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& /* pos */) const { return format(getMinimumFractionDigits(), appendTo, L"%I64d", number); } @@ -290,13 +382,20 @@ UnicodeString &Win32NumberFormat::format(int32_t numDigits, UnicodeString &appen } } - UChar stackBuffer[STACK_BUFFER_SIZE]; - UChar *buffer = stackBuffer; + wchar_t stackBuffer[STACK_BUFFER_SIZE]; + wchar_t *buffer = stackBuffer; FormatInfo formatInfo; formatInfo = *fFormatInfo; buffer[0] = 0x0000; + const wchar_t *localeName = nullptr; + + if (fWindowsLocaleName != nullptr) + { + localeName = reinterpret_cast(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer())); + } + if (fCurrency) { if (fFractionDigitsSet) { formatInfo.currency.NumDigits = (UINT) numDigits; @@ -306,17 +405,17 @@ UnicodeString &Win32NumberFormat::format(int32_t numDigits, UnicodeString &appen formatInfo.currency.Grouping = 0; } - result = GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, buffer, STACK_BUFFER_SIZE); + result = GetCurrencyFormatEx(localeName, 0, nBuffer, &formatInfo.currency, buffer, STACK_BUFFER_SIZE); if (result == 0) { DWORD lastError = GetLastError(); if (lastError == ERROR_INSUFFICIENT_BUFFER) { - int newLength = GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, NULL, 0); + int newLength = GetCurrencyFormatEx(localeName, 0, nBuffer, &formatInfo.currency, NULL, 0); - buffer = NEW_ARRAY(UChar, newLength); + buffer = NEW_ARRAY(wchar_t, newLength); buffer[0] = 0x0000; - GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, buffer, newLength); + GetCurrencyFormatEx(localeName, 0, nBuffer, &formatInfo.currency, buffer, newLength); } } } else { @@ -328,20 +427,20 @@ UnicodeString &Win32NumberFormat::format(int32_t numDigits, UnicodeString &appen formatInfo.number.Grouping = 0; } - result = GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, buffer, STACK_BUFFER_SIZE); + result = GetNumberFormatEx(localeName, 0, nBuffer, &formatInfo.number, buffer, STACK_BUFFER_SIZE); if (result == 0) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - int newLength = GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, NULL, 0); + int newLength = GetNumberFormatEx(localeName, 0, nBuffer, &formatInfo.number, NULL, 0); - buffer = NEW_ARRAY(UChar, newLength); + buffer = NEW_ARRAY(wchar_t, newLength); buffer[0] = 0x0000; - GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, buffer, newLength); + GetNumberFormatEx(localeName, 0, nBuffer, &formatInfo.number, buffer, newLength); } } } - appendTo.append(buffer, (int32_t) wcslen(buffer)); + appendTo.append((UChar *)buffer, (int32_t) wcslen(buffer)); if (buffer != stackBuffer) { DELETE_ARRAY(buffer);