diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs index 16bf6d16f4bae..ee5187e3ca54c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs @@ -585,13 +585,11 @@ internal static int CompareOrdinalIgnoreCase(ref char strA, int lengthA, ref cha } } - if (length == 0) + if (length == 0 || GlobalizationMode.Invariant) { return lengthA - lengthB; } - Debug.Assert(!GlobalizationMode.Invariant); - range -= length; return CompareStringOrdinalIgnoreCaseCore(ref charA, lengthA - range, ref charB, lengthB - range); diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs index ed4f160fc7db5..5b36b22316e40 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs @@ -926,7 +926,7 @@ internal string DisplayName get { string? localizedDisplayName = _sLocalizedDisplayName; - if (localizedDisplayName == null) + if (localizedDisplayName == null && !GlobalizationMode.Invariant) { if (IsSupplementalCustomCulture) { @@ -995,7 +995,7 @@ internal string DisplayName _sLocalizedDisplayName = localizedDisplayName; } - return localizedDisplayName; + return localizedDisplayName!; } } @@ -1011,7 +1011,7 @@ internal string EnglishName get { string? englishDisplayName = _sEnglishDisplayName; - if (englishDisplayName == null) + if (englishDisplayName == null && !GlobalizationMode.Invariant) { // If its neutral use the language name if (IsNeutralCulture) @@ -1056,7 +1056,7 @@ internal string EnglishName _sEnglishDisplayName = englishDisplayName; } - return englishDisplayName; + return englishDisplayName!; } } @@ -1068,7 +1068,7 @@ internal string NativeName get { string? nativeDisplayName = _sNativeDisplayName; - if (nativeDisplayName == null) + if (nativeDisplayName == null && !GlobalizationMode.Invariant) { // If its neutral use the language name if (IsNeutralCulture) @@ -1100,7 +1100,7 @@ internal string NativeName _sNativeDisplayName = nativeDisplayName; } - return nativeDisplayName; + return nativeDisplayName!; } } @@ -1142,7 +1142,7 @@ private string LocalizedLanguageName { get { - if (_sLocalizedLanguage == null) + if (_sLocalizedLanguage == null && !GlobalizationMode.Invariant) { // Usually the UI culture shouldn't be different than what we got from WinRT except // if DefaultThreadCurrentUICulture was set @@ -1160,7 +1160,7 @@ private string LocalizedLanguageName } } - return _sLocalizedLanguage; + return _sLocalizedLanguage!; } } @@ -1183,7 +1183,7 @@ internal int GeoId { get { - if (_iGeoId == undef) + if (_iGeoId == undef && !GlobalizationMode.Invariant) { _iGeoId = GlobalizationMode.UseNls ? NlsGetGeoId(_sRealName!) : IcuGetGeoId(_sRealName!); } @@ -1199,7 +1199,7 @@ internal string LocalizedCountryName get { string? localizedCountry = _sLocalizedCountry; - if (localizedCountry == null) + if (localizedCountry == null && !GlobalizationMode.Invariant) { try { @@ -1214,7 +1214,7 @@ internal string LocalizedCountryName _sLocalizedCountry = localizedCountry; } - return localizedCountry; + return localizedCountry!; } } @@ -1389,7 +1389,7 @@ internal string[] LongTimes { get { - if (_saLongTimes == null) + if (_saLongTimes == null && !GlobalizationMode.Invariant) { Debug.Assert(!GlobalizationMode.Invariant); @@ -1403,7 +1403,7 @@ internal string[] LongTimes _saLongTimes = longTimes; } } - return _saLongTimes; + return _saLongTimes!; } } @@ -1415,7 +1415,7 @@ internal string[] ShortTimes { get { - if (_saShortTimes == null) + if (_saShortTimes == null && !GlobalizationMode.Invariant) { Debug.Assert(!GlobalizationMode.Invariant); @@ -1434,7 +1434,7 @@ internal string[] ShortTimes // Found short times, use them _saShortTimes = shortTimes; } - return _saShortTimes; + return _saShortTimes!; } } @@ -1569,7 +1569,7 @@ internal int FirstDayOfWeek { get { - if (_iFirstDayOfWeek == undef) + if (_iFirstDayOfWeek == undef && !GlobalizationMode.Invariant) { _iFirstDayOfWeek = GlobalizationMode.UseNls ? NlsGetFirstDayOfWeek() : IcuGetFirstDayOfWeek(); } @@ -1801,7 +1801,7 @@ private int ReadingLayout { get { - if (_iReadingLayout == undef) + if (_iReadingLayout == undef && !GlobalizationMode.Invariant) { Debug.Assert(_sRealName != null, "[CultureData.IsRightToLeft] Expected _sRealName to be populated by already"); _iReadingLayout = GetLocaleInfoCore(LocaleNumberData.ReadingLayout); @@ -1853,7 +1853,7 @@ internal int ANSICodePage { get { - if (_iDefaultAnsiCodePage == undef) + if (_iDefaultAnsiCodePage == undef && !GlobalizationMode.Invariant) { _iDefaultAnsiCodePage = GlobalizationMode.UseNls ? NlsGetAnsiCodePage(_sRealName!) : IcuGetAnsiCodePage(_sRealName!); } @@ -1868,7 +1868,7 @@ internal int OEMCodePage { get { - if (_iDefaultOemCodePage == undef) + if (_iDefaultOemCodePage == undef && !GlobalizationMode.Invariant) { _iDefaultOemCodePage = GlobalizationMode.UseNls ? NlsGetOemCodePage(_sRealName!) : IcuGetOemCodePage(_sRealName!); } @@ -1883,7 +1883,7 @@ internal int MacCodePage { get { - if (_iDefaultMacCodePage == undef) + if (_iDefaultMacCodePage == undef && !GlobalizationMode.Invariant) { _iDefaultMacCodePage = GlobalizationMode.UseNls ? NlsGetMacCodePage(_sRealName!) : IcuGetMacCodePage(_sRealName!); } @@ -1898,7 +1898,7 @@ internal int EBCDICCodePage { get { - if (_iDefaultEbcdicCodePage == undef) + if (_iDefaultEbcdicCodePage == undef && !GlobalizationMode.Invariant) { _iDefaultEbcdicCodePage = GlobalizationMode.UseNls ? NlsGetEbcdicCodePage(_sRealName!) : IcuGetEbcdicCodePage(_sRealName!); } @@ -1910,7 +1910,7 @@ internal int LCID { get { - if (_iLanguage == 0) + if (_iLanguage == 0 && !GlobalizationMode.Invariant) { Debug.Assert(_sRealName != null, "[CultureData.LCID] Expected this.sRealName to be populated already"); _iLanguage = GlobalizationMode.UseNls ? NlsLocaleNameToLCID(_sRealName) : IcuLocaleNameToLCID(_sRealName); @@ -1978,7 +1978,7 @@ internal string TimeSeparator { get { - if (_sTimeSeparator == null) + if (_sTimeSeparator == null && !GlobalizationMode.Invariant) { string? longTimeFormat = GlobalizationMode.UseNls ? NlsGetTimeFormatString() : IcuGetTimeFormatString(); if (string.IsNullOrEmpty(longTimeFormat)) @@ -1989,7 +1989,7 @@ internal string TimeSeparator // Compute STIME from time format _sTimeSeparator = GetTimeSeparator(longTimeFormat); } - return _sTimeSeparator; + return _sTimeSeparator!; } } @@ -1998,6 +1998,11 @@ internal string TimeSeparator /// internal string DateSeparator(CalendarId calendarId) { + if (GlobalizationMode.Invariant) + { + return "/"; + } + if (calendarId == CalendarId.JAPAN && !LocalAppContextSwitches.EnforceLegacyJapaneseDateParsing) { // The date separator is derived from the default short date pattern. So far this pattern is using @@ -2252,21 +2257,41 @@ internal void GetNFIValues(NumberFormatInfo nfi) /// internal static string AnsiToLower(string testString) => TextInfo.ToLowerAsciiInvariant(testString); - private int GetLocaleInfoCore(LocaleNumberData type) => GlobalizationMode.UseNls ? - NlsGetLocaleInfo(type) : - IcuGetLocaleInfo(type); + private int GetLocaleInfoCore(LocaleNumberData type) + { + // This is never reached but helps illinker statically remove dependencies + if (GlobalizationMode.Invariant) + return 0; - private string GetLocaleInfoCore(LocaleStringData type) => GlobalizationMode.UseNls ? - NlsGetLocaleInfo(type) : - IcuGetLocaleInfo(type); + return GlobalizationMode.UseNls ? NlsGetLocaleInfo(type) : IcuGetLocaleInfo(type); + } - private string GetLocaleInfoCore(string localeName, LocaleStringData type) => GlobalizationMode.UseNls ? - NlsGetLocaleInfo(localeName, type) : - IcuGetLocaleInfo(localeName, type); + private string GetLocaleInfoCore(LocaleStringData type) + { + // This is never reached but helps illinker statically remove dependencies + if (GlobalizationMode.Invariant) + return null!; - private int[] GetLocaleInfoCore(LocaleGroupingData type) => GlobalizationMode.UseNls ? - NlsGetLocaleInfo(type) : - IcuGetLocaleInfo(type); + return GlobalizationMode.UseNls ? NlsGetLocaleInfo(type) : IcuGetLocaleInfo(type); + } + + private string GetLocaleInfoCore(string localeName, LocaleStringData type) + { + // This is never reached but helps illinker statically remove dependencies + if (GlobalizationMode.Invariant) + return null!; + + return GlobalizationMode.UseNls ? NlsGetLocaleInfo(localeName, type) : IcuGetLocaleInfo(localeName, type); + } + + private int[] GetLocaleInfoCore(LocaleGroupingData type) + { + // This is never reached but helps illinker statically remove dependencies + if (GlobalizationMode.Invariant) + return null!; + + return GlobalizationMode.UseNls ? NlsGetLocaleInfo(type) : IcuGetLocaleInfo(type); + } /// /// The numeric values of the enum members match their Win32 counterparts. The CultureData Win32 PAL implementation diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormatInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormatInfo.cs index ae15d95a30e40..bf1689ce6da0a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormatInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormatInfo.cs @@ -2037,8 +2037,6 @@ internal TokenHashValue[] CreateTokenHashTable() { temp = new TokenHashValue[TOKEN_HASH_SIZE]; - bool koreanLanguage = LanguageName.Equals(KoreanLangName); - string sep = TimeSeparator.Trim(); if (IgnorableComma != sep) InsertHash(temp, IgnorableComma, TokenType.IgnorableSymbol, 0); if (IgnorablePeriod != sep) InsertHash(temp, IgnorablePeriod, TokenType.IgnorableSymbol, 0); @@ -2087,7 +2085,7 @@ internal TokenHashValue[] CreateTokenHashTable() } // TODO: This ignores other custom cultures that might want to do something similar - if (koreanLanguage) + if (LanguageName.Equals(KoreanLangName)) { // Korean suffix InsertHash(temp, KoreanHourSuff, TokenType.SEP_HourSuff, 0); @@ -2206,37 +2204,40 @@ internal TokenHashValue[] CreateTokenHashTable() InsertHash(temp, GetAbbreviatedEraName(i), TokenType.EraToken, i); } - if (LanguageName.Equals(JapaneseLangName)) + if (!GlobalizationMode.Invariant) { - // Japanese allows day of week forms like: "(Tue)" - for (int i = 0; i < 7; i++) - { - string specialDayOfWeek = "(" + GetAbbreviatedDayName((DayOfWeek)i) + ")"; - InsertHash(temp, specialDayOfWeek, TokenType.DayOfWeekToken, i); - } - if (Calendar.GetType() != typeof(JapaneseCalendar)) + if (LanguageName.Equals(JapaneseLangName)) { - // Special case for Japanese. If this is a Japanese DTFI, and the calendar is not Japanese calendar, - // we will check Japanese Era name as well when the calendar is Gregorian. - DateTimeFormatInfo jaDtfi = GetJapaneseCalendarDTFI(); - for (int i = 1; i <= jaDtfi.Calendar.Eras.Length; i++) + // Japanese allows day of week forms like: "(Tue)" + for (int i = 0; i < 7; i++) { - InsertHash(temp, jaDtfi.GetEraName(i), TokenType.JapaneseEraToken, i); - InsertHash(temp, jaDtfi.GetAbbreviatedEraName(i), TokenType.JapaneseEraToken, i); - // m_abbrevEnglishEraNames[0] contains the name for era 1, so the token value is i+1. - InsertHash(temp, jaDtfi.AbbreviatedEnglishEraNames[i - 1], TokenType.JapaneseEraToken, i); + string specialDayOfWeek = "(" + GetAbbreviatedDayName((DayOfWeek)i) + ")"; + InsertHash(temp, specialDayOfWeek, TokenType.DayOfWeekToken, i); + } + if (Calendar.GetType() != typeof(JapaneseCalendar)) + { + // Special case for Japanese. If this is a Japanese DTFI, and the calendar is not Japanese calendar, + // we will check Japanese Era name as well when the calendar is Gregorian. + DateTimeFormatInfo jaDtfi = GetJapaneseCalendarDTFI(); + for (int i = 1; i <= jaDtfi.Calendar.Eras.Length; i++) + { + InsertHash(temp, jaDtfi.GetEraName(i), TokenType.JapaneseEraToken, i); + InsertHash(temp, jaDtfi.GetAbbreviatedEraName(i), TokenType.JapaneseEraToken, i); + // m_abbrevEnglishEraNames[0] contains the name for era 1, so the token value is i+1. + InsertHash(temp, jaDtfi.AbbreviatedEnglishEraNames[i - 1], TokenType.JapaneseEraToken, i); + } } } - } - // TODO: This prohibits similar custom cultures, but we hard coded the name - else if (CultureName.Equals("zh-TW")) - { - DateTimeFormatInfo twDtfi = GetTaiwanCalendarDTFI(); - for (int i = 1; i <= twDtfi.Calendar.Eras.Length; i++) + // TODO: This prohibits similar custom cultures, but we hard coded the name + else if (CultureName.Equals("zh-TW")) { - if (twDtfi.GetEraName(i).Length > 0) + DateTimeFormatInfo twDtfi = GetTaiwanCalendarDTFI(); + for (int i = 1; i <= twDtfi.Calendar.Eras.Length; i++) { - InsertHash(temp, twDtfi.GetEraName(i), TokenType.TEraToken, i); + if (twDtfi.GetEraName(i).Length > 0) + { + InsertHash(temp, twDtfi.GetEraName(i), TokenType.TEraToken, i); + } } } }