Fix Reading the Currency Formats (#33599)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Tue, 17 Mar 2020 16:18:35 +0000 (09:18 -0700)
committerGitHub <noreply@github.com>
Tue, 17 Mar 2020 16:18:35 +0000 (09:18 -0700)
* Fix Reading the Currency Formats

* Fix the managed format arrays

* Support formats missing #'s

src/libraries/Common/src/System/Globalization/FormatProvider.Number.cs
src/libraries/Native/Unix/System.Globalization.Native/pal_localeNumberData.c
src/libraries/System.Globalization/tests/NumberFormatInfo/NumberFormatInfoTests.cs
src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs

index 77981ef..9b7120f 100644 (file)
@@ -607,7 +607,8 @@ namespace System.Globalization
                 "($#)", "-$#", "$-#", "$#-",
                 "(#$)", "-#$", "#-$", "#$-",
                 "-# $", "-$ #", "# $-", "$ #-",
-                "$ -#", "#- $", "($ #)", "(# $)"
+                "$ -#", "#- $", "($ #)", "(# $)",
+                "$- #"
             };
 
             private static readonly string[] s_posPercentFormats =
index 0beae27..b05ae70 100644 (file)
@@ -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);
index 4602ca8..a6cfb1b 100644 (file)
@@ -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)
index 8b0868d..74fe7c0 100644 (file)
@@ -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.