mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
fix(std/io): StringReader implementation (#6148)
This commit is contained in:
parent
78333f0ab3
commit
1a2f88609b
6 changed files with 37 additions and 39 deletions
|
@ -1,6 +1,6 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import { xeval } from "../xeval.ts";
|
||||
import { stringsReader } from "../../io/util.ts";
|
||||
import { StringReader } from "../../io/readers.ts";
|
||||
import { decode, encode } from "../../encoding/utf8.ts";
|
||||
import {
|
||||
assertEquals,
|
||||
|
@ -11,15 +11,19 @@ const { execPath, run } = Deno;
|
|||
|
||||
Deno.test("xevalSuccess", async function (): Promise<void> {
|
||||
const chunks: string[] = [];
|
||||
await xeval(stringsReader("a\nb\nc"), ($): number => chunks.push($));
|
||||
await xeval(new StringReader("a\nb\nc"), ($): number => chunks.push($));
|
||||
assertEquals(chunks, ["a", "b", "c"]);
|
||||
});
|
||||
|
||||
Deno.test("xevalDelimiter", async function (): Promise<void> {
|
||||
const chunks: string[] = [];
|
||||
await xeval(stringsReader("!MADMADAMADAM!"), ($): number => chunks.push($), {
|
||||
delimiter: "MADAM",
|
||||
});
|
||||
await xeval(
|
||||
new StringReader("!MADMADAMADAM!"),
|
||||
($): number => chunks.push($),
|
||||
{
|
||||
delimiter: "MADAM",
|
||||
}
|
||||
);
|
||||
assertEquals(chunks, ["!MAD", "ADAM!"]);
|
||||
});
|
||||
|
||||
|
|
|
@ -17,7 +17,8 @@ import {
|
|||
readLines,
|
||||
} from "./bufio.ts";
|
||||
import * as iotest from "./_iotest.ts";
|
||||
import { charCode, copyBytes, stringsReader } from "./util.ts";
|
||||
import { StringReader } from "./readers.ts";
|
||||
import { charCode, copyBytes } from "./util.ts";
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
|
@ -38,7 +39,7 @@ async function readBytes(buf: BufReader): Promise<string> {
|
|||
|
||||
Deno.test("bufioReaderSimple", async function (): Promise<void> {
|
||||
const data = "hello world";
|
||||
const b = new BufReader(stringsReader(data));
|
||||
const b = new BufReader(new StringReader(data));
|
||||
const s = await readBytes(b);
|
||||
assertEquals(s, data);
|
||||
});
|
||||
|
@ -119,7 +120,7 @@ Deno.test("bufioBufReader", async function (): Promise<void> {
|
|||
for (const readmaker of readMakers) {
|
||||
for (const bufreader of bufreaders) {
|
||||
for (const bufsize of bufsizes) {
|
||||
const read = readmaker.fn(stringsReader(text));
|
||||
const read = readmaker.fn(new StringReader(text));
|
||||
const buf = new BufReader(read, bufsize);
|
||||
const s = await bufreader.fn(buf);
|
||||
const debugStr =
|
||||
|
@ -136,7 +137,7 @@ Deno.test("bufioBufferFull", async function (): Promise<void> {
|
|||
const longString =
|
||||
"And now, hello, world! It is the time for all good men to come to the" +
|
||||
" aid of their party";
|
||||
const buf = new BufReader(stringsReader(longString), MIN_READ_BUFFER_SIZE);
|
||||
const buf = new BufReader(new StringReader(longString), MIN_READ_BUFFER_SIZE);
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
try {
|
||||
|
@ -156,7 +157,7 @@ Deno.test("bufioBufferFull", async function (): Promise<void> {
|
|||
|
||||
Deno.test("bufioReadString", async function (): Promise<void> {
|
||||
const string = "And now, hello world!";
|
||||
const buf = new BufReader(stringsReader(string), MIN_READ_BUFFER_SIZE);
|
||||
const buf = new BufReader(new StringReader(string), MIN_READ_BUFFER_SIZE);
|
||||
|
||||
const line = await buf.readString(",");
|
||||
assert(line !== null);
|
||||
|
@ -248,7 +249,7 @@ Deno.test("bufioPeek", async function (): Promise<void> {
|
|||
const p = new Uint8Array(10);
|
||||
// string is 16 (minReadBufferSize) long.
|
||||
const buf = new BufReader(
|
||||
stringsReader("abcdefghijklmnop"),
|
||||
new StringReader("abcdefghijklmnop"),
|
||||
MIN_READ_BUFFER_SIZE
|
||||
);
|
||||
|
||||
|
@ -453,7 +454,7 @@ Deno.test(
|
|||
const decoder = new TextDecoder();
|
||||
const data = "abcdefghijklmnopqrstuvwxyz";
|
||||
const bufSize = 25;
|
||||
const b = new BufReader(stringsReader(data), bufSize);
|
||||
const b = new BufReader(new StringReader(data), bufSize);
|
||||
|
||||
const r1 = (await b.readLine()) as ReadLineResult;
|
||||
assert(r1 !== null);
|
||||
|
|
|
@ -9,8 +9,9 @@ import {
|
|||
readShort,
|
||||
sliceLongToBytes,
|
||||
} from "./ioutil.ts";
|
||||
import { StringReader } from "./readers.ts";
|
||||
import { BufReader } from "./bufio.ts";
|
||||
import { stringsReader, tempFile } from "./util.ts";
|
||||
import { tempFile } from "./util.ts";
|
||||
import * as path from "../path/mod.ts";
|
||||
|
||||
class BinaryReader implements Reader {
|
||||
|
@ -73,7 +74,7 @@ Deno.test("testSliceLongToBytes2", function (): void {
|
|||
|
||||
Deno.test("testCopyN1", async function (): Promise<void> {
|
||||
const w = new Buffer();
|
||||
const r = stringsReader("abcdefghij");
|
||||
const r = new StringReader("abcdefghij");
|
||||
const n = await copyN(r, w, 3);
|
||||
assertEquals(n, 3);
|
||||
assertEquals(new TextDecoder().decode(w.bytes()), "abc");
|
||||
|
@ -81,7 +82,7 @@ Deno.test("testCopyN1", async function (): Promise<void> {
|
|||
|
||||
Deno.test("testCopyN2", async function (): Promise<void> {
|
||||
const w = new Buffer();
|
||||
const r = stringsReader("abcdefghij");
|
||||
const r = new StringReader("abcdefghij");
|
||||
const n = await copyN(r, w, 11);
|
||||
assertEquals(n, 10);
|
||||
assertEquals(new TextDecoder().decode(w.bytes()), "abcdefghij");
|
||||
|
@ -91,10 +92,17 @@ Deno.test("copyNWriteAllData", async function (): Promise<void> {
|
|||
const { filepath, file } = await tempFile(path.resolve("io"));
|
||||
const size = 16 * 1024 + 1;
|
||||
const data = "a".repeat(32 * 1024);
|
||||
const r = stringsReader(data);
|
||||
const r = new StringReader(data);
|
||||
const n = await copyN(r, file, size); // Over max file possible buffer
|
||||
file.close();
|
||||
await Deno.remove(filepath);
|
||||
|
||||
assertEquals(n, size);
|
||||
});
|
||||
|
||||
Deno.test("testStringReaderEof", async function (): Promise<void> {
|
||||
const r = new StringReader("abc");
|
||||
assertEquals(await r.read(new Uint8Array()), 0);
|
||||
assertEquals(await r.read(new Uint8Array(4)), 3);
|
||||
assertEquals(await r.read(new Uint8Array(1)), null);
|
||||
});
|
||||
|
|
|
@ -7,22 +7,12 @@
|
|||
|
||||
type Reader = Deno.Reader;
|
||||
import { encode } from "../encoding/utf8.ts";
|
||||
const { Buffer } = Deno;
|
||||
|
||||
/** Reader utility for strings */
|
||||
export class StringReader implements Reader {
|
||||
private offs = 0;
|
||||
private buf = new Uint8Array(encode(this.s));
|
||||
|
||||
constructor(private readonly s: string) {}
|
||||
|
||||
read(p: Uint8Array): Promise<number | null> {
|
||||
const n = Math.min(p.byteLength, this.buf.byteLength - this.offs);
|
||||
p.set(this.buf.slice(this.offs, this.offs + n));
|
||||
this.offs += n;
|
||||
if (n === 0) {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
return Promise.resolve(n);
|
||||
export class StringReader extends Buffer {
|
||||
constructor(private readonly s: string) {
|
||||
super(encode(s).buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
const { Buffer, mkdir, open } = Deno;
|
||||
const { mkdir, open } = Deno;
|
||||
type File = Deno.File;
|
||||
type Reader = Deno.Reader;
|
||||
import * as path from "../path/mod.ts";
|
||||
import { encode } from "../encoding/utf8.ts";
|
||||
|
||||
/**
|
||||
* Copy bytes from one Uint8Array to another. Bytes from `src` which don't fit
|
||||
|
@ -28,10 +27,6 @@ export function charCode(s: string): number {
|
|||
return s.charCodeAt(0);
|
||||
}
|
||||
|
||||
export function stringsReader(s: string): Reader {
|
||||
return new Buffer(encode(s).buffer);
|
||||
}
|
||||
|
||||
/** Create or open a temporal file at specified directory with prefix and
|
||||
* postfix
|
||||
* */
|
||||
|
|
|
@ -5,12 +5,12 @@
|
|||
|
||||
import { BufReader } from "../io/bufio.ts";
|
||||
import { TextProtoReader } from "./mod.ts";
|
||||
import { stringsReader } from "../io/util.ts";
|
||||
import { StringReader } from "../io/readers.ts";
|
||||
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
|
||||
const { test } = Deno;
|
||||
|
||||
function reader(s: string): TextProtoReader {
|
||||
return new TextProtoReader(new BufReader(stringsReader(s)));
|
||||
return new TextProtoReader(new BufReader(new StringReader(s)));
|
||||
}
|
||||
|
||||
test({
|
||||
|
@ -187,7 +187,7 @@ test({
|
|||
const input = "abcdefghijklmnopqrstuvwxyz";
|
||||
const bufSize = 25;
|
||||
const tp = new TextProtoReader(
|
||||
new BufReader(stringsReader(input), bufSize)
|
||||
new BufReader(new StringReader(input), bufSize)
|
||||
);
|
||||
const line = await tp.readLine();
|
||||
assertEquals(line, input);
|
||||
|
|
Loading…
Add table
Reference in a new issue