Skip to content

Commit 29efafc

Browse files
Add strict YAML file imports
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 0c2fcc1 commit 29efafc

3 files changed

Lines changed: 913 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function Get-YamlTextEncoding {
2+
<#
3+
.SYNOPSIS
4+
Creates a strict text encoding for YAML file input or output.
5+
6+
.DESCRIPTION
7+
Returns an encoding that throws when bytes or characters cannot be
8+
represented. The encoding preamble matches the public encoding name.
9+
10+
.PARAMETER Name
11+
The public YAML file encoding name.
12+
13+
.EXAMPLE
14+
Get-YamlTextEncoding -Name utf8
15+
16+
Returns strict UTF-8 without a byte order mark.
17+
18+
.INPUTS
19+
None.
20+
21+
.OUTPUTS
22+
System.Text.Encoding
23+
#>
24+
[OutputType(
25+
[System.Text.UTF8Encoding],
26+
[System.Text.UnicodeEncoding],
27+
[System.Text.UTF32Encoding]
28+
)]
29+
[CmdletBinding()]
30+
param (
31+
# Selects the strict decoder and its output preamble policy.
32+
[Parameter(Mandatory)]
33+
[ValidateSet('utf8', 'utf8BOM', 'utf16LE', 'utf16BE', 'utf32LE', 'utf32BE')]
34+
[string] $Name
35+
)
36+
37+
switch ($Name) {
38+
'utf8' {
39+
[System.Text.UTF8Encoding]::new($false, $true)
40+
}
41+
'utf8BOM' {
42+
[System.Text.UTF8Encoding]::new($true, $true)
43+
}
44+
'utf16LE' {
45+
[System.Text.UnicodeEncoding]::new($false, $true, $true)
46+
}
47+
'utf16BE' {
48+
[System.Text.UnicodeEncoding]::new($true, $true, $true)
49+
}
50+
'utf32LE' {
51+
[System.Text.UTF32Encoding]::new($false, $true, $true)
52+
}
53+
'utf32BE' {
54+
[System.Text.UTF32Encoding]::new($true, $true, $true)
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)