-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-YamlQuotedText.ps1
More file actions
63 lines (62 loc) · 2.27 KB
/
Copy pathConvertTo-YamlQuotedText.ps1
File metadata and controls
63 lines (62 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function ConvertTo-YamlQuotedText {
<#
.SYNOPSIS
Encodes a string as a YAML double-quoted scalar.
#>
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory)]
[AllowEmptyString()]
[string] $Value
)
$builder = [System.Text.StringBuilder]::new()
[void] $builder.Append('"')
for ($index = 0; $index -lt $Value.Length; $index++) {
$character = $Value[$index]
$code = [int] $character
if ([char]::IsHighSurrogate($character)) {
if ($index + 1 -ge $Value.Length -or -not [char]::IsLowSurrogate($Value[$index + 1])) {
throw [System.ArgumentException]::new(
'A YAML string cannot contain an unpaired UTF-16 high surrogate.'
)
}
[void] $builder.Append($character)
[void] $builder.Append($Value[++$index])
} elseif ([char]::IsLowSurrogate($character)) {
throw [System.ArgumentException]::new(
'A YAML string cannot contain an unpaired UTF-16 low surrogate.'
)
} elseif ($code -eq 0) {
[void] $builder.Append('\0')
} elseif ($code -eq 7) {
[void] $builder.Append('\a')
} elseif ($code -eq 8) {
[void] $builder.Append('\b')
} elseif ($code -eq 9) {
[void] $builder.Append('\t')
} elseif ($code -eq 10) {
[void] $builder.Append('\n')
} elseif ($code -eq 11) {
[void] $builder.Append('\v')
} elseif ($code -eq 12) {
[void] $builder.Append('\f')
} elseif ($code -eq 13) {
[void] $builder.Append('\r')
} elseif ($code -eq 27) {
[void] $builder.Append('\e')
} elseif ($code -eq 34) {
[void] $builder.Append('\"')
} elseif ($code -eq 92) {
[void] $builder.Append('\\')
} elseif ($code -lt 0x20 -or $code -eq 0x7F -or $code -eq 0xFEFF -or
$code -eq 0xFFFE -or $code -eq 0xFFFF -or
($code -ge 0x80 -and $code -le 0x9F)) {
[void] $builder.Append(('\u{0:X4}' -f $code))
} else {
[void] $builder.Append($character)
}
}
[void] $builder.Append('"')
return $builder.ToString()
}