perf(parse): cut allocations and validate UTF-8 in the scan

This commit is contained in:
2026-09-20 22:15:10 +02:00
parent adf189aa2c
commit b4d564c682
6 changed files with 427 additions and 117 deletions
+31 -12
View File
@@ -24,19 +24,38 @@ func TestSyntaxErrorMessage(t *testing.T) {
}
func TestParseRejectsInvalidUTF8(t *testing.T) {
_, err := ParseMap([]byte("v = \"\xff\"\n"))
if err == nil {
t.Fatal("expected a UTF-8 validation error")
// The scan validates UTF-8 where it meets the byte, so the reported line
// is the invalid byte's own, wherever in the document it sits.
cases := []struct {
name string
doc string
line int
}{
{"in a basic string", "v = \"\xff\"\n", 1},
{"in a literal string", "v = '\xff'\n", 1},
{"in a multiline string", "v = \"\"\"\n\xff\"\"\"\n", 2},
{"in a comment", "v = 1\n# caf\xe9\xff\n", 2},
{"in a bare key", "va\xfflue = 1\n", 1},
{"as a statement", "\xff = 1\n", 1},
{"in a bare value", "v = \xff1\n", 1},
{"after a value", "v = 1 \xff\n", 1},
{"after the first line", "a = 1\nb = \"\xff\"\n", 2},
}
se, ok := err.(*SyntaxError)
if !ok {
t.Fatalf("err is %T, want *SyntaxError", err)
}
if !strings.Contains(se.Msg, "UTF-8") {
t.Errorf("Msg = %q, want it to mention UTF-8", se.Msg)
}
if se.Line != 1 {
t.Errorf("Line = %d, want 1", se.Line)
for _, c := range cases {
_, err := ParseMap([]byte(c.doc))
if err == nil {
t.Fatalf("%s: expected a UTF-8 validation error", c.name)
}
se, ok := err.(*SyntaxError)
if !ok {
t.Fatalf("%s: err is %T, want *SyntaxError", c.name, err)
}
if !strings.Contains(se.Msg, "UTF-8") {
t.Errorf("%s: Msg = %q, want it to mention UTF-8", c.name, se.Msg)
}
if se.Line != c.line {
t.Errorf("%s: Line = %d, want %d", c.name, se.Line, c.line)
}
}
}