0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

std: fix BufReader.readString to actually return Deno.EOF at end (#3191)

This commit is contained in:
Robert Jack Will 2019-10-28 18:28:29 +01:00 committed by Bert Belder
parent b273989446
commit ff9df0c321
2 changed files with 9 additions and 4 deletions

View file

@ -207,15 +207,15 @@ export class BufReader implements Reader {
* it returns the data read before the error and the error itself * it returns the data read before the error and the error itself
* (often io.EOF). * (often io.EOF).
* ReadString returns err != nil if and only if the returned data does not end * ReadString returns err != nil if and only if the returned data does not end
* in * in delim.
* delim.
* For simple uses, a Scanner may be more convenient. * For simple uses, a Scanner may be more convenient.
*/ */
async readString(delim: string): Promise<string | Deno.EOF> { async readString(delim: string): Promise<string | Deno.EOF> {
if (delim.length !== 1) if (delim.length !== 1)
throw new Error("Delimiter should be a single character"); throw new Error("Delimiter should be a single character");
const buffer = await this.readSlice(delim.charCodeAt(0)); 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 /** `readLine()` is a low-level line-reading primitive. Most callers should

View file

@ -161,13 +161,18 @@ test(async function bufioBufferFull(): Promise<void> {
}); });
test(async function bufioReadString(): 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 buf = new BufReader(stringsReader(string), MIN_READ_BUFFER_SIZE);
const line = assertNotEOF(await buf.readString(",")); const line = assertNotEOF(await buf.readString(","));
assertEquals(line, "And now,"); assertEquals(line, "And now,");
assertEquals(line.length, 8); assertEquals(line.length, 8);
const line2 = assertNotEOF(await buf.readString(","));
assertEquals(line2, " hello world!");
assertEquals(await buf.readString(","), Deno.EOF);
try { try {
await buf.readString("deno"); await buf.readString("deno");