1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00

internal: reorder Buffer's copyBytes arguments (#5021)

This commit is contained in:
Marcos Casagrande 2020-04-30 21:46:44 +02:00 committed by GitHub
parent 3e72d63205
commit 4297b865f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,7 +17,7 @@ const MAX_SIZE = 2 ** 32 - 2;
// `off` is the offset into `dst` where it will at which to begin writing values // `off` is the offset into `dst` where it will at which to begin writing values
// from `src`. // from `src`.
// Returns the number of bytes copied. // Returns the number of bytes copied.
function copyBytes(dst: Uint8Array, src: Uint8Array, off = 0): number { function copyBytes(src: Uint8Array, dst: Uint8Array, off = 0): number {
const r = dst.byteLength - off; const r = dst.byteLength - off;
if (src.byteLength > r) { if (src.byteLength > r) {
src = src.subarray(0, r); src = src.subarray(0, r);
@ -95,7 +95,7 @@ export class Buffer implements Reader, ReaderSync, Writer, WriterSync {
} }
return null; return null;
} }
const nread = copyBytes(p, this.#buf.subarray(this.#off)); const nread = copyBytes(this.#buf.subarray(this.#off), p);
this.#off += nread; this.#off += nread;
return nread; return nread;
} }
@ -107,7 +107,7 @@ export class Buffer implements Reader, ReaderSync, Writer, WriterSync {
writeSync(p: Uint8Array): number { writeSync(p: Uint8Array): number {
const m = this.#grow(p.byteLength); const m = this.#grow(p.byteLength);
return copyBytes(this.#buf, p, m); return copyBytes(p, this.#buf, m);
} }
write(p: Uint8Array): Promise<number> { write(p: Uint8Array): Promise<number> {
@ -132,13 +132,13 @@ export class Buffer implements Reader, ReaderSync, Writer, WriterSync {
// ArrayBuffer. We only need m+n <= c to slide, but // ArrayBuffer. We only need m+n <= c to slide, but
// we instead let capacity get twice as large so we // we instead let capacity get twice as large so we
// don't spend all our time copying. // don't spend all our time copying.
copyBytes(this.#buf, this.#buf.subarray(this.#off)); copyBytes(this.#buf.subarray(this.#off), this.#buf);
} else if (c > MAX_SIZE - c - n) { } else if (c > MAX_SIZE - c - n) {
throw new Error("The buffer cannot be grown beyond the maximum size."); throw new Error("The buffer cannot be grown beyond the maximum size.");
} else { } else {
// Not enough space anywhere, we need to allocate. // Not enough space anywhere, we need to allocate.
const buf = new Uint8Array(2 * c + n); const buf = new Uint8Array(2 * c + n);
copyBytes(buf, this.#buf.subarray(this.#off)); copyBytes(this.#buf.subarray(this.#off), buf);
this.#buf = buf; this.#buf = buf;
} }
// Restore this.#off and len(this.#buf). // Restore this.#off and len(this.#buf).