From ff9df0c321eb56c7f89f5ccdaa301453f22f708e Mon Sep 17 00:00:00 2001 From: Robert Jack Will Date: Mon, 28 Oct 2019 18:28:29 +0100 Subject: [PATCH] std: fix BufReader.readString to actually return Deno.EOF at end (#3191) --- std/io/bufio.ts | 6 +++--- std/io/bufio_test.ts | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/std/io/bufio.ts b/std/io/bufio.ts index 213870c3c4..b287ef3c1c 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 75664694ac..780dfd3db5 100644 --- a/std/io/bufio_test.ts +++ b/std/io/bufio_test.ts @@ -161,13 +161,18 @@ test(async function bufioBufferFull(): Promise { }); test(async function bufioReadString(): Promise { - const string = "And now, hello, world!"; + const string = "And now, hello world!"; const buf = new BufReader(stringsReader(string), MIN_READ_BUFFER_SIZE); const line = assertNotEOF(await buf.readString(",")); assertEquals(line, "And now,"); assertEquals(line.length, 8); + const line2 = assertNotEOF(await buf.readString(",")); + assertEquals(line2, " hello world!"); + + assertEquals(await buf.readString(","), Deno.EOF); + try { await buf.readString("deno");