diff --git a/src/go/GoModuleText.ts b/src/go/GoModuleText.ts index 6a62f75227d..8cd582ecaa3 100644 --- a/src/go/GoModuleText.ts +++ b/src/go/GoModuleText.ts @@ -14,6 +14,16 @@ const escapes = new Map([ ['"', 34], ]); +/** Index of the closing quote or line end of the double-quoted field at the start of `input`. */ +const quotedFieldEnd = (input: string): number => { + for (let index = 1; index < input.length; index += 1) { + const character = input[index]; + if (character === "\\") index += 1; + else if (character === '"' || character === "\n") return index; + } + return input.length; +}; + const quoted = (input: string): { value: string; end: number } | null => { if (input.startsWith("`")) { const end = input.indexOf("`", 1); @@ -22,7 +32,9 @@ const quoted = (input: string): { value: string; end: number } | null => { : { value: input.slice(1, end).replace(/\r/g, ""), end: end + 1 }; } if (!input.startsWith('"')) return null; - const output = Buffer.alloc(input.length * 3); + // Each source UTF-16 unit decodes to at most three bytes, so the field's own + // extent bounds the output; the rest of the line does not. + const output = Buffer.alloc(quotedFieldEnd(input) * 3); let length = 0; for (let index = 1; index < input.length;) { const character = input[index];