Enable RegionInfo netstandard 1.7 APIs (#7604)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Fri, 14 Oct 2016 18:26:52 +0000 (11:26 -0700)
committerGitHub <noreply@github.com>
Fri, 14 Oct 2016 18:26:52 +0000 (11:26 -0700)
* Enable RegionInfo netstandard 1.7 APIs

* Fix the typo

* lowercase TRUE and FALSE

src/corefx/System.Globalization.Native/collation.cpp
src/corefx/System.Globalization.Native/locale.hpp
src/corefx/System.Globalization.Native/localeStringData.cpp
src/mscorlib/corefx/SR.cs
src/mscorlib/corefx/System/Globalization/CultureData.Unix.cs
src/mscorlib/corefx/System/Globalization/CultureData.Windows.cs
src/mscorlib/corefx/System/Globalization/CultureData.cs
src/mscorlib/corefx/System/Globalization/CultureInfo.cs
src/mscorlib/corefx/System/Globalization/LocaleData.Unix.cs
src/mscorlib/corefx/System/Globalization/RegionInfo.cs
src/mscorlib/corefx/System/Globalization/STUBS.cs

index 6039a9e..42a9674 100644 (file)
@@ -298,7 +298,7 @@ UCollator* CloneCollatorWithOptions(const UCollator* pCollator, int32_t options,
 // Returns TRUE if all the collation elements in str are completely ignorable
 bool CanIgnoreAllCollationElements(const UCollator* pColl, const UChar* lpStr, int32_t length)
 {
-    bool result = FALSE;
+    bool result = false;
     UErrorCode err = U_ZERO_ERROR;
     UCollationElements* pCollElem = ucol_openElements(pColl, lpStr, length, &err);
 
@@ -306,20 +306,20 @@ bool CanIgnoreAllCollationElements(const UCollator* pColl, const UChar* lpStr, i
     {
         int32_t curCollElem = UCOL_NULLORDER;
 
-        result = TRUE;
+        result = true;
 
         while ((curCollElem = ucol_next(pCollElem, &err)) != UCOL_NULLORDER)
         {
             if (curCollElem != 0)
             {
-                result = FALSE;
+                result = false;
                 break;
             }
         }
 
         if (U_FAILURE(err))
         {
-            result = FALSE;
+            result = false;
         }
 
         ucol_closeElements(pCollElem);
index 4845859..ac28fb1 100644 (file)
@@ -3,6 +3,7 @@
 // See the LICENSE file in the project root for more information.
 
 #include "unicode/locid.h"
+#include "unicode/ucurr.h"
 
 /*
 Function:
index 927da67..a8dcc51 100644 (file)
@@ -27,6 +27,8 @@ enum LocaleStringData : int32_t
     ThousandSeparator = 0x0000000F,
     Digits = 0x00000013,
     MonetarySymbol = 0x00000014,
+    CurrencyEnglishName = 0x00001007,
+    CurrencyNativeName = 0x00001008,
     Iso4217MonetarySymbol = 0x00000015,
     MonetaryDecimalSeparator = 0x00000016,
     MonetaryThousandSeparator = 0x00000017,
@@ -36,6 +38,7 @@ enum LocaleStringData : int32_t
     NegativeSign = 0x00000051,
     Iso639LanguageName = 0x00000059,
     Iso3166CountryName = 0x0000005A,
+    Iso3166CountryName2= 0x00000068,
     NaNSymbol = 0x00000069,
     PositiveInfinitySymbol = 0x0000006a,
     ParentName = 0x0000006d,
@@ -158,6 +161,66 @@ UErrorCode GetLocaleIso3166CountryName(const char* locale, UChar* value, int32_t
 }
 
 /*
+Function:
+GetLocaleIso3166CountryCode
+
+Gets the 3 letter country code for a locale (via uloc_getISO3Country) and converts the result to UChars
+*/
+UErrorCode GetLocaleIso3166CountryCode(const char* locale, UChar* value, int32_t valueLength)
+{
+    const char *pIsoCountryName = uloc_getISO3Country(locale);
+    int len = strlen(pIsoCountryName);
+
+    if (len == 0)
+    {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
+
+    return u_charsToUChars_safe(pIsoCountryName, value, valueLength);
+}
+
+/*
+Function:
+GetLocaleCurrencyName
+
+Gets the locale currency English or native name and convert the result to UChars
+*/
+UErrorCode GetLocaleCurrencyName(const char* locale, bool nativeName, UChar* value, int32_t valueLength)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    
+    UChar currencyThreeLettersName[4]; // 3 letters currency iso name + NULL
+    ucurr_forLocale(locale, currencyThreeLettersName, 4, &status);
+    if (!U_SUCCESS(status))
+    {
+        return status;
+    }
+    
+    int32_t len;
+    UBool formatChoice;
+    const UChar *pCurrencyLongName = ucurr_getName(
+                                        currencyThreeLettersName, 
+                                        nativeName ? locale : ULOC_US, 
+                                        UCURR_LONG_NAME, 
+                                        &formatChoice, 
+                                        &len, 
+                                        &status);
+    if (!U_SUCCESS(status))
+    {
+        return status;
+    }
+    
+    if (len >= valueLength) // we need to have room for NULL too
+    {
+        return U_BUFFER_OVERFLOW_ERROR;
+    }
+    u_strncpy(value, pCurrencyLongName, len);
+    value[len] = 0;
+    
+    return status;
+}
+
+/*
 PAL Function:
 GetLocaleInfoString
 
@@ -226,6 +289,12 @@ extern "C" int32_t GlobalizationNative_GetLocaleInfoString(
         case Iso4217MonetarySymbol:
             status = GetLocaleInfoDecimalFormatSymbol(locale, UNUM_INTL_CURRENCY_SYMBOL, value, valueLength);
             break;
+        case CurrencyEnglishName:
+            status = GetLocaleCurrencyName(locale, false, value, valueLength);
+            break;
+        case CurrencyNativeName:
+            status = GetLocaleCurrencyName(locale, true, value, valueLength);
+            break;
         case MonetaryDecimalSeparator:
             status = GetLocaleInfoDecimalFormatSymbol(locale, UNUM_MONETARY_SEPARATOR_SYMBOL, value, valueLength);
             break;
@@ -251,6 +320,9 @@ extern "C" int32_t GlobalizationNative_GetLocaleInfoString(
         case Iso3166CountryName:
             status = GetLocaleIso3166CountryName(locale, value, valueLength);
             break;
+        case Iso3166CountryName2:
+            status = GetLocaleIso3166CountryCode(locale, value, valueLength);
+            break;
         case NaNSymbol:
             status = GetLocaleInfoDecimalFormatSymbol(locale, UNUM_NAN_SYMBOL, value, valueLength);
             break;
index 44c9b7d..c9ce206 100644 (file)
@@ -144,6 +144,11 @@ namespace System.Globalization
             get { return Environment.GetResourceString("Argument_CultureNotSupported"); }
         }
 
+        public static string Argument_CultureIsNeutral
+        {
+            get { return Environment.GetResourceString("Argument_CultureIsNeutral"); }
+        }
+
         public static string Argument_CustomCultureCannotBePassedByNumber
         {
             get { return Environment.GetResourceString("Argument_CustomCultureCannotBePassedByNumber"); }
index 04e91c3..41c6ae0 100644 (file)
@@ -333,5 +333,11 @@ namespace System.Globalization
             int ebcdicCodePage = LocaleData.GetLocaleDataNumericPart(cultureName, LocaleDataParts.EbcdicCodePage);
             return ebcdicCodePage == -1 ? CultureData.Invariant.IDEFAULTEBCDICCODEPAGE : ebcdicCodePage; 
         }
+
+        private static int GetGeoId(string cultureName)
+        {
+            int geoId = LocaleData.GetLocaleDataNumericPart(cultureName, LocaleDataParts.GeoId);
+            return geoId == -1 ? CultureData.Invariant.IGEOID : geoId; 
+        }
     }
 }
index a00568c..7073251 100644 (file)
@@ -587,5 +587,10 @@ namespace System.Globalization
         {
             throw new NotImplementedException();
         }
+
+        private static int GetGeoId(string cultureName)
+        {
+            throw new NotImplementedException();
+        }
     }
 }
index 5d31ce7..02ada7f 100644 (file)
@@ -84,6 +84,8 @@ namespace System.Globalization
         private String _sEnglishCountry; // english country name (RegionInfo)
         private String _sNativeCountry; // native country name
         private String _sISO3166CountryName; // ISO 3166 (RegionInfo), ie: US
+        private String _sISO3166CountryName2; // 3 char ISO 3166 country name 2 2(RegionInfo) ex: USA (ISO)
+        private int    _iGeoId = undef; // GeoId
 
         // Numbers
         private String _sPositiveSign; // (user can override) positive sign
@@ -108,6 +110,8 @@ namespace System.Globalization
         // Currency
         private String _sCurrency; // (user can override) local monetary symbol
         private String _sIntlMonetarySymbol; // international monetary symbol (RegionInfo)
+        private String _sEnglishCurrency; // English name for this currency
+        private String _sNativeCurrency; // Native name for this currency
         // (nfi populates these 4, don't have to be = undef)
         private int _iCurrencyDigits; // (user can override) # local monetary fractional digits
         private int _iCurrency; // (user can override) positive currency format
@@ -432,10 +436,12 @@ namespace System.Globalization
                     invariant._sNativeLanguage = "Invariant Language";   // Native name of this language
 
                     // Region
-                    invariant._sRegionName = "IV";                   // (RegionInfo)
-                    invariant._sEnglishCountry = "Invariant Country";    // english country name (RegionInfo)
-                    invariant._sNativeCountry = "Invariant Country";    // native country name (Windows Only)
-                    invariant._sISO3166CountryName = "IV";                   // (RegionInfo), ie: US
+                    invariant._sRegionName = "IV";                    // (RegionInfo)
+                    invariant._sEnglishCountry = "Invariant Country"; // english country name (RegionInfo)
+                    invariant._sNativeCountry = "Invariant Country";  // native country name (Windows Only)
+                    invariant._sISO3166CountryName = "IV";            // (RegionInfo), ie: US
+                           invariant._sISO3166CountryName2 = "ivc";          // 3 char ISO 3166 country name 2 2(RegionInfo)
+                           invariant._iGeoId = 244;                          // GeoId (Windows Only)
 
                     // Numbers
                     invariant._sPositiveSign = "+";                    // positive sign
@@ -459,6 +465,8 @@ namespace System.Globalization
                     // Currency
                     invariant._sCurrency = "\x00a4";                // local monetary symbol: for international monetary symbol
                     invariant._sIntlMonetarySymbol = "XDR";                  // international monetary symbol (RegionInfo)
+                           invariant._sEnglishCurrency = "International Monetary Fund"; // English name for this currency (Windows Only)
+                           invariant._sNativeCurrency = "International Monetary Fund"; // Native name for this currency (Windows Only)
                     invariant._iCurrencyDigits = 2;                      // # local monetary fractional digits
                     invariant._iCurrency = 0;                      // positive currency format
                     invariant._iNegativeCurrency = 0;                      // negative currency format
@@ -492,7 +500,7 @@ namespace System.Globalization
 
                     // These are desktop only, not coreclr
 
-                    invariant._iLanguage = 0x007f;                 // locale ID (0409) - NO sort information
+                    invariant._iLanguage = CultureInfo.LOCALE_INVARIANT;   // locale ID (0409) - NO sort information
                     invariant._iDefaultAnsiCodePage = 1252;         // default ansi code page ID (ACP)
                     invariant._iDefaultOemCodePage = 437;           // default oem code page ID (OCP or OEM)
                     invariant._iDefaultMacCodePage = 10000;         // default macintosh code page
@@ -620,7 +628,7 @@ namespace System.Globalization
             string localeName = null;
             CultureData retVal = null;
 
-            if (culture == 0x007f)
+            if (culture == CultureInfo.LOCALE_INVARIANT)
                 return Invariant;
 
             // Convert the lcid to a name, then use that
@@ -958,6 +966,17 @@ namespace System.Globalization
             }
         }
 
+        internal int IGEOID
+        {
+            get
+            {
+                if (_iGeoId == undef)
+                {
+                    _iGeoId = GetGeoId(_sRealName);
+                }
+                return _iGeoId;
+            }
+        }
 
         // localized name for the country
         internal string SLOCALIZEDCOUNTRY
@@ -1023,6 +1042,19 @@ namespace System.Globalization
             }
         }
 
+        // 3 letter ISO 3166 country code
+        internal String SISO3166CTRYNAME2
+        {
+            get
+            {
+                if (_sISO3166CountryName2 == null)
+                {
+                    _sISO3166CountryName2 = GetLocaleInfo(LocaleStringData.Iso3166CountryName2);
+                }
+                return _sISO3166CountryName2;
+            }
+        }
+
         /////////////
         // Numbers //
         ////////////
@@ -1180,6 +1212,32 @@ namespace System.Globalization
             }
         }
 
+        // English name for this currency (RegionInfo), eg: US Dollar
+        internal String SENGLISHCURRENCY
+        {
+            get
+            {
+                if (_sEnglishCurrency == null)
+                {
+                    _sEnglishCurrency = GetLocaleInfo(LocaleStringData.CurrencyEnglishName);
+                }
+                return _sEnglishCurrency;
+            }
+        }
+
+        // Native name for this currency (RegionInfo), eg: Schweiz Frank
+        internal String SNATIVECURRENCY
+        {
+            get
+            {
+                if (_sNativeCurrency == null)
+                {
+                    _sNativeCurrency = GetLocaleInfo(LocaleStringData.CurrencyNativeName);
+                }
+                return _sNativeCurrency;
+            }
+        }
+
         //                internal int iCurrencyDigits          ; // (user can override) # local monetary fractional digits
         //                internal int iCurrency                ; // (user can override) positive currency format
         //                internal int iNegativeCurrency        ; // (user can override) negative currency format
@@ -2192,6 +2250,10 @@ namespace System.Globalization
             Digits = 0x00000013,
             /// <summary>local monetary symbol (coresponds to LOCALE_SCURRENCY)</summary>
             MonetarySymbol = 0x00000014,
+            /// <summary>English currency name (coresponds to LOCALE_SENGCURRNAME)</summary>
+            CurrencyEnglishName = 0x00001007,
+            /// <summary>Native currency name (coresponds to LOCALE_SNATIVECURRNAME)</summary>
+            CurrencyNativeName = 0x00001008,
             /// <summary>uintl monetary symbol (coresponds to LOCALE_SINTLSYMBOL)</summary>
             Iso4217MonetarySymbol = 0x00000015,
             /// <summary>monetary decimal separator (coresponds to LOCALE_SMONDECIMALSEP)</summary>
@@ -2210,6 +2272,8 @@ namespace System.Globalization
             Iso639LanguageName = 0x00000059,
             /// <summary>ISO abbreviated country name (coresponds to LOCALE_SISO3166CTRYNAME)</summary>
             Iso3166CountryName = 0x0000005A,
+            /// <summary>3 letter ISO country code (coresponds to LOCALE_SISO3166CTRYNAME2)</summary>
+            Iso3166CountryName2 = 0x00000068,   // 3 character ISO country name
             /// <summary>Not a Number (coresponds to LOCALE_SNAN)</summary>
             NaNSymbol = 0x00000069,
             /// <summary>+ Infinity (coresponds to LOCALE_SPOSINFINITY)</summary>
index 6b64386..a5fa224 100644 (file)
@@ -150,11 +150,12 @@ namespace System.Globalization
 
         // LOCALE constants of interest to us internally and privately for LCID functions
         // (ie: avoid using these and use names if possible)
-        private  const int LOCALE_NEUTRAL        = 0x0000;
+        internal const int LOCALE_NEUTRAL        = 0x0000;
         private  const int LOCALE_USER_DEFAULT   = 0x0400;
         private  const int LOCALE_SYSTEM_DEFAULT = 0x0800;
         internal const int LOCALE_CUSTOM_UNSPECIFIED = 0x1000;
-        internal const int LOCALE_CUSTOM_DEFAULT = 0x0c00;
+        internal const int LOCALE_CUSTOM_DEFAULT  = 0x0c00;
+        internal const int LOCALE_INVARIANT       = 0x007F;
 
         static AsyncLocal<CultureInfo> s_asyncLocalCurrentCulture; 
         static AsyncLocal<CultureInfo> s_asyncLocalCurrentUICulture;
index 916ed42..eb131fe 100644 (file)
@@ -10,12 +10,12 @@ namespace System.Globalization
 {
     internal enum LocaleDataParts
     {
-        Lcid = 0, 
-        AnsiCodePage = 1, 
-        OemCodePage = 2, 
-        MacCodePage = 3, 
+        Lcid = 0,
+        AnsiCodePage = 1,
+        OemCodePage = 2,
+        MacCodePage = 3,
         EbcdicCodePage = 4,
-        GeoId = 5    
+        GeoId = 5
     }
     
     internal partial class LocaleData
index 0669349..05e0e12 100644 (file)
@@ -88,6 +88,36 @@ namespace System.Globalization
             SetName(name);
         }
 
+        [System.Security.SecuritySafeCritical]  // auto-generated
+        public RegionInfo(int culture)
+        {
+            if (culture == CultureInfo.LOCALE_INVARIANT) //The InvariantCulture has no matching region
+            { 
+                throw new ArgumentException(SR.Argument_NoRegionInvariantCulture);
+            }
+            
+            if (culture == CultureInfo.LOCALE_NEUTRAL)
+            {
+                // Not supposed to be neutral
+                throw new ArgumentException(SR.Format(SR.Argument_CultureIsNeutral, culture), "culture");
+            }
+
+            if (culture == CultureInfo.LOCALE_CUSTOM_DEFAULT)
+            {
+                // Not supposed to be neutral
+                throw new ArgumentException(SR.Format(SR.Argument_CustomCultureCannotBePassedByNumber, culture), "culture");
+            }
+            
+            _cultureData = CultureData.GetCultureData(culture, true);
+            _name = _cultureData.SREGIONNAME;
+
+            if (_cultureData.IsNeutralCulture)
+            {
+                // Not supposed to be neutral
+                throw new ArgumentException(SR.Format(SR.Argument_CultureIsNeutral, culture), "culture");
+            }
+        }
+
         internal RegionInfo(CultureData cultureData)
         {
             _cultureData = cultureData;
@@ -228,6 +258,38 @@ namespace System.Globalization
 
         ////////////////////////////////////////////////////////////////////////
         //
+        //  ThreeLetterISORegionName
+        //
+        //  Returns the three letter ISO region name (ie: USA)
+        //
+        ////////////////////////////////////////////////////////////////////////
+        public virtual String ThreeLetterISORegionName
+        {
+            get
+            {
+                return (_cultureData.SISO3166CTRYNAME2);
+            }
+        }
+
+        ////////////////////////////////////////////////////////////////////////
+        //
+        //  ThreeLetterWindowsRegionName
+        //
+        //  Returns the three letter windows region name (ie: USA)
+        //
+        ////////////////////////////////////////////////////////////////////////
+        public virtual String ThreeLetterWindowsRegionName
+        {
+            get
+            {
+                // ThreeLetterWindowsRegionName is really same as ThreeLetterISORegionName 
+                return ThreeLetterISORegionName;
+            }
+        }
+
+
+        ////////////////////////////////////////////////////////////////////////
+        //
         //  IsMetric
         //
         //  Returns true if this region uses the metric measurement system
@@ -242,6 +304,48 @@ namespace System.Globalization
             }
         }
 
+        [System.Runtime.InteropServices.ComVisible(false)]        
+        public virtual int GeoId 
+        {
+            get 
+            {
+                return (_cultureData.IGEOID);
+            }
+        }
+
+        ////////////////////////////////////////////////////////////////////////
+        //
+        //  CurrencyEnglishName
+        //
+        //  English name for this region's currency, ie: Swiss Franc
+        //
+        ////////////////////////////////////////////////////////////////////////
+        [System.Runtime.InteropServices.ComVisible(false)]
+        public virtual string CurrencyEnglishName
+        {
+            get
+            {
+                return (_cultureData.SENGLISHCURRENCY);
+            }
+        }
+
+        ////////////////////////////////////////////////////////////////////////
+        //
+        //  CurrencyNativeName
+        //
+        //  Native name for this region's currency, ie: Schweizer Franken
+        //  WARNING: You need a full locale name for this to make sense.
+        //
+        ////////////////////////////////////////////////////////////////////////
+        [System.Runtime.InteropServices.ComVisible(false)]
+        public virtual string CurrencyNativeName
+        {
+            get
+            {
+                return (_cultureData.SNATIVECURRENCY);
+            }
+        }
+
         ////////////////////////////////////////////////////////////////////////
         //
         //  CurrencySymbol
index 7c0b3ef..5318d83 100644 (file)
@@ -67,17 +67,4 @@ namespace System.Globalization
         [System.Runtime.InteropServices.ComVisibleAttribute(false)]
         public string[] NativeDigits { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
     }
-
-    public partial class RegionInfo
-    {
-        public RegionInfo(int culture) { throw new NotImplementedException(); }
-        [System.Runtime.InteropServices.ComVisibleAttribute(false)]
-        public virtual string CurrencyEnglishName { get { throw new NotImplementedException(); } }
-        [System.Runtime.InteropServices.ComVisibleAttribute(false)]
-        public virtual string CurrencyNativeName { get { throw new NotImplementedException(); } }
-        [System.Runtime.InteropServices.ComVisibleAttribute(false)]
-        public virtual int GeoId { get { throw new NotImplementedException(); } }
-        public virtual string ThreeLetterISORegionName { get { throw new NotImplementedException(); } }
-        public virtual string ThreeLetterWindowsRegionName { get { throw new NotImplementedException(); } }
-    }
 }
\ No newline at end of file