Files
PowerShell/src/System.Management.Automation/utils/EncodingUtils.cs
T
xtqqczze 05cab7fa5a Formatting: Add empty line between declarations (#12824)
# PR Summary

Automated fixes:
* RCS0013: Add empty line between single-line declarations of different kind
* RCS010: Add empty line between declarations

## PR Context


## PR Checklist

- [x] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
    - Use the present tense and imperative mood when describing your changes
- [x] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] This PR is ready to merge and is not [Work in Progress](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---work-in-progress).
    - If the PR is work in progress, please add the prefix `WIP:` or `[ WIP ]` to the beginning of the title (the `WIP` bot will keep its status check at `Pending` while the prefix is present) and remove the prefix when the PR is ready.
- **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)**
    - [x] None
    - **OR**
    - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Experimental_Features.md)
        - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here -->
- **User-facing changes**
    - [x] Not Applicable
    - **OR**
    - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
        - [ ] Issue filed: <!-- Number/link of that issue here -->
- **Testing - New and feature**
    - [x] N/A or can only be tested interactively
    - **OR**
    - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting)
- **Tooling**
    - [x] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted.
    - **OR**
    - [ ] I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include:
        - Impact on [PowerShell Editor Services](https://github.com/PowerShell/PowerShellEditorServices) which is used in the [PowerShell extension](https://github.com/PowerShell/vscode-powershell) for VSCode (which runs in a different PS Host).
        - Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features.
        - Impact on [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) (which provides linting & formatting in the editor extensions).
        - Impact on [EditorSyntax](https://github.com/PowerShell/EditorSyntax) (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
2020-05-29 04:56:33 +00:00

134 lines
5.2 KiB
C#

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Management.Automation.Internal;
namespace System.Management.Automation
{
internal static class EncodingConversion
{
internal const string Unknown = "unknown";
internal const string String = "string";
internal const string Unicode = "unicode";
internal const string BigEndianUnicode = "bigendianunicode";
internal const string BigEndianUtf32 = "bigendianutf32";
internal const string Ascii = "ascii";
internal const string Utf8 = "utf8";
internal const string Utf8NoBom = "utf8NoBOM";
internal const string Utf8Bom = "utf8BOM";
internal const string Utf7 = "utf7";
internal const string Utf32 = "utf32";
internal const string Default = "default";
internal const string OEM = "oem";
internal static readonly string[] TabCompletionResults = {
Ascii, BigEndianUnicode, BigEndianUtf32, OEM, Unicode, Utf7, Utf8, Utf8Bom, Utf8NoBom, Utf32
};
internal static Dictionary<string, Encoding> encodingMap = new Dictionary<string, Encoding>(StringComparer.OrdinalIgnoreCase)
{
{ Ascii, System.Text.Encoding.ASCII },
{ BigEndianUnicode, System.Text.Encoding.BigEndianUnicode },
{ BigEndianUtf32, new UTF32Encoding(bigEndian: true, byteOrderMark: true) },
{ Default, ClrFacade.GetDefaultEncoding() },
{ OEM, ClrFacade.GetOEMEncoding() },
{ Unicode, System.Text.Encoding.Unicode },
{ Utf7, System.Text.Encoding.UTF7 },
{ Utf8, ClrFacade.GetDefaultEncoding() },
{ Utf8Bom, System.Text.Encoding.UTF8 },
{ Utf8NoBom, ClrFacade.GetDefaultEncoding() },
{ Utf32, System.Text.Encoding.UTF32 },
{ String, System.Text.Encoding.Unicode },
{ Unknown, System.Text.Encoding.Unicode },
};
/// <summary>
/// Retrieve the encoding parameter from the command line
/// it throws if the encoding does not match the known ones.
/// </summary>
/// <returns>A System.Text.Encoding object (null if no encoding specified).</returns>
internal static Encoding Convert(Cmdlet cmdlet, string encoding)
{
if (string.IsNullOrEmpty(encoding))
{
// no parameter passed, default to UTF8
return ClrFacade.GetDefaultEncoding();
}
Encoding foundEncoding;
if (encodingMap.TryGetValue(encoding, out foundEncoding))
{
return foundEncoding;
}
// error condition: unknown encoding value
string validEncodingValues = string.Join(", ", TabCompletionResults);
string msg = StringUtil.Format(PathUtilsStrings.OutFile_WriteToFileEncodingUnknown, encoding, validEncodingValues);
ErrorRecord errorRecord = new ErrorRecord(
PSTraceSource.NewArgumentException("Encoding"),
"WriteToFileEncodingUnknown",
ErrorCategory.InvalidArgument,
null);
errorRecord.ErrorDetails = new ErrorDetails(msg);
cmdlet.ThrowTerminatingError(errorRecord);
return null;
}
}
/// <summary>
/// To make it easier to specify -Encoding parameter, we add an ArgumentTransformationAttribute here.
/// When the input data is of type string and is valid to be converted to System.Text.Encoding, we do
/// the conversion and return the converted value. Otherwise, we just return the input data.
/// </summary>
internal sealed class ArgumentToEncodingTransformationAttribute : ArgumentTransformationAttribute
{
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
{
switch (inputData)
{
case string stringName:
if (EncodingConversion.encodingMap.TryGetValue(stringName, out Encoding foundEncoding))
{
return foundEncoding;
}
else
{
return System.Text.Encoding.GetEncoding(stringName);
}
case int intName:
return System.Text.Encoding.GetEncoding(intName);
}
return inputData;
}
}
/// <summary>
/// Provides the set of Encoding values for tab completion of an Encoding parameter.
/// </summary>
internal sealed class ArgumentEncodingCompletionsAttribute : ArgumentCompletionsAttribute
{
public ArgumentEncodingCompletionsAttribute() : base(
EncodingConversion.Ascii,
EncodingConversion.BigEndianUnicode,
EncodingConversion.BigEndianUtf32,
EncodingConversion.OEM,
EncodingConversion.Unicode,
EncodingConversion.Utf7,
EncodingConversion.Utf8,
EncodingConversion.Utf8Bom,
EncodingConversion.Utf8NoBom,
EncodingConversion.Utf32
)
{ }
}
}