From deb7947812aa758cb873a40bfcb0e23cbf49ce98 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sun, 23 Oct 2022 01:00:36 +0100 Subject: [PATCH 1/2] Replace `UTF8Encoding(false)` with `Encoding.Default` --- .../commands/utility/CsvCommands.cs | 4 ++-- .../FormatAndOutput/format-hex/Format-Hex.cs | 2 +- .../FormatAndOutput/out-file/Out-File.cs | 2 +- .../utility/ImplicitRemotingCommands.cs | 2 +- .../commands/utility/MatchString.cs | 2 +- .../commands/utility/Tee-Object.cs | 2 +- .../commands/utility/XmlCommands.cs | 2 +- .../engine/ExternalScriptInfo.cs | 2 +- .../engine/Modules/ScriptAnalysis.cs | 2 +- .../namespaces/FileSystemContentStream.cs | 2 +- .../namespaces/FileSystemProvider.cs | 6 +++--- .../utils/ClrFacade.cs | 21 ++----------------- .../utils/EncodingUtils.cs | 8 +++---- 13 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs index eb3ebacff4a..c42977996ae 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs @@ -229,7 +229,7 @@ public Encoding Encoding } } - private Encoding _encoding = ClrFacade.GetDefaultEncoding(); + private Encoding _encoding = Encoding.Default; /// /// Gets or sets property that sets append parameter. @@ -615,7 +615,7 @@ public Encoding Encoding } } - private Encoding _encoding = ClrFacade.GetDefaultEncoding(); + private Encoding _encoding = Encoding.Default; /// /// Avoid writing out duplicate warning messages when there are one or more unspecified names. diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs index 90d007adb2a..de73b2155a6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs @@ -85,7 +85,7 @@ public Encoding Encoding } } - private Encoding _encoding = ClrFacade.GetDefaultEncoding(); + private Encoding _encoding = Encoding.Default; /// /// Gets or sets count of bytes to read from the input stream. diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-file/Out-File.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-file/Out-File.cs index 2fbdb73dc9c..50025b12f97 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-file/Out-File.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-file/Out-File.cs @@ -91,7 +91,7 @@ public Encoding Encoding } } - private Encoding _encoding = ClrFacade.GetDefaultEncoding(); + private Encoding _encoding = Encoding.Default; /// /// Property that sets append parameter. diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs index f0044dd1885..8c378c41658 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs @@ -90,7 +90,7 @@ public Encoding Encoding } } - private Encoding _encoding = ClrFacade.GetDefaultEncoding(); + private Encoding _encoding = Encoding.Default; #endregion Parameters diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index b9964d92bcb..d6e0b004bdf 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -1363,7 +1363,7 @@ public Encoding Encoding } } - private Encoding _encoding = ClrFacade.GetDefaultEncoding(); + private Encoding _encoding = Encoding.Default; /// /// Gets or sets the number of context lines to collect. If set to a diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs index a23b0be61dc..d8b3261c118 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs @@ -81,7 +81,7 @@ public SwitchParameter Append [ArgumentToEncodingTransformationAttribute] [ArgumentEncodingCompletionsAttribute] [ValidateNotNullOrEmpty] - public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding(); + public Encoding Encoding { get; set; } = Encoding.Default; /// /// Variable parameter. diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs index 74a8ee1b7c9..1ce259b726e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs @@ -128,7 +128,7 @@ public Encoding Encoding } } - private Encoding _encoding = ClrFacade.GetDefaultEncoding(); + private Encoding _encoding = Encoding.Default; #endregion Command Line Parameters diff --git a/src/System.Management.Automation/engine/ExternalScriptInfo.cs b/src/System.Management.Automation/engine/ExternalScriptInfo.cs index bcd75bf85f3..1e3620a604d 100644 --- a/src/System.Management.Automation/engine/ExternalScriptInfo.cs +++ b/src/System.Management.Automation/engine/ExternalScriptInfo.cs @@ -515,7 +515,7 @@ private void ReadScriptContents() { using (FileStream readerStream = new FileStream(_path, FileMode.Open, FileAccess.Read)) { - Encoding defaultEncoding = ClrFacade.GetDefaultEncoding(); + Encoding defaultEncoding = Encoding.Default; using (StreamReader scriptReader = new StreamReader(readerStream, defaultEncoding)) { diff --git a/src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs b/src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs index 8720fc1f161..b26b4ab76d9 100644 --- a/src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs +++ b/src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs @@ -94,7 +94,7 @@ internal static string ReadScript(string path) { using (FileStream readerStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { - Encoding defaultEncoding = ClrFacade.GetDefaultEncoding(); + Encoding defaultEncoding = Encoding.Default; Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = readerStream.SafeFileHandle; using (StreamReader scriptReader = new StreamReader(readerStream, defaultEncoding)) diff --git a/src/System.Management.Automation/namespaces/FileSystemContentStream.cs b/src/System.Management.Automation/namespaces/FileSystemContentStream.cs index 352298107d5..4d3a01543b7 100644 --- a/src/System.Management.Automation/namespaces/FileSystemContentStream.cs +++ b/src/System.Management.Automation/namespaces/FileSystemContentStream.cs @@ -794,7 +794,7 @@ private bool ReadByteEncoded(bool waitChanges, List blocks, bool readBac // the changes if (waitChanges) { - WaitForChanges(_path, _mode, _access, _share, ClrFacade.GetDefaultEncoding()); + WaitForChanges(_path, _mode, _access, _share, Encoding.Default); byteRead = _stream.ReadByte(); } } diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 07625fc2ab0..292ac082d9e 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -6610,7 +6610,7 @@ public IContentReader GetContentReader(string path) // Defaults for the file read operation string delimiter = "\n"; - Encoding encoding = ClrFacade.GetDefaultEncoding(); + Encoding encoding = Encoding.Default; bool waitForChanges = false; bool streamTypeSpecified = false; @@ -6792,7 +6792,7 @@ public IContentWriter GetContentWriter(string path) // If this is true, then the content will be read as bytes bool usingByteEncoding = false; bool streamTypeSpecified = false; - Encoding encoding = ClrFacade.GetDefaultEncoding(); + Encoding encoding = Encoding.Default; const FileMode filemode = FileMode.OpenOrCreate; string streamName = null; bool suppressNewline = false; @@ -7668,7 +7668,7 @@ public Encoding Encoding } } - private Encoding _encoding = ClrFacade.GetDefaultEncoding(); + private Encoding _encoding = Encoding.Default; /// /// Return file contents as a byte stream or create file from a series of bytes. diff --git a/src/System.Management.Automation/utils/ClrFacade.cs b/src/System.Management.Automation/utils/ClrFacade.cs index cabac1bd335..2331eb15ce9 100644 --- a/src/System.Management.Automation/utils/ClrFacade.cs +++ b/src/System.Management.Automation/utils/ClrFacade.cs @@ -100,23 +100,6 @@ private static IEnumerable GetPSVisibleAssemblies() #region Encoding - /// - /// Facade for getting default encoding. - /// - internal static Encoding GetDefaultEncoding() - { - if (s_defaultEncoding == null) - { - // load all available encodings - EncodingRegisterProvider(); - s_defaultEncoding = new UTF8Encoding(false); - } - - return s_defaultEncoding; - } - - private static volatile Encoding s_defaultEncoding; - /// /// Facade for getting OEM encoding /// OEM encodings work on all platforms, or rather codepage 437 is available on both Windows and Non-Windows. @@ -142,7 +125,7 @@ internal static Encoding GetOEMEncoding() private static void EncodingRegisterProvider() { - if (s_defaultEncoding == null && s_oemEncoding == null) + if (s_oemEncoding == null) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); } @@ -263,7 +246,7 @@ private static SecurityZone ReadFromZoneIdentifierDataStream(string filePath) } // If we successfully get the zone data stream, try to read the ZoneId information - using (StreamReader zoneDataReader = new StreamReader(zoneDataStream, GetDefaultEncoding())) + using (StreamReader zoneDataReader = new StreamReader(zoneDataStream, Encoding.Default)) { string line = null; bool zoneTransferMatched = false; diff --git a/src/System.Management.Automation/utils/EncodingUtils.cs b/src/System.Management.Automation/utils/EncodingUtils.cs index e9802488006..b2ee2d8c877 100644 --- a/src/System.Management.Automation/utils/EncodingUtils.cs +++ b/src/System.Management.Automation/utils/EncodingUtils.cs @@ -33,15 +33,15 @@ internal static class EncodingConversion { Ascii, System.Text.Encoding.ASCII }, { BigEndianUnicode, System.Text.Encoding.BigEndianUnicode }, { BigEndianUtf32, new UTF32Encoding(bigEndian: true, byteOrderMark: true) }, - { Default, ClrFacade.GetDefaultEncoding() }, + { Default, Encoding.Default }, { OEM, ClrFacade.GetOEMEncoding() }, { Unicode, System.Text.Encoding.Unicode }, #pragma warning disable SYSLIB0001 { Utf7, System.Text.Encoding.UTF7 }, #pragma warning restore SYSLIB0001 - { Utf8, ClrFacade.GetDefaultEncoding() }, + { Utf8, Encoding.Default }, { Utf8Bom, System.Text.Encoding.UTF8 }, - { Utf8NoBom, ClrFacade.GetDefaultEncoding() }, + { Utf8NoBom, Encoding.Default }, { Utf32, System.Text.Encoding.UTF32 }, { String, System.Text.Encoding.Unicode }, { Unknown, System.Text.Encoding.Unicode }, @@ -57,7 +57,7 @@ internal static Encoding Convert(Cmdlet cmdlet, string encoding) if (string.IsNullOrEmpty(encoding)) { // no parameter passed, default to UTF8 - return ClrFacade.GetDefaultEncoding(); + return Encoding.Default; } Encoding foundEncoding; From 92eb081b954c6e8bf8996e276fb88475f374d674 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sun, 23 Oct 2022 01:07:35 +0100 Subject: [PATCH 2/2] inline locals --- src/System.Management.Automation/engine/ExternalScriptInfo.cs | 4 +--- .../engine/Modules/ScriptAnalysis.cs | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/engine/ExternalScriptInfo.cs b/src/System.Management.Automation/engine/ExternalScriptInfo.cs index 1e3620a604d..a44e514c785 100644 --- a/src/System.Management.Automation/engine/ExternalScriptInfo.cs +++ b/src/System.Management.Automation/engine/ExternalScriptInfo.cs @@ -515,9 +515,7 @@ private void ReadScriptContents() { using (FileStream readerStream = new FileStream(_path, FileMode.Open, FileAccess.Read)) { - Encoding defaultEncoding = Encoding.Default; - - using (StreamReader scriptReader = new StreamReader(readerStream, defaultEncoding)) + using (StreamReader scriptReader = new StreamReader(readerStream, Encoding.Default)) { _scriptContents = scriptReader.ReadToEnd(); _originalEncoding = scriptReader.CurrentEncoding; diff --git a/src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs b/src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs index b26b4ab76d9..f8b188f47b5 100644 --- a/src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs +++ b/src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs @@ -94,10 +94,9 @@ internal static string ReadScript(string path) { using (FileStream readerStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { - Encoding defaultEncoding = Encoding.Default; Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = readerStream.SafeFileHandle; - using (StreamReader scriptReader = new StreamReader(readerStream, defaultEncoding)) + using (StreamReader scriptReader = new StreamReader(readerStream, Encoding.Default)) { return scriptReader.ReadToEnd(); }