mirror of
https://github.com/denoland/deno.git
synced 2024-12-28 01:59:06 -05:00
parent
f545f1d571
commit
9739ba55df
2 changed files with 22 additions and 2 deletions
|
@ -211,8 +211,11 @@ export class BufReader implements Reader {
|
|||
* delim.
|
||||
* For simple uses, a Scanner may be more convenient.
|
||||
*/
|
||||
async readString(_delim: string): Promise<string | Deno.EOF> {
|
||||
throw new Error("Not implemented");
|
||||
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);
|
||||
}
|
||||
|
||||
/** `readLine()` is a low-level line-reading primitive. Most callers should
|
||||
|
|
|
@ -160,6 +160,23 @@ test(async function bufioBufferFull(): Promise<void> {
|
|||
assertEquals(actual, "world!");
|
||||
});
|
||||
|
||||
test(async function bufioReadString(): Promise<void> {
|
||||
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);
|
||||
|
||||
try {
|
||||
await buf.readString("deno");
|
||||
|
||||
fail("should throw");
|
||||
} catch (err) {
|
||||
assert(err.message, "Delimiter should be a single character");
|
||||
}
|
||||
});
|
||||
|
||||
const testInput = encoder.encode(
|
||||
"012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy"
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue