Skip to content

Commit

Permalink
std: fix BufReader.readString to actually return Deno.EOF at end (#3191)
Browse files Browse the repository at this point in the history
  • Loading branch information
matey-jack authored and piscisaureus committed Oct 28, 2019
1 parent b273989 commit ff9df0c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions std/io/bufio.ts
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion std/io/bufio_test.ts
Expand Up @@ -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");

Expand Down

0 comments on commit ff9df0c

Please sign in to comment.