0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 20:25:12 -05:00

BREAKING(std/bytes): Adjust APIs based on std-wg discussion (#8612)

This commit is contained in:
Yosi Pramajaya 2020-12-06 21:51:13 +07:00 committed by GitHub
parent c10280214e
commit 0b37a79060
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 215 additions and 108 deletions

View file

@ -6,67 +6,77 @@ bytes module is made to provide helpers to manipulation of bytes slice.
All the following functions are exposed in `mod.ts`. All the following functions are exposed in `mod.ts`.
## findIndex ## indexOf
Find first index of binary pattern from given binary array. Find first index of binary pattern from given binary array, or -1 if it is not
present.
```typescript ```typescript
import { findIndex } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; import { indexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
findIndex( indexOf(
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]), new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2]),
); ); // => returns 2
// => returns 2 indexOf(
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
new Uint8Array([0, 1, 2]),
3,
); // => returns 5
``` ```
## findLastIndex ## lastIndexOf
Find last index of binary pattern from given binary array. Find last index of binary pattern from given binary array, or -1 if it is not
present.
```typescript ```typescript
import { findLastIndex } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; import { lastIndexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
findLastIndex( lastIndexOf(
new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]), new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2]),
); ); // => returns 5
// => returns 3 lastIndexOf(
new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
new Uint8Array([0, 1, 2]),
3,
); // => returns 0
``` ```
## equal ## equals
Check whether given binary arrays are equal to each other. Check whether given binary arrays are equal to each other.
```typescript ```typescript
import { equal } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; import { equals } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); // returns true equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); // returns true
equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 4])); // returns false equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 4])); // returns false
``` ```
## hasPrefix ## startsWith
Check whether binary array has binary prefix. Check whether binary array starts with prefix.
```typescript ```typescript
import { hasPrefix } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; import { startsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns true startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns true
hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns false startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns false
``` ```
## hasSuffix ## endsWith
Check whether binary array ends with suffix. Check whether binary array ends with suffix.
```typescript ```typescript
import { hasSuffix } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; import { endsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
hasSuffix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns false endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns false
hasSuffix(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns true endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns true
``` ```
## repeat ## repeat
@ -81,12 +91,19 @@ repeat(new Uint8Array([1]), 3); // returns Uint8Array(3) [ 1, 1, 1 ]
## concat ## concat
Concatenate two binary arrays and return new one. Concatenate multiple binary arrays and return new one.
```typescript ```typescript
import { concat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; import { concat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
concat(new Uint8Array([1, 2]), new Uint8Array([3, 4])); // returns Uint8Array(4) [ 1, 2, 3, 4 ] concat(new Uint8Array([1, 2]), new Uint8Array([3, 4])); // returns Uint8Array(4) [ 1, 2, 3, 4 ]
concat(
new Uint8Array([1, 2]),
new Uint8Array([3, 4]),
new Uint8Array([5, 6]),
new Uint8Array([7, 8]),
); // => returns Uint8Array(8) [ 1, 2, 3, 4, 5, 6, 7, 8 ]
``` ```
## contains ## contains
@ -107,14 +124,14 @@ contains(
); // => returns false ); // => returns false
``` ```
## copyBytes ## copy
Copy bytes from one binary array to another. Copy bytes from one binary array to another.
```typescript ```typescript
import { copyBytes } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; import { copy } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
const dst = new Uint8Array(4); const dst = new Uint8Array(4);
const src = Uint8Array.of(1, 2, 3, 4); const src = Uint8Array.of(1, 2, 3, 4);
const len = copyBytes(src, dest); // returns len = 4 const len = copy(src, dest); // returns len = 4
``` ```

View file

@ -1,12 +1,23 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
/** Find first index of binary pattern from a. If not found, then return -1 /** Find first index of binary pattern from source. If not found, then return -1
* @param source source array * @param source source array
* @param pat pattern to find in source array * @param pat pattern to find in source array
* @param start the index to start looking in the source
*/ */
export function findIndex(source: Uint8Array, pat: Uint8Array): number { export function indexOf(
source: Uint8Array,
pat: Uint8Array,
start = 0,
): number {
if (start >= source.length) {
return -1;
}
if (start < 0) {
start = 0;
}
const s = pat[0]; const s = pat[0];
for (let i = 0; i < source.length; i++) { for (let i = start; i < source.length; i++) {
if (source[i] !== s) continue; if (source[i] !== s) continue;
const pin = i; const pin = i;
let matched = 1; let matched = 1;
@ -25,13 +36,24 @@ export function findIndex(source: Uint8Array, pat: Uint8Array): number {
return -1; return -1;
} }
/** Find last index of binary pattern from a. If not found, then return -1. /** Find last index of binary pattern from source. If not found, then return -1.
* @param source source array * @param source source array
* @param pat pattern to find in source array * @param pat pattern to find in source array
* @param start the index to start looking in the source
*/ */
export function findLastIndex(source: Uint8Array, pat: Uint8Array): number { export function lastIndexOf(
source: Uint8Array,
pat: Uint8Array,
start = source.length - 1,
): number {
if (start < 0) {
return -1;
}
if (start >= source.length) {
start = source.length - 1;
}
const e = pat[pat.length - 1]; const e = pat[pat.length - 1];
for (let i = source.length - 1; i >= 0; i--) { for (let i = start; i >= 0; i--) {
if (source[i] !== e) continue; if (source[i] !== e) continue;
const pin = i; const pin = i;
let matched = 1; let matched = 1;
@ -51,22 +73,22 @@ export function findLastIndex(source: Uint8Array, pat: Uint8Array): number {
} }
/** Check whether binary arrays are equal to each other. /** Check whether binary arrays are equal to each other.
* @param source first array to check equality * @param a first array to check equality
* @param match second array to check equality * @param b second array to check equality
*/ */
export function equal(source: Uint8Array, match: Uint8Array): boolean { export function equals(a: Uint8Array, b: Uint8Array): boolean {
if (source.length !== match.length) return false; if (a.length !== b.length) return false;
for (let i = 0; i < match.length; i++) { for (let i = 0; i < b.length; i++) {
if (source[i] !== match[i]) return false; if (a[i] !== b[i]) return false;
} }
return true; return true;
} }
/** Check whether binary array starts with prefix. /** Check whether binary array starts with prefix.
* @param source srouce array * @param source source array
* @param prefix prefix array to check in source * @param prefix prefix array to check in source
*/ */
export function hasPrefix(source: Uint8Array, prefix: Uint8Array): boolean { export function startsWith(source: Uint8Array, prefix: Uint8Array): boolean {
for (let i = 0, max = prefix.length; i < max; i++) { for (let i = 0, max = prefix.length; i < max; i++) {
if (source[i] !== prefix[i]) return false; if (source[i] !== prefix[i]) return false;
} }
@ -77,7 +99,7 @@ export function hasPrefix(source: Uint8Array, prefix: Uint8Array): boolean {
* @param source source array * @param source source array
* @param suffix suffix array to check in source * @param suffix suffix array to check in source
*/ */
export function hasSuffix(source: Uint8Array, suffix: Uint8Array): boolean { export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean {
for ( for (
let srci = source.length - 1, sfxi = suffix.length - 1; let srci = source.length - 1, sfxi = suffix.length - 1;
sfxi >= 0; sfxi >= 0;
@ -91,6 +113,7 @@ export function hasSuffix(source: Uint8Array, suffix: Uint8Array): boolean {
/** Repeat bytes. returns a new byte slice consisting of `count` copies of `b`. /** Repeat bytes. returns a new byte slice consisting of `count` copies of `b`.
* @param origin The origin bytes * @param origin The origin bytes
* @param count The count you want to repeat. * @param count The count you want to repeat.
* @throws `RangeError` When count is negative
*/ */
export function repeat(origin: Uint8Array, count: number): Uint8Array { export function repeat(origin: Uint8Array, count: number): Uint8Array {
if (count === 0) { if (count === 0) {
@ -98,7 +121,7 @@ export function repeat(origin: Uint8Array, count: number): Uint8Array {
} }
if (count < 0) { if (count < 0) {
throw new Error("bytes: negative repeat count"); throw new RangeError("bytes: negative repeat count");
} else if ((origin.length * count) / count !== origin.length) { } else if ((origin.length * count) / count !== origin.length) {
throw new Error("bytes: repeat count causes overflow"); throw new Error("bytes: repeat count causes overflow");
} }
@ -111,23 +134,31 @@ export function repeat(origin: Uint8Array, count: number): Uint8Array {
const nb = new Uint8Array(origin.length * count); const nb = new Uint8Array(origin.length * count);
let bp = copyBytes(origin, nb); let bp = copy(origin, nb);
for (; bp < nb.length; bp *= 2) { for (; bp < nb.length; bp *= 2) {
copyBytes(nb.slice(0, bp), nb, bp); copy(nb.slice(0, bp), nb, bp);
} }
return nb; return nb;
} }
/** Concatenate two binary arrays and return new one. /** Concatenate multiple binary arrays and return new one.
* @param origin origin array to concatenate * @param buf binary arrays to concatenate
* @param b array to concatenate with origin
*/ */
export function concat(origin: Uint8Array, b: Uint8Array): Uint8Array { export function concat(...buf: Uint8Array[]): Uint8Array {
const output = new Uint8Array(origin.length + b.length); let length = 0;
output.set(origin, 0); for (const b of buf) {
output.set(b, origin.length); length += b.length;
}
const output = new Uint8Array(length);
let index = 0;
for (const b of buf) {
output.set(b, index);
index += b.length;
}
return output; return output;
} }
@ -136,7 +167,7 @@ export function concat(origin: Uint8Array, b: Uint8Array): Uint8Array {
* @param pat patter array * @param pat patter array
*/ */
export function contains(source: Uint8Array, pat: Uint8Array): boolean { export function contains(source: Uint8Array, pat: Uint8Array): boolean {
return findIndex(source, pat) != -1; return indexOf(source, pat) != -1;
} }
/** /**
@ -148,7 +179,7 @@ export function contains(source: Uint8Array, pat: Uint8Array): boolean {
* @param off Offset into `dst` at which to begin writing values from `src`. * @param off Offset into `dst` at which to begin writing values from `src`.
* @return number of bytes copied * @return number of bytes copied
*/ */
export function copyBytes(src: Uint8Array, dst: Uint8Array, off = 0): number { export function copy(src: Uint8Array, dst: Uint8Array, off = 0): number {
off = Math.max(0, Math.min(off, dst.byteLength)); off = Math.max(0, Math.min(off, dst.byteLength));
const dstBytesAvailable = dst.byteLength - off; const dstBytesAvailable = dst.byteLength - off;
if (src.byteLength > dstBytesAvailable) { if (src.byteLength > dstBytesAvailable) {

View file

@ -3,60 +3,106 @@
import { import {
concat, concat,
contains, contains,
copyBytes, copy,
equal, endsWith,
findIndex, equals,
findLastIndex, indexOf,
hasPrefix, lastIndexOf,
hasSuffix,
repeat, repeat,
startsWith,
} from "./mod.ts"; } from "./mod.ts";
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts"; import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
import { decode, encode } from "../encoding/utf8.ts"; import { decode, encode } from "../encoding/utf8.ts";
Deno.test("[bytes] findIndex1", () => { Deno.test("[bytes] indexOf1", () => {
const i = findIndex( const i = indexOf(
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]), new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2]),
); );
assertEquals(i, 2); assertEquals(i, 2);
}); });
Deno.test("[bytes] findIndex2", () => { Deno.test("[bytes] indexOf2", () => {
const i = findIndex(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1])); const i = indexOf(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1]));
assertEquals(i, 1); assertEquals(i, 1);
}); });
Deno.test("[bytes] findIndex3", () => { Deno.test("[bytes] indexOf3", () => {
const i = findIndex(encode("Deno"), encode("D")); const i = indexOf(encode("Deno"), encode("D"));
assertEquals(i, 0); assertEquals(i, 0);
}); });
Deno.test("[bytes] findLastIndex1", () => { Deno.test("[bytes] indexOf4", () => {
const i = findLastIndex( const i = indexOf(new Uint8Array(), new Uint8Array([0, 1]));
assertEquals(i, -1);
});
Deno.test("[bytes] indexOf with start index", () => {
const i = indexOf(
new Uint8Array([0, 1, 2, 0, 1, 2]),
new Uint8Array([0, 1]),
1,
);
assertEquals(i, 3);
});
Deno.test("[bytes] indexOf with start index 2", () => {
const i = indexOf(
new Uint8Array([0, 1, 2, 0, 1, 2]),
new Uint8Array([0, 1]),
7,
);
assertEquals(i, -1);
});
Deno.test("[bytes] lastIndexOf1", () => {
const i = lastIndexOf(
new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]), new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]),
new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2]),
); );
assertEquals(i, 3); assertEquals(i, 3);
}); });
Deno.test("[bytes] findLastIndex2", () => { Deno.test("[bytes] lastIndexOf2", () => {
const i = findLastIndex(new Uint8Array([0, 1, 1]), new Uint8Array([0, 1])); const i = lastIndexOf(new Uint8Array([0, 1, 1]), new Uint8Array([0, 1]));
assertEquals(i, 0); assertEquals(i, 0);
}); });
Deno.test("[bytes] equal", () => { Deno.test("[bytes] lastIndexOf3", () => {
const v = equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); const i = lastIndexOf(new Uint8Array(), new Uint8Array([0, 1]));
assertEquals(i, -1);
});
Deno.test("[bytes] lastIndexOf with start index", () => {
const i = lastIndexOf(
new Uint8Array([0, 1, 2, 0, 1, 2]),
new Uint8Array([0, 1]),
2,
);
assertEquals(i, 0);
});
Deno.test("[bytes] lastIndexOf with start index 2", () => {
const i = lastIndexOf(
new Uint8Array([0, 1, 2, 0, 1, 2]),
new Uint8Array([0, 1]),
-1,
);
assertEquals(i, -1);
});
Deno.test("[bytes] equals", () => {
const v = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3]));
assertEquals(v, true); assertEquals(v, true);
}); });
Deno.test("[bytes] hasPrefix", () => { Deno.test("[bytes] startsWith", () => {
const v = hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); const v = startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
assertEquals(v, true); assertEquals(v, true);
}); });
Deno.test("[bytes] hasSuffix", () => { Deno.test("[bytes] endsWith", () => {
const v = hasSuffix(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); const v = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2]));
assertEquals(v, true); assertEquals(v, true);
}); });
@ -111,7 +157,20 @@ Deno.test("[bytes] concat empty arrays", () => {
assert(u2 !== joined); assert(u2 !== joined);
}); });
Deno.test("[bytes] contain", () => { Deno.test("[bytes] concat multiple arrays", () => {
const u1 = encode("Hello ");
const u2 = encode("W");
const u3 = encode("o");
const u4 = encode("r");
const u5 = encode("l");
const u6 = encode("d");
const joined = concat(u1, u2, u3, u4, u5, u6);
assertEquals(decode(joined), "Hello World");
assert(u1 !== joined);
assert(u2 !== joined);
});
Deno.test("[bytes] contains", () => {
const source = encode("deno.land"); const source = encode("deno.land");
const pattern = encode("deno"); const pattern = encode("deno");
assert(contains(source, pattern)); assert(contains(source, pattern));
@ -119,36 +178,36 @@ Deno.test("[bytes] contain", () => {
assert(contains(new Uint8Array([0, 1, 2, 3]), new Uint8Array([2, 3]))); assert(contains(new Uint8Array([0, 1, 2, 3]), new Uint8Array([2, 3])));
}); });
Deno.test("[io/tuil] copyBytes", function (): void { Deno.test("[bytes] copy", function (): void {
const dst = new Uint8Array(4); const dst = new Uint8Array(4);
dst.fill(0); dst.fill(0);
let src = Uint8Array.of(1, 2); let src = Uint8Array.of(1, 2);
let len = copyBytes(src, dst, 0); let len = copy(src, dst, 0);
assert(len === 2); assert(len === 2);
assertEquals(dst, Uint8Array.of(1, 2, 0, 0)); assertEquals(dst, Uint8Array.of(1, 2, 0, 0));
dst.fill(0); dst.fill(0);
src = Uint8Array.of(1, 2); src = Uint8Array.of(1, 2);
len = copyBytes(src, dst, 1); len = copy(src, dst, 1);
assert(len === 2); assert(len === 2);
assertEquals(dst, Uint8Array.of(0, 1, 2, 0)); assertEquals(dst, Uint8Array.of(0, 1, 2, 0));
dst.fill(0); dst.fill(0);
src = Uint8Array.of(1, 2, 3, 4, 5); src = Uint8Array.of(1, 2, 3, 4, 5);
len = copyBytes(src, dst); len = copy(src, dst);
assert(len === 4); assert(len === 4);
assertEquals(dst, Uint8Array.of(1, 2, 3, 4)); assertEquals(dst, Uint8Array.of(1, 2, 3, 4));
dst.fill(0); dst.fill(0);
src = Uint8Array.of(1, 2); src = Uint8Array.of(1, 2);
len = copyBytes(src, dst, 100); len = copy(src, dst, 100);
assert(len === 0); assert(len === 0);
assertEquals(dst, Uint8Array.of(0, 0, 0, 0)); assertEquals(dst, Uint8Array.of(0, 0, 0, 0));
dst.fill(0); dst.fill(0);
src = Uint8Array.of(3, 4); src = Uint8Array.of(3, 4);
len = copyBytes(src, dst, -2); len = copy(src, dst, -2);
assert(len === 2); assert(len === 2);
assertEquals(dst, Uint8Array.of(3, 4, 0, 0)); assertEquals(dst, Uint8Array.of(3, 4, 0, 0));
}); });

View file

@ -7,7 +7,7 @@
type Reader = Deno.Reader; type Reader = Deno.Reader;
type Writer = Deno.Writer; type Writer = Deno.Writer;
type WriterSync = Deno.WriterSync; type WriterSync = Deno.WriterSync;
import { copyBytes } from "../bytes/mod.ts"; import { copy } from "../bytes/mod.ts";
import { assert } from "../_util/assert.ts"; import { assert } from "../_util/assert.ts";
const DEFAULT_BUF_SIZE = 4096; const DEFAULT_BUF_SIZE = 4096;
@ -150,7 +150,7 @@ export class BufReader implements Reader {
} }
// copy as much as we can // copy as much as we can
const copied = copyBytes(this.buf.subarray(this.r, this.w), p, 0); const copied = copy(this.buf.subarray(this.r, this.w), p, 0);
this.r += copied; this.r += copied;
// this.lastByte = this.buf[this.r - 1]; // this.lastByte = this.buf[this.r - 1];
// this.lastCharSize = -1; // this.lastCharSize = -1;
@ -502,7 +502,7 @@ export class BufWriter extends AbstractBufBase implements Writer {
throw e; throw e;
} }
} else { } else {
numBytesWritten = copyBytes(data, this.buf, this.usedBufferBytes); numBytesWritten = copy(data, this.buf, this.usedBufferBytes);
this.usedBufferBytes += numBytesWritten; this.usedBufferBytes += numBytesWritten;
await this.flush(); await this.flush();
} }
@ -510,7 +510,7 @@ export class BufWriter extends AbstractBufBase implements Writer {
data = data.subarray(numBytesWritten); data = data.subarray(numBytesWritten);
} }
numBytesWritten = copyBytes(data, this.buf, this.usedBufferBytes); numBytesWritten = copy(data, this.buf, this.usedBufferBytes);
this.usedBufferBytes += numBytesWritten; this.usedBufferBytes += numBytesWritten;
totalBytesWritten += numBytesWritten; totalBytesWritten += numBytesWritten;
return totalBytesWritten; return totalBytesWritten;
@ -595,7 +595,7 @@ export class BufWriterSync extends AbstractBufBase implements WriterSync {
throw e; throw e;
} }
} else { } else {
numBytesWritten = copyBytes(data, this.buf, this.usedBufferBytes); numBytesWritten = copy(data, this.buf, this.usedBufferBytes);
this.usedBufferBytes += numBytesWritten; this.usedBufferBytes += numBytesWritten;
this.flush(); this.flush();
} }
@ -603,7 +603,7 @@ export class BufWriterSync extends AbstractBufBase implements WriterSync {
data = data.subarray(numBytesWritten); data = data.subarray(numBytesWritten);
} }
numBytesWritten = copyBytes(data, this.buf, this.usedBufferBytes); numBytesWritten = copy(data, this.buf, this.usedBufferBytes);
this.usedBufferBytes += numBytesWritten; this.usedBufferBytes += numBytesWritten;
totalBytesWritten += numBytesWritten; totalBytesWritten += numBytesWritten;
return totalBytesWritten; return totalBytesWritten;

