Address post-merge feedback on TimeZoneInfo display names PR (#53229)
authorStephen Toub <stoub@microsoft.com>
Tue, 25 May 2021 22:11:02 +0000 (18:11 -0400)
committerGitHub <noreply@github.com>
Tue, 25 May 2021 22:11:02 +0000 (18:11 -0400)
src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.Win32.cs
src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs

index b52989b..4047bf6 100644 (file)
@@ -5,11 +5,10 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Globalization;
 using System.IO;
+using System.Runtime.InteropServices;
 using System.Security;
 using System.Threading;
 
-using Microsoft.Win32.SafeHandles;
-
 using Internal.Win32;
 
 using REG_TZI_FORMAT = Interop.Kernel32.REG_TZI_FORMAT;
@@ -36,8 +35,8 @@ namespace System
         private const int MaxKeyLength = 255;
         private const string InvariantUtcStandardDisplayName = "Coordinated Universal Time";
 
-        private static readonly Dictionary<string, string> s_FileMuiPathCache = new();
-        private static readonly TimeZoneInfo s_utcTimeZone = CreateUtcTimeZone();
+        private static readonly Dictionary<string, string> s_fileMuiPathCache = new();
+        private static readonly TimeZoneInfo s_utcTimeZone = CreateUtcTimeZone(); // must be initialized after s_fileMuiPathCache
 
         private sealed partial class CachedData
         {
@@ -749,9 +748,9 @@ namespace System
             string? result;
             string cacheKey = $"{cultureInfo.Name};{filePath}";
 
-            lock (s_FileMuiPathCache)
+            lock (s_fileMuiPathCache)
             {
-                if (s_FileMuiPathCache.TryGetValue(cacheKey, out result))
+                if (s_fileMuiPathCache.TryGetValue(cacheKey, out result))
                 {
                     return result;
                 }
@@ -759,9 +758,9 @@ namespace System
 
             result = GetFileMuiPath(filePath, cultureInfo);
 
-            lock (s_FileMuiPathCache)
+            lock (s_fileMuiPathCache)
             {
-                s_FileMuiPathCache.TryAdd(cacheKey, result);
+                s_fileMuiPathCache.TryAdd(cacheKey, result);
             }
 
             return result;
@@ -787,6 +786,7 @@ namespace System
                     Interop.Kernel32.MUI_USE_INSTALLED_LANGUAGES,
                     filePath, language, ref languageLength,
                     fileMuiPath, ref fileMuiPathLength, ref enumerator);
+                fileMuiPath[Interop.Kernel32.MAX_PATH] = '\0';
 
                 if (!succeeded)
                 {
@@ -802,33 +802,31 @@ namespace System
                         Interop.Kernel32.MUI_USER_PREFERRED_UI_LANGUAGES,
                         filePath, language, ref languageLength,
                         fileMuiPath, ref fileMuiPathLength, ref enumerator);
+                    fileMuiPath[Interop.Kernel32.MAX_PATH] = '\0';
 
                     if (succeeded)
                     {
-                        fileMuiPath[Interop.Kernel32.MAX_PATH] = '\0';
                         return new string(fileMuiPath);
                     }
 
-                    // Shouldn't get here, as there's always at least one language installed.
+                    Debug.Fail("Shouldn't get here, as there's always at least one language installed.");
                     return string.Empty;
                 }
 
                 // Lookup succeeded.  Check for exact match to the desired culture.
                 language[Interop.Kernel32.LOCALE_NAME_MAX_LENGTH] = '\0';
-                var lang = new string(language);
-                if (string.Equals(lang, cultureInfo.Name, StringComparison.OrdinalIgnoreCase))
+                ReadOnlySpan<char> lang = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(language);
+                if (lang.Equals(cultureInfo.Name, StringComparison.OrdinalIgnoreCase))
                 {
-                    fileMuiPath[Interop.Kernel32.MAX_PATH] = '\0';
                     return new string(fileMuiPath);
                 }
 
                 // Check for match of any parent of the language returned to the desired culture.
-                var ci = CultureInfo.GetCultureInfo(lang);
+                var ci = CultureInfo.GetCultureInfo(lang.ToString());
                 while (ci.Parent.Name != string.Empty)
                 {
                     if (ci.Parent.Name.Equals(cultureInfo.Name, StringComparison.OrdinalIgnoreCase))
                     {
-                        fileMuiPath[Interop.Kernel32.MAX_PATH] = '\0';
                         return new string(fileMuiPath);
                     }
 
@@ -876,11 +874,11 @@ namespace System
             string system32 = Environment.SystemDirectory;
 
             // trim the string "@tzres.dll" => "tzres.dll"
-            string tzresDll = resources[0].TrimStart('@');
+            ReadOnlySpan<char> tzresDll = resources[0].AsSpan().TrimStart('@');
 
             try
             {
-                filePath = Path.Combine(system32, tzresDll);
+                filePath = Path.Join(system32, tzresDll);
             }
             catch (ArgumentException)
             {
index 5f51b16..89844b5 100644 (file)
@@ -6,7 +6,6 @@ using System.Collections.ObjectModel;
 using System.Diagnostics;
 using System.Diagnostics.CodeAnalysis;
 using System.Globalization;
-using System.Runtime.InteropServices;
 using System.Runtime.Serialization;
 using System.Threading;