Skip to content

Commit 73aa66f

Browse files
Add \xHH hex escape parsing, fix PSAvoidLongLines, update help text and README escape lists
1 parent ba4fee0 commit 73aa66f

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Practical implications of the core schema:
4747
- Integers and floats parse to their native types using invariant culture.
4848
- Anything else is a string. Quoted strings (`'...'`, `"..."`) always preserve the string type.
4949

50-
The supported YAML subset covers block-style mappings, block-style sequences, nested structures, single- and double-quoted scalars (with `\n`, `\t`, `\r`, `\\`, `\"` escapes in double quotes), document start (`---`) and end (`...`) markers, and full-line / inline `#` comments. Flow style (`[a, b]`, `{a: 1}`), block scalars (`|`, `>`), anchors, aliases, tags, multi-document streams, and `!!timestamp` are not yet supported.
50+
The supported YAML subset covers block-style mappings, block-style sequences, nested structures, single- and double-quoted scalars (with `\n`, `\t`, `\r`, `\0`, `\\`, `\"`, `\xHH` escapes in double quotes), document start (`---`) and end (`...`) markers, and full-line / inline `#` comments. Flow style (`[a, b]`, `{a: 1}`), block scalars (`|`, `>`), anchors, aliases, tags, multi-document streams, and `!!timestamp` are not yet supported.
5151

5252
### Example 1: Parse a YAML string
5353

src/functions/private/Expand-YamlDoubleQuoted.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
'"' { $null = $sb.Append('"') }
2626
'\' { $null = $sb.Append('\') }
2727
'0' { $null = $sb.Append([char]0) }
28+
'x' {
29+
if ($i + 3 -lt $Text.Length) {
30+
$hex = $Text.Substring($i + 2, 2)
31+
$code = 0
32+
if ([int]::TryParse($hex, [System.Globalization.NumberStyles]::HexNumber, $null, [ref]$code)) {
33+
$null = $sb.Append([char]$code)
34+
$i += 4
35+
continue
36+
}
37+
}
38+
$expanded = $false
39+
}
2840
default { $expanded = $false }
2941
}
3042

src/functions/public/ConvertFrom-Yaml.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- Block-style sequences (- item)
1313
- Nested structures
1414
- Scalars: strings, integers, floats, booleans (`true`/`false`), null (`null`/`~`/empty)
15-
- Single- and double-quoted strings (with `\n`, `\t`, `\r`, `\\`, `\"` in double quotes)
15+
- Single- and double-quoted strings (with `\n`, `\t`, `\r`, `\0`, `\\`, `\"`, `\xHH` in double quotes)
1616
- Document start (`---`) and end (`...`) markers are tolerated
1717
- Full-line comments (`#`) and inline comments after values
1818
@@ -108,7 +108,9 @@
108108

109109
if ($context.Index -lt $lines.Count) {
110110
$leftover = $lines[$context.Index]
111-
throw "ConvertFrom-Yaml: unexpected content at line $($leftover.Number): '$($leftover.Content)'. The document has trailing content that was not consumed by the parser."
111+
$msg = 'ConvertFrom-Yaml: unexpected content at line {0}: ''{1}''.'
112+
$msg += ' The document has trailing content that was not consumed.'
113+
throw ($msg -f $leftover.Number, $leftover.Content)
112114
}
113115

114116
if ($NoEnumerate -and $result -is [System.Collections.IList]) {

tests/ConvertTo-Yaml.Tests.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,4 +584,15 @@ Describe 'Round-trip ConvertTo-Yaml | ConvertFrom-Yaml' {
584584
$result['value'] | Should -Be ''
585585
$result['value'] | Should -BeOfType [string]
586586
}
587+
588+
It 'Round-trips control characters via \xHH escapes' {
589+
# Format-YamlDoubleQuoted emits \xHH for control chars (e.g. NUL, BEL).
590+
# Expand-YamlDoubleQuoted must parse them back correctly.
591+
$nul = [string][char]0
592+
$bel = [string][char]7
593+
$obj = [ordered]@{ nul = $nul; bel = $bel }
594+
$result = $obj | ConvertTo-Yaml | ConvertFrom-Yaml -AsHashtable
595+
$result['nul'] | Should -Be $nul
596+
$result['bel'] | Should -Be $bel
597+
}
587598
}

0 commit comments

Comments
 (0)