mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
remove function prefix of bytes module
This commit is contained in:
parent
bd46d60ded
commit
a457942678
@ -2,7 +2,7 @@
|
||||
import { copyBytes } from "../io/util.ts";
|
||||
|
||||
/** 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];
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
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 **/
|
||||
export function bytesFindLastIndex(a: Uint8Array, pat: Uint8Array): number {
|
||||
export function findLastIndex(a: Uint8Array, pat: Uint8Array): number {
|
||||
const e = pat[pat.length - 1];
|
||||
for (let i = a.length - 1; i >= 0; i--) {
|
||||
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 **/
|
||||
export function bytesEqual(a: Uint8Array, match: Uint8Array): boolean {
|
||||
export function equal(a: Uint8Array, match: Uint8Array): boolean {
|
||||
if (a.length !== match.length) return false;
|
||||
for (let i = 0; i < match.length; i++) {
|
||||
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 **/
|
||||
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++) {
|
||||
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 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) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
@ -1,58 +1,46 @@
|
||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import {
|
||||
bytesFindIndex,
|
||||
bytesFindLastIndex,
|
||||
bytesEqual,
|
||||
bytesHasPrefix,
|
||||
bytesRepeat
|
||||
} from "./bytes.ts";
|
||||
import { findIndex, findLastIndex, equal, hasPrefix, repeat } from "./bytes.ts";
|
||||
import { test } from "../testing/mod.ts";
|
||||
import { assertEquals, assertThrows } from "../testing/asserts.ts";
|
||||
|
||||
test(function bytesBytesFindIndex1(): void {
|
||||
const i = bytesFindIndex(
|
||||
test(function bytesfindIndex1(): void {
|
||||
const i = findIndex(
|
||||
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
|
||||
new Uint8Array([0, 1, 2])
|
||||
);
|
||||
assertEquals(i, 2);
|
||||
});
|
||||
|
||||
test(function bytesBytesFindIndex2(): void {
|
||||
const i = bytesFindIndex(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1]));
|
||||
test(function bytesfindIndex2(): void {
|
||||
const i = findIndex(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1]));
|
||||
assertEquals(i, 1);
|
||||
});
|
||||
|
||||
test(function bytesBytesFindLastIndex1(): void {
|
||||
const i = bytesFindLastIndex(
|
||||
test(function bytesfindLastIndex1(): void {
|
||||
const i = findLastIndex(
|
||||
new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]),
|
||||
new Uint8Array([0, 1, 2])
|
||||
);
|
||||
assertEquals(i, 3);
|
||||
});
|
||||
|
||||
test(function bytesBytesFindLastIndex2(): void {
|
||||
const i = bytesFindLastIndex(
|
||||
new Uint8Array([0, 1, 1]),
|
||||
new Uint8Array([0, 1])
|
||||
);
|
||||
test(function bytesfindLastIndex2(): void {
|
||||
const i = findLastIndex(new Uint8Array([0, 1, 1]), new Uint8Array([0, 1]));
|
||||
assertEquals(i, 0);
|
||||
});
|
||||
|
||||
test(function bytesBytesBytesEqual(): void {
|
||||
const v = bytesEqual(
|
||||
new Uint8Array([0, 1, 2, 3]),
|
||||
new Uint8Array([0, 1, 2, 3])
|
||||
);
|
||||
test(function bytesBytesequal(): void {
|
||||
const v = equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3]));
|
||||
assertEquals(v, true);
|
||||
});
|
||||
|
||||
test(function bytesBytesHasPrefix(): void {
|
||||
const v = bytesHasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
|
||||
test(function byteshasPrefix(): void {
|
||||
const v = hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
|
||||
assertEquals(v, true);
|
||||
});
|
||||
|
||||
test(function bytesBytesRepeat(): void {
|
||||
test(function bytesrepeat(): void {
|
||||
// input / output / count / error message
|
||||
const repeatTestCase = [
|
||||
["", "", 0],
|
||||
@ -69,16 +57,13 @@ test(function bytesBytesRepeat(): void {
|
||||
if (errMsg) {
|
||||
assertThrows(
|
||||
(): void => {
|
||||
bytesRepeat(
|
||||
new TextEncoder().encode(input as string),
|
||||
count as number
|
||||
);
|
||||
repeat(new TextEncoder().encode(input as string), count as number);
|
||||
},
|
||||
Error,
|
||||
errMsg as string
|
||||
);
|
||||
} else {
|
||||
const newBytes = bytesRepeat(
|
||||
const newBytes = repeat(
|
||||
new TextEncoder().encode(input as string),
|
||||
count as number
|
||||
);
|
||||
|
@ -6,12 +6,7 @@ type Reader = Deno.Reader;
|
||||
type ReadResult = Deno.ReadResult;
|
||||
type Writer = Deno.Writer;
|
||||
import { FormFile } from "../multipart/formfile.ts";
|
||||
import {
|
||||
bytesFindIndex,
|
||||
bytesFindLastIndex,
|
||||
bytesHasPrefix,
|
||||
bytesEqual
|
||||
} from "../bytes/bytes.ts";
|
||||
import { findIndex, findLastIndex, hasPrefix, equal } from "../bytes/bytes.ts";
|
||||
import { copyN } from "../io/ioutil.ts";
|
||||
import { MultiReader } from "../io/readers.ts";
|
||||
import { tempFile } from "../io/util.ts";
|
||||
@ -60,7 +55,7 @@ export function scanUntilBoundary(
|
||||
state: BufState
|
||||
): [number, BufState] {
|
||||
if (total === 0) {
|
||||
if (bytesHasPrefix(buf, dashBoundary)) {
|
||||
if (hasPrefix(buf, dashBoundary)) {
|
||||
switch (matchAfterPrefix(buf, dashBoundary, state)) {
|
||||
case -1:
|
||||
return [dashBoundary.length, null];
|
||||
@ -69,12 +64,12 @@ export function scanUntilBoundary(
|
||||
case 1:
|
||||
return [0, "EOF"];
|
||||
}
|
||||
if (bytesHasPrefix(dashBoundary, buf)) {
|
||||
if (hasPrefix(dashBoundary, buf)) {
|
||||
return [0, state];
|
||||
}
|
||||
}
|
||||
}
|
||||
const i = bytesFindIndex(buf, newLineDashBoundary);
|
||||
const i = findIndex(buf, newLineDashBoundary);
|
||||
if (i >= 0) {
|
||||
switch (matchAfterPrefix(buf.slice(i), newLineDashBoundary, state)) {
|
||||
case -1:
|
||||
@ -86,11 +81,11 @@ export function scanUntilBoundary(
|
||||
return [i, "EOF"];
|
||||
}
|
||||
}
|
||||
if (bytesHasPrefix(newLineDashBoundary, buf)) {
|
||||
if (hasPrefix(newLineDashBoundary, buf)) {
|
||||
return [0, state];
|
||||
}
|
||||
const j = bytesFindLastIndex(buf, newLineDashBoundary.slice(0, 1));
|
||||
if (j >= 0 && bytesHasPrefix(newLineDashBoundary, buf.slice(j))) {
|
||||
const j = findLastIndex(buf, newLineDashBoundary.slice(0, 1));
|
||||
if (j >= 0 && hasPrefix(newLineDashBoundary, buf.slice(j))) {
|
||||
return [j, null];
|
||||
}
|
||||
return [buf.length, state];
|
||||
@ -299,7 +294,7 @@ export class MultipartReader {
|
||||
if (this.currentPart) {
|
||||
this.currentPart.close();
|
||||
}
|
||||
if (bytesEqual(this.dashBoundary, encoder.encode("--"))) {
|
||||
if (equal(this.dashBoundary, encoder.encode("--"))) {
|
||||
throw new Error("boundary is empty");
|
||||
}
|
||||
let expectNewPart = false;
|
||||
@ -331,7 +326,7 @@ export class MultipartReader {
|
||||
if (this.partsRead === 0) {
|
||||
continue;
|
||||
}
|
||||
if (bytesEqual(line, this.newLine)) {
|
||||
if (equal(line, this.newLine)) {
|
||||
expectNewPart = true;
|
||||
continue;
|
||||
}
|
||||
@ -340,19 +335,19 @@ export class MultipartReader {
|
||||
}
|
||||
|
||||
private isFinalBoundary(line: Uint8Array): boolean {
|
||||
if (!bytesHasPrefix(line, this.dashBoundaryDash)) {
|
||||
if (!hasPrefix(line, this.dashBoundaryDash)) {
|
||||
return false;
|
||||
}
|
||||
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 {
|
||||
if (!bytesHasPrefix(line, this.dashBoundary)) {
|
||||
if (!hasPrefix(line, this.dashBoundary)) {
|
||||
return false;
|
||||
}
|
||||
const rest = line.slice(this.dashBoundary.length);
|
||||
return bytesEqual(skipLWSPChar(rest), this.newLine);
|
||||
return equal(skipLWSPChar(rest), this.newLine);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user