mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Allow non-default encodings to be used in user's script/code (#18605)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System.Linq;
|
||||
using System.Management.Automation.Host;
|
||||
using System.Management.Automation.Language;
|
||||
using System.Management.Automation.Runspaces;
|
||||
using System.Text;
|
||||
|
||||
namespace System.Management.Automation
|
||||
{
|
||||
@@ -14,8 +14,15 @@ namespace System.Management.Automation
|
||||
/// </summary>
|
||||
internal class AutomationEngine
|
||||
{
|
||||
static AutomationEngine()
|
||||
{
|
||||
// Register the encoding provider to load encodings that are not supported by default,
|
||||
// so as to allow them to be used in user's script/code.
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
}
|
||||
|
||||
// Holds the parser to use for this instance of the engine...
|
||||
internal Language.Parser EngineParser;
|
||||
internal Parser EngineParser;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the handle to the execution context
|
||||
|
||||
@@ -107,10 +107,8 @@ namespace System.Management.Automation
|
||||
{
|
||||
if (s_oemEncoding == null)
|
||||
{
|
||||
// load all available encodings
|
||||
EncodingRegisterProvider();
|
||||
#if UNIX
|
||||
s_oemEncoding = new UTF8Encoding(false);
|
||||
s_oemEncoding = Encoding.Default;
|
||||
#else
|
||||
uint oemCp = Interop.Windows.GetOEMCP();
|
||||
s_oemEncoding = Encoding.GetEncoding((int)oemCp);
|
||||
@@ -122,14 +120,6 @@ namespace System.Management.Automation
|
||||
|
||||
private static volatile Encoding s_oemEncoding;
|
||||
|
||||
private static void EncodingRegisterProvider()
|
||||
{
|
||||
if (s_oemEncoding == null)
|
||||
{
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Encoding
|
||||
|
||||
#if !UNIX
|
||||
|
||||
@@ -30,21 +30,21 @@ namespace System.Management.Automation
|
||||
|
||||
internal static readonly Dictionary<string, Encoding> encodingMap = new Dictionary<string, Encoding>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
{ Ascii, System.Text.Encoding.ASCII },
|
||||
{ BigEndianUnicode, System.Text.Encoding.BigEndianUnicode },
|
||||
{ Ascii, Encoding.ASCII },
|
||||
{ BigEndianUnicode, Encoding.BigEndianUnicode },
|
||||
{ BigEndianUtf32, new UTF32Encoding(bigEndian: true, byteOrderMark: true) },
|
||||
{ Default, Encoding.Default },
|
||||
{ OEM, ClrFacade.GetOEMEncoding() },
|
||||
{ Unicode, System.Text.Encoding.Unicode },
|
||||
{ Unicode, Encoding.Unicode },
|
||||
#pragma warning disable SYSLIB0001
|
||||
{ Utf7, System.Text.Encoding.UTF7 },
|
||||
{ Utf7, Encoding.UTF7 },
|
||||
#pragma warning restore SYSLIB0001
|
||||
{ Utf8, Encoding.Default },
|
||||
{ Utf8Bom, System.Text.Encoding.UTF8 },
|
||||
{ Utf8Bom, Encoding.UTF8 },
|
||||
{ Utf8NoBom, Encoding.Default },
|
||||
{ Utf32, System.Text.Encoding.UTF32 },
|
||||
{ String, System.Text.Encoding.Unicode },
|
||||
{ Unknown, System.Text.Encoding.Unicode },
|
||||
{ Utf32, Encoding.UTF32 },
|
||||
{ String, Encoding.Unicode },
|
||||
{ Unknown, Encoding.Unicode },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -49,6 +49,7 @@ Describe "Validate start of console host" -Tag CI {
|
||||
'System.Security.AccessControl.dll'
|
||||
'System.Security.Cryptography.dll'
|
||||
'System.Security.Principal.Windows.dll'
|
||||
'System.Text.Encoding.CodePages.dll'
|
||||
'System.Text.Encoding.Extensions.dll'
|
||||
'System.Text.RegularExpressions.dll'
|
||||
'System.Threading.dll'
|
||||
|
||||
@@ -128,4 +128,10 @@ public class NullStringTest {
|
||||
[NullStringTest]::Get([NullString]::Value) | Should -BeExactly 'System.String'
|
||||
[NullStringTest]::Get(@('foo', [NullString]::Value, 'bar')) | Should -BeExactly 'System.String[]; 2nd element is NULL'
|
||||
}
|
||||
|
||||
It 'Non-default encoding should work in PowerShell' {
|
||||
$powershell = Join-Path -Path $PSHOME -ChildPath "pwsh"
|
||||
$result = & $powershell -noprofile -c '[System.Text.Encoding]::GetEncoding("IBM437").WebName'
|
||||
$result | Should -BeExactly "ibm437"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user