mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
BREAKING(io): remove StringWriter
(#6061)
This commit is contained in:
parent
45cef56ce9
commit
87a5b9e2ec
@ -9,7 +9,6 @@
|
||||
"./read-all": "./read_all.ts",
|
||||
"./reader-from-stream-reader": "./reader_from_stream_reader.ts",
|
||||
"./string-reader": "./string_reader.ts",
|
||||
"./string-writer": "./string_writer.ts",
|
||||
"./to-readable-stream": "./to_readable_stream.ts",
|
||||
"./to-writable-stream": "./to_writable_stream.ts",
|
||||
"./types": "./types.ts",
|
||||
|
@ -22,7 +22,6 @@ export * from "./iterate_reader.ts";
|
||||
export * from "./read_all.ts";
|
||||
export * from "./reader_from_stream_reader.ts";
|
||||
export * from "./string_reader.ts";
|
||||
export * from "./string_writer.ts";
|
||||
export * from "./to_readable_stream.ts";
|
||||
export * from "./to_writable_stream.ts";
|
||||
export * from "./types.ts";
|
||||
|
@ -1,120 +0,0 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
// This module is browser compatible.
|
||||
|
||||
import type { Writer, WriterSync } from "./types.ts";
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
/**
|
||||
* Writer utility for buffering string chunks.
|
||||
*
|
||||
* @example Usage
|
||||
* ```ts
|
||||
* import {
|
||||
* copy,
|
||||
* StringReader,
|
||||
* StringWriter,
|
||||
* } from "@std/io";
|
||||
* import { assertEquals } from "@std/assert/equals";
|
||||
*
|
||||
* const w = new StringWriter("base");
|
||||
* const r = new StringReader("0123456789");
|
||||
*
|
||||
* await copy(r, w); // copy all
|
||||
* assertEquals(w.toString(), "base0123456789");
|
||||
* ```
|
||||
*
|
||||
* @deprecated Write to a
|
||||
* {@linkcode https://jsr.io/@std/streams/doc/buffer/~/Buffer | Buffer}'s
|
||||
* `writable` property instead. This will be removed in 0.225.0.
|
||||
*/
|
||||
export class StringWriter implements Writer, WriterSync {
|
||||
#chunks: Uint8Array[] = [];
|
||||
#byteLength = 0;
|
||||
#cache: string | undefined;
|
||||
#base: string;
|
||||
|
||||
/**
|
||||
* Construct a new instance.
|
||||
*
|
||||
* @param base The base string to write to the buffer.
|
||||
*/
|
||||
constructor(base = "") {
|
||||
const c = new TextEncoder().encode(base);
|
||||
this.#chunks.push(c);
|
||||
this.#byteLength += c.byteLength;
|
||||
this.#base = base;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the bytes to the buffer asynchronously.
|
||||
*
|
||||
* @example Usage
|
||||
* ```ts
|
||||
* import { StringWriter } from "@std/io/string-writer";
|
||||
* import { assertEquals } from "@std/assert/equals";
|
||||
*
|
||||
* const w = new StringWriter("base");
|
||||
* await w.write(new TextEncoder().encode("0123"));
|
||||
* assertEquals(w.toString(), "base0123");
|
||||
* ```
|
||||
*
|
||||
* @param p The bytes to write to the buffer.
|
||||
* @returns The number of bytes written to the buffer in total.
|
||||
*/
|
||||
write(p: Uint8Array): Promise<number> {
|
||||
return Promise.resolve(this.writeSync(p));
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the bytes to the buffer synchronously.
|
||||
*
|
||||
* @example Usage
|
||||
* ```ts
|
||||
* import { StringWriter } from "@std/io/string-writer";
|
||||
* import { assertEquals } from "@std/assert/equals";
|
||||
*
|
||||
* const w = new StringWriter("base");
|
||||
* w.writeSync(new TextEncoder().encode("0123"));
|
||||
* assertEquals(w.toString(), "base0123");
|
||||
* ```
|
||||
*
|
||||
* @param p The bytes to write to the buffer.
|
||||
* @returns The number of bytes written to the buffer in total.
|
||||
*/
|
||||
writeSync(p: Uint8Array): number {
|
||||
this.#chunks.push(new Uint8Array(p));
|
||||
this.#byteLength += p.byteLength;
|
||||
this.#cache = undefined;
|
||||
return p.byteLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string written to the buffer.
|
||||
*
|
||||
* @example Usage
|
||||
* ```ts
|
||||
* import { StringWriter } from "@std/io/string-writer";
|
||||
* import { assertEquals } from "@std/assert/equals";
|
||||
*
|
||||
* const w = new StringWriter("base");
|
||||
* await w.write(new TextEncoder().encode("0123"));
|
||||
* assertEquals(w.toString(), "base0123");
|
||||
* ```
|
||||
*
|
||||
* @returns the string written to the buffer.
|
||||
*/
|
||||
toString(): string {
|
||||
if (this.#cache) {
|
||||
return this.#cache;
|
||||
}
|
||||
const buf = new Uint8Array(this.#byteLength);
|
||||
let offs = 0;
|
||||
for (const chunk of this.#chunks) {
|
||||
buf.set(chunk, offs);
|
||||
offs += chunk.byteLength;
|
||||
}
|
||||
this.#cache = decoder.decode(buf);
|
||||
return this.#cache;
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { StringWriter } from "./string_writer.ts";
|
||||
import { StringReader } from "./string_reader.ts";
|
||||
import { copy } from "./copy.ts";
|
||||
|
||||
Deno.test("ioStringWriter", async function () {
|
||||
const w = new StringWriter("base");
|
||||
const r = new StringReader("0123456789");
|
||||
await copy(r, w);
|
||||
assertEquals(w.toString(), "base0123456789");
|
||||
});
|
||||
|
||||
Deno.test("ioStringWriterSync", function () {
|
||||
const encoder = new TextEncoder();
|
||||
const w = new StringWriter("");
|
||||
w.writeSync(encoder.encode("deno"));
|
||||
assertEquals(w.toString(), "deno");
|
||||
w.writeSync(encoder.encode("\nland"));
|
||||
assertEquals(w.toString(), "deno\nland");
|
||||
});
|
||||
|
||||
Deno.test("ioStringWriterIsolationTest", async function () {
|
||||
const encoder = new TextEncoder();
|
||||
const src = "ABC";
|
||||
const srcChunks = src.split("").map((c) => encoder.encode(c));
|
||||
|
||||
const w = new StringWriter();
|
||||
for (const c of srcChunks) {
|
||||
const written = await w.write(c);
|
||||
assertEquals(written, 1);
|
||||
}
|
||||
assertEquals(w.toString(), src);
|
||||
});
|
Loading…
Reference in New Issue
Block a user