mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
std: fix BufReader.readString to actually return Deno.EOF at end (#3191)
This commit is contained in:
parent
b273989446
commit
ff9df0c321
2 changed files with 9 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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");
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue