Skip to content

Commit e3d0993

Browse files
committed
feat: add indexOf method and test cases.
1 parent 38b7c64 commit e3d0993

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

src/dynamicBuffer.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,49 @@ export class DynamicBuffer {
214214
return this.buffer[offset];
215215
}
216216

217+
/**
218+
* Gets the first index at which the given value can be found in the buffer, or `-1` if it is
219+
* not present.
220+
*
221+
* ```js
222+
* buf.append('Hello world');
223+
* console.log(buf.indexOf('world'));
224+
* // 6
225+
* console.log(buf.indexOf('not in buffer'));
226+
* // -1
227+
* ```
228+
*
229+
* @param value The value what to search for.
230+
* @param byteOffset Where to begin searching in the buffer, and it'll be calculated from the
231+
* end of buffer if it's negative. Default `0`.
232+
* @param encoding The character encoding if the value is a string, default 'utf8'.
233+
* @returns The index of first occurrence of value in the buffer, and `-1` if the buffer does
234+
* not contain this value.
235+
*/
236+
indexOf(
237+
value: string | Buffer | Uint8Array | number | DynamicBuffer,
238+
byteOffset: number = 0,
239+
encoding: BufferEncoding = 'utf8',
240+
) {
241+
let search: string | Buffer | Uint8Array | number;
242+
if (value instanceof DynamicBuffer) {
243+
search = value.buffer?.subarray(0, value.length) || '';
244+
} else {
245+
search = value;
246+
}
247+
248+
if (!this.buffer || this.length === 0) {
249+
return (typeof search === 'object' || typeof search === 'string') && search.length === 0 ? 0 : -1;
250+
}
251+
252+
let start = byteOffset >= 0 ? byteOffset : this.length + byteOffset;
253+
if (start >= this.length) {
254+
start = this.length;
255+
}
256+
257+
return this.buffer.subarray(start, this.length).indexOf(search, 0, encoding);
258+
}
259+
217260
/**
218261
* Copies the buffer data onto a new `Buffer` instance without unused parts.
219262
*

test/search.spec.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import assert from 'assert';
2+
import { describe, it } from 'mocha';
3+
4+
import { DynamicBuffer } from '../src';
5+
6+
describe('IndexOf tests', () => {
7+
it('Test indexOf with empty buffer and empty value.', () => {
8+
const buf = new DynamicBuffer();
9+
10+
assert.equal(buf.indexOf('Hello'), -1);
11+
assert.equal(buf.indexOf(''), 0);
12+
assert.equal(buf.indexOf(0), -1);
13+
assert.equal(buf.indexOf(Buffer.alloc(0)), 0);
14+
assert.equal(buf.indexOf(new Uint8Array()), 0);
15+
assert.equal(buf.indexOf(new DynamicBuffer()), 0);
16+
});
17+
18+
it('Test indexOf with empty value.', () => {
19+
const buf = new DynamicBuffer();
20+
buf.append('Hello world');
21+
22+
assert.equal(buf.indexOf(''), 0);
23+
assert.equal(buf.indexOf(0), -1);
24+
assert.equal(buf.indexOf(Buffer.alloc(0)), 0);
25+
assert.equal(buf.indexOf(new Uint8Array()), 0);
26+
assert.equal(buf.indexOf(new DynamicBuffer()), 0);
27+
});
28+
29+
it('Test indexOf with string.', () => {
30+
const buf = new DynamicBuffer();
31+
buf.append('Hello world');
32+
33+
assert.equal(buf.indexOf('Hello'), 0);
34+
assert.equal(buf.indexOf('world'), 6);
35+
assert.equal(buf.indexOf('no'), -1);
36+
});
37+
38+
it('Test indexOf with number.', () => {
39+
const buf = new DynamicBuffer();
40+
buf.append('Hello world');
41+
42+
assert.equal(buf.indexOf(72), 0); // H
43+
assert.equal(buf.indexOf(119), 6); // w
44+
assert.equal(buf.indexOf(97), -1); // a
45+
});
46+
47+
it('Test indexOf with Buffer.', () => {
48+
const buf = new DynamicBuffer();
49+
buf.append('Hello world');
50+
51+
assert.equal(buf.indexOf(Buffer.from('Hello')), 0);
52+
assert.equal(buf.indexOf(Buffer.from('world')), 6);
53+
assert.equal(buf.indexOf(Buffer.from('no')), -1);
54+
});
55+
56+
it('Test indexOf with Uint8Array.', () => {
57+
const buf = new DynamicBuffer();
58+
buf.append('Hello world');
59+
60+
assert.equal(buf.indexOf(new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f])), 0);
61+
assert.equal(buf.indexOf(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64])), 6);
62+
assert.equal(buf.indexOf(new Uint8Array([0x6e, 0x6c])), -1);
63+
});
64+
65+
it('Test indexOf with DynamicBuffer.', () => {
66+
const buf = new DynamicBuffer();
67+
buf.append('Hello world');
68+
69+
const buf1 = new DynamicBuffer();
70+
buf1.append('Hello');
71+
assert.equal(buf.indexOf(buf1), 0);
72+
73+
const buf2 = new DynamicBuffer();
74+
buf2.append('world');
75+
assert.equal(buf.indexOf(buf2), 6);
76+
77+
const buf3 = new DynamicBuffer();
78+
buf3.append('no');
79+
assert.equal(buf.indexOf(buf3), -1);
80+
});
81+
82+
it('Test indexOf with byteOffset parameter.', () => {
83+
const buf = new DynamicBuffer();
84+
buf.append('Hello world');
85+
86+
assert.equal(buf.indexOf('Hello'), 0);
87+
assert.equal(buf.indexOf('Hello', 0), 0);
88+
assert.equal(buf.indexOf('Hello', 1), -1);
89+
assert.equal(buf.indexOf('Hello', -1), -1);
90+
assert.equal(buf.indexOf('Hello', -11), 0); // -11 equals 0
91+
assert.equal(buf.indexOf('Hello', 32), -1);
92+
assert.equal(buf.indexOf('', 32), 0);
93+
});
94+
});

0 commit comments

Comments
 (0)