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 all 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 @@ -224,7 +224,7 @@ public Encoding Encoding

set
{
EncodingConversion.WarnIfObsolete(this, value);
EncodingConversion.WarnIfUtf7Encoding(this, value);
_encoding = value;
}
}
Expand Down Expand Up @@ -610,7 +610,7 @@ public Encoding Encoding

set
{
EncodingConversion.WarnIfObsolete(this, value);
EncodingConversion.WarnIfUtf7Encoding(this, value);
_encoding = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Encoding Encoding

set
{
EncodingConversion.WarnIfObsolete(this, value);
EncodingConversion.WarnIfUtf7Encoding(this, value);
_encoding = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Encoding Encoding

set
{
EncodingConversion.WarnIfObsolete(this, value);
EncodingConversion.WarnIfUtf7Encoding(this, value);
_encoding = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Encoding Encoding

set
{
EncodingConversion.WarnIfObsolete(this, value);
EncodingConversion.WarnIfUtf7Encoding(this, value);
_encoding = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ public Encoding Encoding

set
{
EncodingConversion.WarnIfObsolete(this, value);
EncodingConversion.WarnIfUtf7Encoding(this, value);
_encoding = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Encoding Encoding

set
{
EncodingConversion.WarnIfObsolete(this, value);
EncodingConversion.WarnIfUtf7Encoding(this, value);
_encoding = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public Encoding Encoding

set
{
EncodingConversion.WarnIfObsolete(this, value);
EncodingConversion.WarnIfUtf7Encoding(this, value);
_encoding = value;
}
}
Expand Down
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);
}
WarnIfUtf7Encoding(value);
_encoding = value;
// If an encoding was explicitly set, be sure to capture that.
WasStreamTypeSpecified = true;

void WarnIfUtf7Encoding(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);
}
}
Comment on lines +7664 to +7674
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems overkill to me. I prefer the original code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xtqqczze Can you please respond to this comment? Thanks!

Copy link
Contributor Author

@xtqqczze xtqqczze Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the symbolic constant because it explains the meaning at a glance without having to read the comment for an explanation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iSazonov, @SteveL-MSFT Thoughts?

Copy link
Collaborator

@iSazonov iSazonov Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess @daxian-dbw don't like the modern syntax in the place but ok with the new name WarnIfUtf7Encoding. If so I agree with him.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should be clearer on my comment.
It's not the syntax value is { CodePage: CodePageUtf7 }, but that you have a local method in a setter. Please avoid having a local method in a setter unless it's really needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xtqqczze Please revert the change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xtqqczze - please respond to this feedback

}
}

Expand Down
18 changes: 8 additions & 10 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);
}
WarnIfUtf7Encoding(cmdlet, foundEncoding);

return foundEncoding;
}
Expand All @@ -89,15 +85,17 @@ internal static Encoding Convert(Cmdlet cmdlet, string encoding)
}

/// <summary>
/// Warn if the encoding has been designated as obsolete.
/// Warn if the encoding is obsolete UTF-7 encoding.
/// </summary>
/// <param name="cmdlet">A cmdlet instance which is used to emit the warning.</param>
/// <param name="encoding">The encoding to check for obsolescence.</param>
internal static void WarnIfObsolete(Cmdlet cmdlet, Encoding encoding)
internal static void WarnIfUtf7Encoding(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