Skip to content

Commit

Permalink
std: fix BufReader.readString to actually return EOF at end
Browse files Browse the repository at this point in the history
  • Loading branch information
matey-jack committed Oct 23, 2019
1 parent 4bebbda commit b332394
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 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
6 changes: 6 additions & 0 deletions std/io/bufio_test.ts
Expand Up @@ -168,6 +168,12 @@ test(async function bufioReadString(): Promise<void> {
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");

Expand Down

0 comments on commit b332394

Please sign in to comment.