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
Unify cmdlets with parameter 'Encoding' to be of type System.Text.Encoding #5080
Changes from all commits
bcc2719
431370a
53b31e9
6ef8580
e29baac
b59b0ee
840343d
007bae4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,8 +212,20 @@ public SwitchParameter NoClobber | |
/// Encoding optional flag | ||
/// </summary> | ||
[Parameter()] | ||
[ValidateSetAttribute(new string[] { "Unicode", "UTF7", "UTF8", "ASCII", "UTF32", "BigEndianUnicode", "Default", "OEM" })] | ||
public string Encoding { get; set; } | ||
[ArgumentToEncodingTransformationAttribute()] | ||
[ArgumentCompletions( | ||
EncodingConversion.Ascii, | ||
EncodingConversion.BigEndianUnicode, | ||
EncodingConversion.OEM, | ||
EncodingConversion.Unicode, | ||
EncodingConversion.Utf7, | ||
EncodingConversion.Utf8, | ||
EncodingConversion.Utf8Bom, | ||
EncodingConversion.Utf8NoBom, | ||
EncodingConversion.Utf32 | ||
)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments for other instances of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that doesn't work, because arrays aren't constant (I tried that first) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, then we are good here. #Closed |
||
[ValidateNotNullOrEmpty] | ||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding(); | ||
|
||
/// <summary> | ||
/// Property that sets append parameter. | ||
|
@@ -373,7 +385,7 @@ private void CreateFileStream() | |
PathUtils.MasterStreamOpen( | ||
this, | ||
this.Path, | ||
Encoding ?? "ASCII", | ||
Encoding, | ||
false, // defaultEncoding | ||
Append, | ||
Force, | ||
|
@@ -577,8 +589,20 @@ public SwitchParameter UseCulture | |
/// Encoding optional flag | ||
/// </summary> | ||
[Parameter()] | ||
[ValidateSetAttribute(new[] { "Unicode", "UTF7", "UTF8", "ASCII", "UTF32", "BigEndianUnicode", "Default", "OEM" })] | ||
public string Encoding { get; set; } | ||
[ArgumentToEncodingTransformationAttribute()] | ||
[ArgumentCompletions( | ||
EncodingConversion.Ascii, | ||
EncodingConversion.BigEndianUnicode, | ||
EncodingConversion.OEM, | ||
EncodingConversion.Unicode, | ||
EncodingConversion.Utf7, | ||
EncodingConversion.Utf8, | ||
EncodingConversion.Utf8Bom, | ||
EncodingConversion.Utf8NoBom, | ||
EncodingConversion.Utf32 | ||
)] | ||
[ValidateNotNullOrEmpty] | ||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding(); | ||
|
||
/// <summary> | ||
/// Avoid writing out duplicate warning messages when there are | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
--********************************************************************/ | ||
|
||
using System; | ||
using System.Text; | ||
using System.Management.Automation; | ||
using System.Management.Automation.Internal; | ||
using System.Management.Automation.Host; | ||
|
@@ -72,25 +73,20 @@ public string LiteralPath | |
/// </summary> | ||
/// | ||
[Parameter(Position = 1)] | ||
[ValidateNotNullOrEmpty] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we still need this attribute, so that user cannot pass in a null value. |
||
[ValidateSetAttribute(new string[] { | ||
EncodingConversion.Unknown, | ||
EncodingConversion.String, | ||
EncodingConversion.Unicode, | ||
[ArgumentToEncodingTransformationAttribute()] | ||
[ArgumentCompletions( | ||
EncodingConversion.Ascii, | ||
EncodingConversion.BigEndianUnicode, | ||
EncodingConversion.Utf8, | ||
EncodingConversion.OEM, | ||
EncodingConversion.Unicode, | ||
EncodingConversion.Utf7, | ||
EncodingConversion.Utf32, | ||
EncodingConversion.Ascii, | ||
EncodingConversion.Default, | ||
EncodingConversion.OEM })] | ||
public string Encoding | ||
{ | ||
get { return _encoding; } | ||
set { _encoding = value; } | ||
} | ||
|
||
private string _encoding; | ||
EncodingConversion.Utf8, | ||
EncodingConversion.Utf8Bom, | ||
EncodingConversion.Utf8NoBom, | ||
EncodingConversion.Utf32 | ||
)] | ||
[ValidateNotNullOrEmpty] | ||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding(); | ||
|
||
/// <summary> | ||
/// Property that sets append parameter. | ||
|
@@ -196,7 +192,7 @@ private LineOutput InstantiateLineOutputInterface() | |
PathUtils.MasterStreamOpen( | ||
this, | ||
FilePath, | ||
_encoding, | ||
Encoding, | ||
false, // defaultEncoding | ||
Append, | ||
Force, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,19 +77,20 @@ public SwitchParameter Force | |
/// Encoding optional flag | ||
/// </summary> | ||
[Parameter] | ||
[ValidateSetAttribute(new string[] { "Unicode", "UTF7", "UTF8", "ASCII", "UTF32", "BigEndianUnicode", "Default", "OEM" })] | ||
public string Encoding | ||
{ | ||
get | ||
{ | ||
return _encoding.GetType().Name; | ||
} | ||
set | ||
{ | ||
_encoding = EncodingConversion.Convert(this, value); | ||
} | ||
} | ||
private Encoding _encoding = System.Text.Encoding.UTF8; | ||
[ArgumentToEncodingTransformationAttribute()] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
[ArgumentCompletions( | ||
EncodingConversion.Ascii, | ||
EncodingConversion.BigEndianUnicode, | ||
EncodingConversion.OEM, | ||
EncodingConversion.Unicode, | ||
EncodingConversion.Utf7, | ||
EncodingConversion.Utf8, | ||
EncodingConversion.Utf8Bom, | ||
EncodingConversion.Utf8NoBom, | ||
EncodingConversion.Utf32 | ||
)] | ||
[ValidateNotNullOrEmpty] | ||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding(); | ||
|
||
#endregion Parameters | ||
|
||
|
@@ -144,7 +145,7 @@ protected override void BeginProcessing() | |
List<string> generatedFiles = GenerateProxyModule( | ||
tempDirectory, | ||
Path.GetFileName(directory.FullName), | ||
_encoding, | ||
Encoding, | ||
_force, | ||
listOfCommandMetadata, | ||
alias2resolvedCommandName, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
--********************************************************************/ | ||
|
||
using System; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.IO; | ||
using System.Collections; | ||
|
@@ -1201,19 +1202,20 @@ public SwitchParameter AllMatches | |
/// The text encoding to process each file as. | ||
/// </summary> | ||
[Parameter] | ||
[ValidateNotNullOrEmpty] | ||
[ValidateSetAttribute(new string[] { | ||
[ArgumentToEncodingTransformationAttribute()] | ||
[ArgumentCompletions( | ||
EncodingConversion.Ascii, | ||
EncodingConversion.BigEndianUnicode, | ||
EncodingConversion.OEM, | ||
EncodingConversion.Unicode, | ||
EncodingConversion.Utf7, | ||
EncodingConversion.Utf8, | ||
EncodingConversion.Utf32, | ||
EncodingConversion.Ascii, | ||
EncodingConversion.BigEndianUnicode, | ||
EncodingConversion.Default, | ||
EncodingConversion.OEM })] | ||
public string Encoding { get; set; } | ||
|
||
private System.Text.Encoding _textEncoding; | ||
EncodingConversion.Utf8Bom, | ||
EncodingConversion.Utf8NoBom, | ||
EncodingConversion.Utf32 | ||
)] | ||
[ValidateNotNullOrEmpty] | ||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding(); | ||
|
||
/// <summary> | ||
/// The number of context lines to collect. If set to a | ||
|
@@ -1282,16 +1284,6 @@ public new int[] Context | |
/// </summary> | ||
protected override void BeginProcessing() | ||
{ | ||
// Process encoding switch. | ||
if (Encoding != null) | ||
{ | ||
_textEncoding = EncodingConversion.Convert(this, Encoding); | ||
} | ||
else | ||
{ | ||
_textEncoding = new System.Text.UTF8Encoding(); | ||
} | ||
|
||
if (!_simpleMatch) | ||
{ | ||
RegexOptions regexOptions = (_caseSensitive) ? RegexOptions.None : RegexOptions.IgnoreCase; | ||
|
@@ -1434,7 +1426,7 @@ private bool ProcessFile(string filename) | |
|
||
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
using (StreamReader sr = new StreamReader(fs, _textEncoding)) | ||
using (StreamReader sr = new StreamReader(fs, Encoding)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an ArgumentNullException path here if Encoding is null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed with attribute |
||
{ | ||
String line; | ||
int lineNo = 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to add
AttributeNotNullOrEmpty
attribute. Before this change, $null value would be rejected because the validate attribute will fail. After this change, user is able to pass in a null value.