summaryrefslogtreecommitdiff
path: root/std/io
diff options
context:
space:
mode:
authorRobert Jack Will <matey-jack@users.noreply.github.com>2019-10-28 18:28:29 +0100
committerBert Belder <bertbelder@gmail.com>2019-10-28 10:28:29 -0700
commitff9df0c321eb56c7f89f5ccdaa301453f22f708e (patch)
tree1e7c3363ebb61a45583ffe6b27bdd3de53c7fc65 /std/io
parentb273989446d62808f922280247604431d1d219cb (diff)
std: fix BufReader.readString to actually return Deno.EOF at end (#3191)
Diffstat (limited to 'std/io')
-rw-r--r--std/io/bufio.ts6
-rw-r--r--std/io/bufio_test.ts7
2 files changed, 9 insertions, 4 deletions
diff --git a/std/io/bufio.ts b/std/io/bufio.ts
index 213870c3c..b287ef3c1 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<string | Deno.EOF> {
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 75664694a..780dfd3db 100644
--- a/std/io/bufio_test.ts
+++ b/std/io/bufio_test.ts
@@ -161,13 +161,18 @@ test(async function bufioBufferFull(): Promise<void> {
});
test(async function bufioReadString(): Promise<void> {
- 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");