Skip to content

Do not turn \n and \t into escapes in quoted keys - #67

Open
bardrop wants to merge 1 commit into
SamboyCoding:masterfrom
bardrop-oss:fix-backslash-n-t-in-keys
Open

bardrop wants to merge 1 commit into
SamboyCoding:masterfrom
bardrop-oss:fix-backslash-n-t-in-keys

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown

What is wrong

The parser turns the two characters \n and \t into a real newline and tab in every key, after the key is read. Basic-string keys already have their escapes handled by the string reader, so this extra step is not needed there. For literal keys (single quotes) and for keys with an escaped backslash, it changes the key.

'C:\new' = 1
'C:\tools' = 2
"C:\\temp" = 3
var doc = new TomlParser().Parse(toml);
// actual keys:   "C:<newline>ew", "C:<tab>ools", "C:<tab>emp"
// expected keys: @"C:\new",       @"C:\tools",   @"C:\temp"

Literal strings have no escapes in TOML, so 'C:\new' must stay C:\new. Python tomllib and the toml-test decoders agree.

The fix

Remove the Replace("\\n", "\n").Replace("\\t", "\t") at the end of ReadKey. "\n" and "tab\there" keys still work, because ReadSingleLineBasicString already decodes them.

The test

BackslashesInQuotedKeysAreNotTurnedIntoEscapes in BasicKeyValueTests. It fails before the fix and passes after. The full suite passes: 165 passed, 0 failed. I also ran toml-test (TOML 1.0) before and after: same result both times (194/205 valid, 376/474 invalid), so nothing else changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant