diff --git a/std/io/bufio.ts b/std/io/bufio.ts index 213870c3c4f15..b287ef3c1c234 100644 --- a/std/io/bufio.ts +++ b/std/io/bufio.ts @@ -207,15 +207,15 @@ export class BufReader implements Reader { * it returns the data read before the error and the error itself * (often io.EOF). * ReadString returns err != nil if and only if the returned data does not end - * in - * delim. + * in delim. * For simple uses, a Scanner may be more convenient. */ async readString(delim: string): Promise { if (delim.length !== 1) throw new Error("Delimiter should be a single character"); const buffer = await this.readSlice(delim.charCodeAt(0)); - return new TextDecoder().decode(buffer || undefined); + if (buffer == Deno.EOF) return Deno.EOF; + return new TextDecoder().decode(buffer); } /** `readLine()` is a low-level line-reading primitive. Most callers should diff --git a/std/io/bufio_test.ts b/std/io/bufio_test.ts index 75664694ac25c..1a89277977eb4 100644 --- a/std/io/bufio_test.ts +++ b/std/io/bufio_test.ts @@ -168,6 +168,12 @@ test(async function bufioReadString(): Promise { assertEquals(line, "And now,"); assertEquals(line.length, 8); + const line2 = assertNotEOF(await buf.readString(",")); + const line3 = assertNotEOF(await buf.readString(",")); + assertEquals(line3, " world!"); + + assertEquals(await buf.readString(","), Deno.EOF); + try { await buf.readString("deno");