diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-06-06 10:37:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-06 10:37:52 -0400 |
commit | 1a2f88609b3e1bc27790c77331ac96b423625eb6 (patch) | |
tree | d950e2cb4aaafc459b16d4f9333b2c29d7adbcc4 | |
parent | 78333f0ab3614e8dbd3dfdc95e9dbb53f80ffe5d (diff) |
fix(std/io): StringReader implementation (#6148)
-rw-r--r-- | std/examples/tests/xeval_test.ts | 14 | ||||
-rw-r--r-- | std/io/bufio_test.ts | 15 | ||||
-rw-r--r-- | std/io/ioutil_test.ts | 16 | ||||
-rw-r--r-- | std/io/readers.ts | 18 | ||||
-rw-r--r-- | std/io/util.ts | 7 | ||||
-rw-r--r-- | std/textproto/test.ts | 6 |
6 files changed, 37 insertions, 39 deletions
diff --git a/std/examples/tests/xeval_test.ts b/std/examples/tests/xeval_test.ts index 3db1459a9..f86e27866 100644 --- a/std/examples/tests/xeval_test.ts +++ b/std/examples/tests/xeval_test.ts @@ -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!"]); }); diff --git a/std/io/bufio_test.ts b/std/io/bufio_test.ts index 671ed2115..b03640d52 100644 --- a/std/io/bufio_test.ts +++ b/std/io/bufio_test.ts @@ -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); diff --git a/std/io/ioutil_test.ts b/std/io/ioutil_test.ts index cf8eaf437..977c7022c 100644 --- a/std/io/ioutil_test.ts +++ b/std/io/ioutil_test.ts @@ -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); +}); diff --git a/std/io/readers.ts b/std/io/readers.ts index 201b87cd8..d43655263 100644 --- a/std/io/readers.ts +++ b/std/io/readers.ts @@ -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); } } diff --git a/std/io/util.ts b/std/io/util.ts index 237747a89..47e48a981 100644 --- a/std/io/util.ts +++ b/std/io/util.ts @@ -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 * */ diff --git a/std/textproto/test.ts b/std/textproto/test.ts index ec66bfd8c..7539e9779 100644 --- a/std/textproto/test.ts +++ b/std/textproto/test.ts @@ -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); |