perf(stream): append text deltas as JSON without an intermediate Marshal

This commit is contained in:
2026-09-29 12:26:15 +01:00
parent baabd5d3bb
commit 2cd686a669
2 changed files with 77 additions and 6 deletions
+17
View File
@@ -1150,3 +1150,20 @@ func TestContentChunkerMatchesChunk(t *testing.T) {
}
}
}
func TestAppendJSONStringMatchesMarshal(t *testing.T) {
var cases []string
for r := rune(0); r < 0x250; r++ {
cases = append(cases, string(r), "a"+string(r)+"b")
}
cases = append(cases, "\u2028", "\u2029", "x\u2028y\u2029z", "bad\xffutf8", "\xc3", "\xe2\x80", "\U0001F600 emoji", "", "<script>alert('&')</script>")
for _, s := range cases {
want, err := json.Marshal(s)
if err != nil {
t.Fatalf("Marshal(%q): %v", s, err)
}
if got := appendJSONString(nil, s); !bytes.Equal(got, want) {
t.Errorf("appendJSONString(%q) = %s, want %s", s, got, want)
}
}
}