From 75036ffec9473dd1d948c052c041fdedd7784ac9 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 17 Mar 2020 09:18:35 -0700 Subject: [PATCH] Fix Reading the Currency Formats (#33599) * Fix Reading the Currency Formats * Fix the managed format arrays * Support formats missing #'s --- .../src/System/Globalization/FormatProvider.Number.cs | 3 ++- .../System.Globalization.Native/pal_localeNumberData.c | 12 +++++------- .../tests/NumberFormatInfo/NumberFormatInfoTests.cs | 14 ++++++++++++++ .../System.Private.CoreLib/src/System/Number.Formatting.cs | 5 +++-- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs b/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs index 77981ef..9b7120f 100644 --- a/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs +++ b/src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs @@ -607,7 +607,8 @@ namespace System.Globalization "($#)", "-$#", "$-#", "$#-", "(#$)", "-#$", "#-$", "#$-", "-# $", "-$ #", "# $-", "$ #-", - "$ -#", "#- $", "($ #)", "(# $)" + "$ -#", "#- $", "($ #)", "(# $)", + "$- #" }; private static readonly string[] s_posPercentFormats = diff --git a/src/libraries/Native/Unix/System.Globalization.Native/pal_localeNumberData.c b/src/libraries/Native/Unix/System.Globalization.Native/pal_localeNumberData.c index 0beae27..b05ae70 100644 --- a/src/libraries/Native/Unix/System.Globalization.Native/pal_localeNumberData.c +++ b/src/libraries/Native/Unix/System.Globalization.Native/pal_localeNumberData.c @@ -19,6 +19,7 @@ #define UCHAR_PERCENT ((UChar)0x0025) // '%' #define UCHAR_OPENPAREN ((UChar)0x0028) // '(' #define UCHAR_CLOSEPAREN ((UChar)0x0029) // ')' +#define UCHAR_ZERO ((UChar)0x0030) // '0' #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0])) @@ -101,6 +102,7 @@ static char* NormalizeNumericPattern(const UChar* srcPattern, int isNegative) switch (ch) { case UCHAR_DIGIT: + case UCHAR_ZERO: if (!digitAdded) { digitAdded = TRUE; @@ -123,10 +125,6 @@ static char* NormalizeNumericPattern(const UChar* srcPattern, int isNegative) spaceAdded = TRUE; destPattern[index++] = ' '; } - else - { - assert(FALSE); - } break; case UCHAR_MINUS: @@ -200,8 +198,7 @@ static int GetNumericPattern(const UNumberFormat* pNumberFormat, } } - // TODO: https://github.com/dotnet/runtime/issues/946 - // assert(FALSE); // should have found a valid pattern + assert(FALSE); // should have found a valid pattern free(normalizedPattern); return INVALID_FORMAT; @@ -232,7 +229,8 @@ static int GetCurrencyNegativePattern(const char* locale) "C -n", "n- C", "(C n)", - "(n C)"}; + "(n C)", + "C- n" }; UErrorCode status = U_ZERO_ERROR; UNumberFormat* pFormat = unum_open(UNUM_CURRENCY, NULL, 0, locale, NULL, &status); diff --git a/src/libraries/System.Globalization/tests/NumberFormatInfo/NumberFormatInfoTests.cs b/src/libraries/System.Globalization/tests/NumberFormatInfo/NumberFormatInfoTests.cs index 4602ca8..a6cfb1b 100644 --- a/src/libraries/System.Globalization/tests/NumberFormatInfo/NumberFormatInfoTests.cs +++ b/src/libraries/System.Globalization/tests/NumberFormatInfo/NumberFormatInfoTests.cs @@ -87,6 +87,20 @@ namespace System.Globalization.Tests Assert.Equal(newDigits, nfi.NativeDigits); } + [Fact] + public void TestNFIFormatLimits() + { + foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures)) + { + NumberFormatInfo nfi = ci.NumberFormat; + Assert.InRange(nfi.CurrencyNegativePattern, 0, 16); + Assert.InRange(nfi.CurrencyPositivePattern, 0, 3); + Assert.InRange(nfi.PercentNegativePattern, 0, 11); + Assert.InRange(nfi.PercentPositivePattern, 0, 3); + Assert.InRange(nfi.NumberNegativePattern, 0, 4); + } + } + [Theory] [MemberData(nameof(DigitSubstitution_TestData))] public void DigitSubstitutionListTest(string cultureName, DigitShapes shape) diff --git a/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs b/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs index 8b0868d..74fe7c0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs @@ -277,7 +277,8 @@ namespace System "($#)", "-$#", "$-#", "$#-", "(#$)", "-#$", "#-$", "#$-", "-# $", "-$ #", "# $-", "$ #-", - "$ -#", "#- $", "($ #)", "(# $)" + "$ -#", "#- $", "($ #)", "(# $)", + "$- #" }; private static readonly string[] s_posPercentFormats = @@ -2178,7 +2179,7 @@ namespace System { if (groupDigits != null) { - Debug.Assert(sGroup != null, "Must be nulll when groupDigits != null"); + Debug.Assert(sGroup != null, "Must be null when groupDigits != null"); int groupSizeIndex = 0; // Index into the groupDigits array. int bufferSize = digPos; // The length of the result buffer string. int groupSize = 0; // The current group size. -- 2.7.4