Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor UTF-7 encoding detection #18384

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7656,15 +7656,22 @@ public Encoding Encoding

set
{
// Check for UTF-7 by checking for code page 65000
// See: https://docs.microsoft.com/en-us/dotnet/core/compatibility/corefx#utf-7-code-paths-are-obsolete
if (value != null && value.CodePage == 65000)
{
_provider.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete);
}
WarnIfObsolete(value);
xtqqczze marked this conversation as resolved.
Show resolved Hide resolved
_encoding = value;
// If an encoding was explicitly set, be sure to capture that.
WasStreamTypeSpecified = true;

void WarnIfObsolete(Encoding value)
{
const int CodePageUtf7 = 65000;

// Use the recommended pattern to check for UTF-7.
// https://docs.microsoft.com/dotnet/core/compatibility/core-libraries/5.0/utf-7-code-paths-obsolete
if (value is { CodePage: CodePageUtf7 })
{
_provider.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete);
}
}
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/System.Management.Automation/utils/EncodingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ internal static Encoding Convert(Cmdlet cmdlet, string encoding)
Encoding foundEncoding;
if (encodingMap.TryGetValue(encoding, out foundEncoding))
{
// Write a warning if using utf7 as it is obsolete in .NET5
if (string.Equals(encoding, Utf7, StringComparison.OrdinalIgnoreCase))
{
cmdlet.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete);
}
WarnIfObsolete(cmdlet, foundEncoding);

return foundEncoding;
}
Expand Down Expand Up @@ -95,9 +91,11 @@ internal static Encoding Convert(Cmdlet cmdlet, string encoding)
/// <param name="encoding">The encoding to check for obsolescence.</param>
internal static void WarnIfObsolete(Cmdlet cmdlet, Encoding encoding)
{
// Check for UTF-7 by checking for code page 65000
// See: https://docs.microsoft.com/en-us/dotnet/core/compatibility/corefx#utf-7-code-paths-are-obsolete
if (encoding != null && encoding.CodePage == 65000)
const int CodePageUtf7 = 65000;

// Use the recommended pattern to check for UTF-7.
// https://docs.microsoft.com/dotnet/core/compatibility/core-libraries/5.0/utf-7-code-paths-obsolete
if (encoding is { CodePage: CodePageUtf7 })
{
cmdlet.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete);
}
Expand Down