Skip to content

Commit 61b736a

Browse files
yos1pcaspervonb
authored andcommitted
BREAKING(bytes): Adjust APIs based on std-wg discussion (denoland/deno#8612)
1 parent db46c14 commit 61b736a

File tree

7 files changed

+215
-108
lines changed

7 files changed

+215
-108
lines changed

bytes/README.md

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,77 @@ bytes module is made to provide helpers to manipulation of bytes slice.
66

77
All the following functions are exposed in `mod.ts`.
88

9-
## findIndex
9+
## indexOf
1010

11-
Find first index of binary pattern from given binary array.
11+
Find first index of binary pattern from given binary array, or -1 if it is not
12+
present.
1213

1314
```typescript
14-
import { findIndex } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
15+
import { indexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
1516

16-
findIndex(
17+
indexOf(
1718
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
1819
new Uint8Array([0, 1, 2]),
19-
);
20+
); // => returns 2
2021

21-
// => returns 2
22+
indexOf(
23+
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
24+
new Uint8Array([0, 1, 2]),
25+
3,
26+
); // => returns 5
2227
```
2328

24-
## findLastIndex
29+
## lastIndexOf
2530

26-
Find last index of binary pattern from given binary array.
31+
Find last index of binary pattern from given binary array, or -1 if it is not
32+
present.
2733

2834
```typescript
29-
import { findLastIndex } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
35+
import { lastIndexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
3036

31-
findLastIndex(
32-
new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]),
37+
lastIndexOf(
38+
new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
3339
new Uint8Array([0, 1, 2]),
34-
);
40+
); // => returns 5
3541

36-
// => returns 3
42+
lastIndexOf(
43+
new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
44+
new Uint8Array([0, 1, 2]),
45+
3,
46+
); // => returns 0
3747
```
3848

39-
## equal
49+
## equals
4050

4151
Check whether given binary arrays are equal to each other.
4252

4353
```typescript
44-
import { equal } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
54+
import { equals } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
4555

46-
equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); // returns true
47-
equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 4])); // returns false
56+
equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); // returns true
57+
equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 4])); // returns false
4858
```
4959

50-
## hasPrefix
60+
## startsWith
5161

52-
Check whether binary array has binary prefix.
62+
Check whether binary array starts with prefix.
5363

5464
```typescript
55-
import { hasPrefix } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
65+
import { startsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
5666

57-
hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns true
58-
hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns false
67+
startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns true
68+
startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns false
5969
```
6070

61-
## hasSuffix
71+
## endsWith
6272

6373
Check whether binary array ends with suffix.
6474

6575
```typescript
66-
import { hasSuffix } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
76+
import { endsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
6777

68-
hasSuffix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns false
69-
hasSuffix(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns true
78+
endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns false
79+
endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns true
7080
```
7181

7282
## repeat
@@ -81,12 +91,19 @@ repeat(new Uint8Array([1]), 3); // returns Uint8Array(3) [ 1, 1, 1 ]
8191

8292
## concat
8393

84-
Concatenate two binary arrays and return new one.
94+
Concatenate multiple binary arrays and return new one.
8595

8696
```typescript
8797
import { concat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
8898

8999
concat(new Uint8Array([1, 2]), new Uint8Array([3, 4])); // returns Uint8Array(4) [ 1, 2, 3, 4 ]
100+
101+
concat(
102+
new Uint8Array([1, 2]),
103+
new Uint8Array([3, 4]),
104+
new Uint8Array([5, 6]),
105+
new Uint8Array([7, 8]),
106+
); // => returns Uint8Array(8) [ 1, 2, 3, 4, 5, 6, 7, 8 ]
90107
```
91108

92109
## contains
@@ -107,14 +124,14 @@ contains(
107124
); // => returns false
108125
```
109126

110-
## copyBytes
127+
## copy
111128

112129
Copy bytes from one binary array to another.
113130

114131
```typescript
115-
import { copyBytes } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
132+
import { copy } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
116133

117134
const dst = new Uint8Array(4);
118135
const src = Uint8Array.of(1, 2, 3, 4);
119-
const len = copyBytes(src, dest); // returns len = 4
136+
const len = copy(src, dest); // returns len = 4
120137
```

bytes/mod.ts

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
22

3-
/** Find first index of binary pattern from a. If not found, then return -1
3+
/** Find first index of binary pattern from source. If not found, then return -1
44
* @param source source array
55
* @param pat pattern to find in source array
6+
* @param start the index to start looking in the source
67
*/
7-
export function findIndex(source: Uint8Array, pat: Uint8Array): number {
8+
export function indexOf(
9+
source: Uint8Array,
10+
pat: Uint8Array,
11+
start = 0,
12+
): number {
13+
if (start >= source.length) {
14+
return -1;
15+
}
16+
if (start < 0) {
17+
start = 0;
18+
}
819
const s = pat[0];
9-
for (let i = 0; i < source.length; i++) {
20+
for (let i = start; i < source.length; i++) {
1021
if (source[i] !== s) continue;
1122
const pin = i;
1223
let matched = 1;
@@ -25,13 +36,24 @@ export function findIndex(source: Uint8Array, pat: Uint8Array): number {
2536
return -1;
2637
}
2738

28-
/** Find last index of binary pattern from a. If not found, then return -1.
39+
/** Find last index of binary pattern from source. If not found, then return -1.
2940
* @param source source array
3041
* @param pat pattern to find in source array
42+
* @param start the index to start looking in the source
3143
*/
32-
export function findLastIndex(source: Uint8Array, pat: Uint8Array): number {
44+
export function lastIndexOf(
45+
source: Uint8Array,
46+
pat: Uint8Array,
47+
start = source.length - 1,
48+
): number {
49+
if (start < 0) {
50+
return -1;
51+
}
52+
if (start >= source.length) {
53+
start = source.length - 1;
54+
}
3355
const e = pat[pat.length - 1];
34-
for (let i = source.length - 1; i >= 0; i--) {
56+
for (let i = start; i >= 0; i--) {
3557
if (source[i] !== e) continue;
3658
const pin = i;
3759
let matched = 1;
@@ -51,22 +73,22 @@ export function findLastIndex(source: Uint8Array, pat: Uint8Array): number {
5173
}
5274

5375
/** Check whether binary arrays are equal to each other.
54-
* @param source first array to check equality
55-
* @param match second array to check equality
76+
* @param a first array to check equality
77+
* @param b second array to check equality
5678
*/
57-
export function equal(source: Uint8Array, match: Uint8Array): boolean {
58-
if (source.length !== match.length) return false;
59-
for (let i = 0; i < match.length; i++) {
60-
if (source[i] !== match[i]) return false;
79+
export function equals(a: Uint8Array, b: Uint8Array): boolean {
80+
if (a.length !== b.length) return false;
81+
for (let i = 0; i < b.length; i++) {
82+
if (a[i] !== b[i]) return false;
6183
}
6284
return true;
6385
}
6486

6587
/** Check whether binary array starts with prefix.
66-
* @param source srouce array
88+
* @param source source array
6789
* @param prefix prefix array to check in source
6890
*/
69-
export function hasPrefix(source: Uint8Array, prefix: Uint8Array): boolean {
91+
export function startsWith(source: Uint8Array, prefix: Uint8Array): boolean {
7092
for (let i = 0, max = prefix.length; i < max; i++) {
7193
if (source[i] !== prefix[i]) return false;
7294
}
@@ -77,7 +99,7 @@ export function hasPrefix(source: Uint8Array, prefix: Uint8Array): boolean {
7799
* @param source source array
78100
* @param suffix suffix array to check in source
79101
*/
80-
export function hasSuffix(source: Uint8Array, suffix: Uint8Array): boolean {
102+
export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean {
81103
for (
82104
let srci = source.length - 1, sfxi = suffix.length - 1;
83105
sfxi >= 0;
@@ -91,14 +113,15 @@ export function hasSuffix(source: Uint8Array, suffix: Uint8Array): boolean {
91113
/** Repeat bytes. returns a new byte slice consisting of `count` copies of `b`.
92114
* @param origin The origin bytes
93115
* @param count The count you want to repeat.
116+
* @throws `RangeError` When count is negative
94117
*/
95118
export function repeat(origin: Uint8Array, count: number): Uint8Array {
96119
if (count === 0) {
97120
return new Uint8Array();
98121
}
99122

100123
if (count < 0) {
101-
throw new Error("bytes: negative repeat count");
124+
throw new RangeError("bytes: negative repeat count");
102125
} else if ((origin.length * count) / count !== origin.length) {
103126
throw new Error("bytes: repeat count causes overflow");
104127
}
@@ -111,23 +134,31 @@ export function repeat(origin: Uint8Array, count: number): Uint8Array {
111134

112135
const nb = new Uint8Array(origin.length * count);
113136

114-
let bp = copyBytes(origin, nb);
137+
let bp = copy(origin, nb);
115138

116139
for (; bp < nb.length; bp *= 2) {
117-
copyBytes(nb.slice(0, bp), nb, bp);
140+
copy(nb.slice(0, bp), nb, bp);
118141
}
119142

120143
return nb;
121144
}
122145

123-
/** Concatenate two binary arrays and return new one.
124-
* @param origin origin array to concatenate
125-
* @param b array to concatenate with origin
146+
/** Concatenate multiple binary arrays and return new one.
147+
* @param buf binary arrays to concatenate
126148
*/
127-
export function concat(origin: Uint8Array, b: Uint8Array): Uint8Array {
128-
const output = new Uint8Array(origin.length + b.length);
129-
output.set(origin, 0);
130-
output.set(b, origin.length);
149+
export function concat(...buf: Uint8Array[]): Uint8Array {
150+
let length = 0;
151+
for (const b of buf) {
152+
length += b.length;
153+
}
154+
155+
const output = new Uint8Array(length);
156+
let index = 0;
157+
for (const b of buf) {
158+
output.set(b, index);
159+
index += b.length;
160+
}
161+
131162
return output;
132163
}
133164

@@ -136,7 +167,7 @@ export function concat(origin: Uint8Array, b: Uint8Array): Uint8Array {
136167
* @param pat patter array
137168
*/
138169
export function contains(source: Uint8Array, pat: Uint8Array): boolean {
139-
return findIndex(source, pat) != -1;
170+
return indexOf(source, pat) != -1;
140171
}
141172

142173
/**
@@ -148,7 +179,7 @@ export function contains(source: Uint8Array, pat: Uint8Array): boolean {
148179
* @param off Offset into `dst` at which to begin writing values from `src`.
149180
* @return number of bytes copied
150181
*/
151-
export function copyBytes(src: Uint8Array, dst: Uint8Array, off = 0): number {
182+
export function copy(src: Uint8Array, dst: Uint8Array, off = 0): number {
152183
off = Math.max(0, Math.min(off, dst.byteLength));
153184
const dstBytesAvailable = dst.byteLength - off;
154185
if (src.byteLength > dstBytesAvailable) {

0 commit comments

Comments
 (0)