1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

remove function prefix of bytes module

Original: a457942678
This commit is contained in:
axetroy 2019-05-24 20:24:04 +08:00 committed by Ryan Dahl
parent 4ab0e0e918
commit c6505c5de6
3 changed files with 34 additions and 54 deletions

View file

@ -2,7 +2,7 @@
import { copyBytes } from "../io/util.ts"; import { copyBytes } from "../io/util.ts";
/** Find first index of binary pattern from a. If not found, then return -1 **/ /** Find first index of binary pattern from a. If not found, then return -1 **/
export function bytesFindIndex(a: Uint8Array, pat: Uint8Array): number { export function findIndex(a: Uint8Array, pat: Uint8Array): number {
const s = pat[0]; const s = pat[0];
for (let i = 0; i < a.length; i++) { for (let i = 0; i < a.length; i++) {
if (a[i] !== s) continue; if (a[i] !== s) continue;
@ -24,7 +24,7 @@ export function bytesFindIndex(a: Uint8Array, pat: Uint8Array): number {
} }
/** Find last index of binary pattern from a. If not found, then return -1 **/ /** Find last index of binary pattern from a. If not found, then return -1 **/
export function bytesFindLastIndex(a: Uint8Array, pat: Uint8Array): number { export function findLastIndex(a: Uint8Array, pat: Uint8Array): number {
const e = pat[pat.length - 1]; const e = pat[pat.length - 1];
for (let i = a.length - 1; i >= 0; i--) { for (let i = a.length - 1; i >= 0; i--) {
if (a[i] !== e) continue; if (a[i] !== e) continue;
@ -46,7 +46,7 @@ export function bytesFindLastIndex(a: Uint8Array, pat: Uint8Array): number {
} }
/** Check whether binary arrays are equal to each other **/ /** Check whether binary arrays are equal to each other **/
export function bytesEqual(a: Uint8Array, match: Uint8Array): boolean { export function equal(a: Uint8Array, match: Uint8Array): boolean {
if (a.length !== match.length) return false; if (a.length !== match.length) return false;
for (let i = 0; i < match.length; i++) { for (let i = 0; i < match.length; i++) {
if (a[i] !== match[i]) return false; if (a[i] !== match[i]) return false;
@ -55,7 +55,7 @@ export function bytesEqual(a: Uint8Array, match: Uint8Array): boolean {
} }
/** Check whether binary array has binary prefix **/ /** Check whether binary array has binary prefix **/
export function bytesHasPrefix(a: Uint8Array, prefix: Uint8Array): boolean { export function hasPrefix(a: 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 (a[i] !== prefix[i]) return false; if (a[i] !== prefix[i]) return false;
} }
@ -67,7 +67,7 @@ export function bytesHasPrefix(a: Uint8Array, prefix: Uint8Array): boolean {
* @param b The origin bytes * @param b The origin bytes
* @param count The count you want to repeat. * @param count The count you want to repeat.
*/ */
export function bytesRepeat(b: Uint8Array, count: number): Uint8Array { export function repeat(b: Uint8Array, count: number): Uint8Array {
if (count === 0) { if (count === 0) {
return new Uint8Array(); return new Uint8Array();
} }

View file

@ -1,58 +1,46 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { import { findIndex, findLastIndex, equal, hasPrefix, repeat } from "./bytes.ts";
bytesFindIndex,
bytesFindLastIndex,
bytesEqual,
bytesHasPrefix,
bytesRepeat
} from "./bytes.ts";
import { test } from "../testing/mod.ts"; import { test } from "../testing/mod.ts";
import { assertEquals, assertThrows } from "../testing/asserts.ts"; import { assertEquals, assertThrows } from "../testing/asserts.ts";
test(function bytesBytesFindIndex1(): void { test(function bytesfindIndex1(): void {
const i = bytesFindIndex( const i = findIndex(
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);
}); });
test(function bytesBytesFindIndex2(): void { test(function bytesfindIndex2(): void {
const i = bytesFindIndex(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1])); const i = findIndex(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1]));
assertEquals(i, 1); assertEquals(i, 1);
}); });
test(function bytesBytesFindLastIndex1(): void { test(function bytesfindLastIndex1(): void {
const i = bytesFindLastIndex( const i = findLastIndex(
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);
}); });
test(function bytesBytesFindLastIndex2(): void { test(function bytesfindLastIndex2(): void {
const i = bytesFindLastIndex( const i = findLastIndex(new Uint8Array([0, 1, 1]), new Uint8Array([0, 1]));
new Uint8Array([0, 1, 1]),
new Uint8Array([0, 1])
);
assertEquals(i, 0); assertEquals(i, 0);
}); });
test(function bytesBytesBytesEqual(): void { test(function bytesBytesequal(): void {
const v = bytesEqual( const v = equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3]));
new Uint8Array([0, 1, 2, 3]),
new Uint8Array([0, 1, 2, 3])
);
assertEquals(v, true); assertEquals(v, true);
}); });
test(function bytesBytesHasPrefix(): void { test(function byteshasPrefix(): void {
const v = bytesHasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); const v = hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
assertEquals(v, true); assertEquals(v, true);
}); });
test(function bytesBytesRepeat(): void { test(function bytesrepeat(): void {
// input / output / count / error message // input / output / count / error message
const repeatTestCase = [ const repeatTestCase = [
["", "", 0], ["", "", 0],
@ -69,16 +57,13 @@ test(function bytesBytesRepeat(): void {
if (errMsg) { if (errMsg) {
assertThrows( assertThrows(
(): void => { (): void => {
bytesRepeat( repeat(new TextEncoder().encode(input as string), count as number);
new TextEncoder().encode(input as string),
count as number
);
}, },
Error, Error,
errMsg as string errMsg as string
); );
} else { } else {
const newBytes = bytesRepeat( const newBytes = repeat(
new TextEncoder().encode(input as string), new TextEncoder().encode(input as string),
count as number count as number
); );

View file

@ -6,12 +6,7 @@ type Reader = Deno.Reader;
type ReadResult = Deno.ReadResult; type ReadResult = Deno.ReadResult;
type Writer = Deno.Writer; type Writer = Deno.Writer;
import { FormFile } from "../multipart/formfile.ts"; import { FormFile } from "../multipart/formfile.ts";
import { import { findIndex, findLastIndex, hasPrefix, equal } from "../bytes/bytes.ts";
bytesFindIndex,
bytesFindLastIndex,
bytesHasPrefix,
bytesEqual
} from "../bytes/bytes.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 { tempFile } from "../io/util.ts"; import { tempFile } from "../io/util.ts";
@ -60,7 +55,7 @@ export function scanUntilBoundary(
state: BufState state: BufState
): [number, BufState] { ): [number, BufState] {
if (total === 0) { if (total === 0) {
if (bytesHasPrefix(buf, dashBoundary)) { if (hasPrefix(buf, dashBoundary)) {
switch (matchAfterPrefix(buf, dashBoundary, state)) { switch (matchAfterPrefix(buf, dashBoundary, state)) {
case -1: case -1:
return [dashBoundary.length, null]; return [dashBoundary.length, null];
@ -69,12 +64,12 @@ export function scanUntilBoundary(
case 1: case 1:
return [0, "EOF"]; return [0, "EOF"];
} }
if (bytesHasPrefix(dashBoundary, buf)) { if (hasPrefix(dashBoundary, buf)) {
return [0, state]; return [0, state];
} }
} }
} }
const i = bytesFindIndex(buf, newLineDashBoundary); const i = findIndex(buf, newLineDashBoundary);
if (i >= 0) { if (i >= 0) {
switch (matchAfterPrefix(buf.slice(i), newLineDashBoundary, state)) { switch (matchAfterPrefix(buf.slice(i), newLineDashBoundary, state)) {
case -1: case -1:
@ -86,11 +81,11 @@ export function scanUntilBoundary(
return [i, "EOF"]; return [i, "EOF"];
} }
} }
if (bytesHasPrefix(newLineDashBoundary, buf)) { if (hasPrefix(newLineDashBoundary, buf)) {
return [0, state]; return [0, state];
} }
const j = bytesFindLastIndex(buf, newLineDashBoundary.slice(0, 1)); const j = findLastIndex(buf, newLineDashBoundary.slice(0, 1));
if (j >= 0 && bytesHasPrefix(newLineDashBoundary, buf.slice(j))) { if (j >= 0 && hasPrefix(newLineDashBoundary, buf.slice(j))) {
return [j, null]; return [j, null];
} }
return [buf.length, state]; return [buf.length, state];
@ -299,7 +294,7 @@ export class MultipartReader {
if (this.currentPart) { if (this.currentPart) {
this.currentPart.close(); this.currentPart.close();
} }
if (bytesEqual(this.dashBoundary, encoder.encode("--"))) { if (equal(this.dashBoundary, encoder.encode("--"))) {
throw new Error("boundary is empty"); throw new Error("boundary is empty");
} }
let expectNewPart = false; let expectNewPart = false;
@ -331,7 +326,7 @@ export class MultipartReader {
if (this.partsRead === 0) { if (this.partsRead === 0) {
continue; continue;
} }
if (bytesEqual(line, this.newLine)) { if (equal(line, this.newLine)) {
expectNewPart = true; expectNewPart = true;
continue; continue;
} }
@ -340,19 +335,19 @@ export class MultipartReader {
} }
private isFinalBoundary(line: Uint8Array): boolean { private isFinalBoundary(line: Uint8Array): boolean {
if (!bytesHasPrefix(line, this.dashBoundaryDash)) { if (!hasPrefix(line, this.dashBoundaryDash)) {
return false; return false;
} }
let rest = line.slice(this.dashBoundaryDash.length, line.length); let rest = line.slice(this.dashBoundaryDash.length, line.length);
return rest.length === 0 || bytesEqual(skipLWSPChar(rest), this.newLine); return rest.length === 0 || equal(skipLWSPChar(rest), this.newLine);
} }
private isBoundaryDelimiterLine(line: Uint8Array): boolean { private isBoundaryDelimiterLine(line: Uint8Array): boolean {
if (!bytesHasPrefix(line, this.dashBoundary)) { if (!hasPrefix(line, this.dashBoundary)) {
return false; return false;
} }
const rest = line.slice(this.dashBoundary.length); const rest = line.slice(this.dashBoundary.length);
return bytesEqual(skipLWSPChar(rest), this.newLine); return equal(skipLWSPChar(rest), this.newLine);
} }
} }