Skip to content

Commit

Permalink
remove function prefix of bytes module
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy authored and ry committed May 24, 2019
1 parent bd46d60 commit a457942
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 54 deletions.
10 changes: 5 additions & 5 deletions bytes/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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();
}
Expand Down
47 changes: 16 additions & 31 deletions bytes/bytes_test.ts
Original file line number Diff line number Diff line change
@@ -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],
Expand All @@ -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
);
Expand Down
31 changes: 13 additions & 18 deletions mime/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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];
Expand All @@ -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:
Expand All @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}

Expand Down

0 comments on commit a457942

Please sign in to comment.