View file

@ -17,7 +17,7 @@ import {
import * as iotest from "./_iotest.ts"; import * as iotest from "./_iotest.ts";
import { StringReader } from "./readers.ts"; import { StringReader } from "./readers.ts";
import { StringWriter } from "./writers.ts"; import { StringWriter } from "./writers.ts";
import { copyBytes } from "../bytes/mod.ts"; import { copy } from "../bytes/mod.ts";
const encoder = new TextEncoder(); const encoder = new TextEncoder();
@ -201,7 +201,7 @@ class TestReader implements Deno.Reader {
if (nread === 0) { if (nread === 0) {
return Promise.resolve(null); return Promise.resolve(null);
} }
copyBytes(this.data, buf as Uint8Array); copy(this.data, buf as Uint8Array);
this.data = this.data.subarray(nread); this.data = this.data.subarray(nread);
return Promise.resolve(nread); return Promise.resolve(nread);
} }

View file

@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { equal, findIndex, findLastIndex, hasPrefix } from "../bytes/mod.ts"; import { equals, indexOf, lastIndexOf, startsWith } from "../bytes/mod.ts";
import { copyN } from "../io/ioutil.ts"; import { copyN } from "../io/ioutil.ts";
import { MultiReader } from "../io/readers.ts"; import { MultiReader } from "../io/readers.ts";
import { extname } from "../path/mod.ts"; import { extname } from "../path/mod.ts";
@ -101,7 +101,7 @@ export function scanUntilBoundary(
): number | null { ): number | null {
if (total === 0) { if (total === 0) {
// At beginning of body, allow dashBoundary. // At beginning of body, allow dashBoundary.
if (hasPrefix(buf, dashBoundary)) { if (startsWith(buf, dashBoundary)) {
switch (matchAfterPrefix(buf, dashBoundary, eof)) { switch (matchAfterPrefix(buf, dashBoundary, eof)) {
case -1: case -1:
return dashBoundary.length; return dashBoundary.length;
@ -111,13 +111,13 @@ export function scanUntilBoundary(
return null; return null;
} }
} }
if (hasPrefix(dashBoundary, buf)) { if (startsWith(dashBoundary, buf)) {
return 0; return 0;
} }
} }
// Search for "\n--boundary". // Search for "\n--boundary".
const i = findIndex(buf, newLineDashBoundary); const i = indexOf(buf, newLineDashBoundary);
if (i >= 0) { if (i >= 0) {
switch (matchAfterPrefix(buf.slice(i), newLineDashBoundary, eof)) { switch (matchAfterPrefix(buf.slice(i), newLineDashBoundary, eof)) {
case -1: case -1:
@ -128,15 +128,15 @@ export function scanUntilBoundary(
return i > 0 ? i : null; return i > 0 ? i : null;
} }
} }
if (hasPrefix(newLineDashBoundary, buf)) { if (startsWith(newLineDashBoundary, buf)) {
return 0; return 0;
} }
// Otherwise, anything up to the final \n is not part of the boundary and so // Otherwise, anything up to the final \n is not part of the boundary and so
// must be part of the body. Also, if the section from the final \n onward is // must be part of the body. Also, if the section from the final \n onward is
// not a prefix of the boundary, it too must be part of the body. // not a prefix of the boundary, it too must be part of the body.
const j = findLastIndex(buf, newLineDashBoundary.slice(0, 1)); const j = lastIndexOf(buf, newLineDashBoundary.slice(0, 1));
if (j >= 0 && hasPrefix(newLineDashBoundary, buf.slice(j))) { if (j >= 0 && startsWith(newLineDashBoundary, buf.slice(j))) {
return j; return j;
} }
@ -364,7 +364,7 @@ export class MultipartReader {
if (this.currentPart) { if (this.currentPart) {
this.currentPart.close(); this.currentPart.close();
} }
if (equal(this.dashBoundary, encoder.encode("--"))) { if (equals(this.dashBoundary, encoder.encode("--"))) {
throw new Error("boundary is empty"); throw new Error("boundary is empty");
} }
let expectNewPart = false; let expectNewPart = false;
@ -393,7 +393,7 @@ export class MultipartReader {
if (this.partsRead === 0) { if (this.partsRead === 0) {
continue; continue;
} }
if (equal(line, this.newLine)) { if (equals(line, this.newLine)) {
expectNewPart = true; expectNewPart = true;
continue; continue;
} }
@ -402,19 +402,19 @@ export class MultipartReader {
} }
private isFinalBoundary(line: Uint8Array): boolean { private isFinalBoundary(line: Uint8Array): boolean {
if (!hasPrefix(line, this.dashBoundaryDash)) { if (!startsWith(line, this.dashBoundaryDash)) {
return false; return false;
} }
const rest = line.slice(this.dashBoundaryDash.length, line.length); const rest = line.slice(this.dashBoundaryDash.length, line.length);
return rest.length === 0 || equal(skipLWSPChar(rest), this.newLine); return rest.length === 0 || equals(skipLWSPChar(rest), this.newLine);
} }
private isBoundaryDelimiterLine(line: Uint8Array): boolean { private isBoundaryDelimiterLine(line: Uint8Array): boolean {
if (!hasPrefix(line, this.dashBoundary)) { if (!startsWith(line, this.dashBoundary)) {
return false; return false;
} }
const rest = line.slice(this.dashBoundary.length); const rest = line.slice(this.dashBoundary.length);
return equal(skipLWSPChar(rest), this.newLine); return equals(skipLWSPChar(rest), this.newLine);
} }
} }

View file

@ -327,7 +327,7 @@ Deno.test({
assertEquals(decode(second.payload), "second"); assertEquals(decode(second.payload), "second");
assertEquals(ping.opcode, OpCode.Ping); assertEquals(ping.opcode, OpCode.Ping);
assertEquals(third.opcode, OpCode.BinaryFrame); assertEquals(third.opcode, OpCode.BinaryFrame);
assertEquals(bytes.equal(third.payload, new Uint8Array([3])), true); assertEquals(bytes.equals(third.payload, new Uint8Array([3])), true);
}, },
}); });