Skip to content

Commit 48a197a

Browse files
committed
feat: add copy method.
1 parent c7194ee commit 48a197a

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

src/dynamicBuffer.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,44 @@ export class DynamicBuffer {
719719
return this;
720720
}
721721

722+
/**
723+
* Copies data from a region of `buf` to a region in `target`.
724+
*
725+
* @param target A buffer to copy into.
726+
* @param targetStart The offset within `target` at which to be begin writing, default 0.
727+
* @param sourceStart The offset within `buf` from which to begin copying, default 0.
728+
* @param sourceEnd The offset within `buf` at which to stop copying (not inclusive),
729+
* default `buf.length`.
730+
* @returns The number of bytes copied.
731+
*/
732+
copy(
733+
target: DynamicBuffer | Buffer | Uint8Array,
734+
targetStart: number = 0,
735+
sourceStart: number = 0,
736+
sourceEnd: number = this.length,
737+
): number {
738+
if (targetStart < 0) {
739+
throw new RangeError(`The value of 'targetStart' is out of range. It must be >=0. Received ${targetStart}`);
740+
}
741+
if (sourceStart < 0) {
742+
throw new RangeError(`The value of 'sourceStart' is out of range. It must be >=0. Received ${sourceStart}`);
743+
}
744+
if (sourceEnd > this.length) {
745+
// eslint-disable-next-line no-param-reassign
746+
sourceEnd = this.length;
747+
}
748+
749+
if (target instanceof DynamicBuffer) {
750+
return target.write(this.toString(this.encoding, sourceStart, sourceEnd), targetStart);
751+
}
752+
753+
if (!this.buffer || this.length === 0) {
754+
return 0;
755+
}
756+
757+
return this.buffer.copy(target, targetStart, sourceStart, sourceEnd);
758+
}
759+
722760
/**
723761
* Write data into internal buffer with the specified offset.
724762
*

test/export.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,69 @@ describe('Exports to JSON object', () => {
113113
assert.equal(JSON.stringify(buf), '{"type":"Buffer","data":[72,101,108,108,111]}');
114114
});
115115
});
116+
117+
describe('Copy to another storage', () => {
118+
it('Test copy()', () => {
119+
const str = 'hello world';
120+
const buf1 = new DynamicBuffer(str);
121+
const buf2 = new DynamicBuffer();
122+
123+
assert.equal(buf1.copy(buf2), str.length);
124+
assert.equal(buf2.toString(), 'hello world');
125+
});
126+
127+
it('Test copy() with builtin Buffer', () => {
128+
const str = 'hello world';
129+
const buf1 = new DynamicBuffer(str);
130+
const buf2 = Buffer.alloc(15, '.');
131+
132+
assert.equal(buf1.copy(buf2), str.length);
133+
assert.equal(buf2.toString(), 'hello world....');
134+
});
135+
136+
it('Test copy() with builtin Uint8Array', () => {
137+
const str = 'hello world';
138+
const buf1 = new DynamicBuffer(str);
139+
const buf2 = new Uint8Array(str.length);
140+
141+
assert.equal(buf1.copy(buf2), str.length);
142+
for (let i = 0; i < buf2.length; i += 1) {
143+
assert.equal(buf2[i], str.charCodeAt(i));
144+
}
145+
});
146+
147+
it('Test copy() with invalid target start', () => {
148+
const buf1 = new DynamicBuffer('hello world');
149+
const buf2 = new DynamicBuffer();
150+
151+
assert.throws(() => {
152+
buf1.copy(buf2, -1);
153+
});
154+
});
155+
156+
it('Test copy() with invalid source start', () => {
157+
const buf1 = new DynamicBuffer('hello world');
158+
const buf2 = new DynamicBuffer();
159+
160+
assert.throws(() => {
161+
buf1.copy(buf2, 0, -1);
162+
});
163+
});
164+
165+
it('Test copy() with big source end', () => {
166+
const str = '!hello world';
167+
const buf1 = new DynamicBuffer(str);
168+
const buf2 = new DynamicBuffer('...');
169+
170+
assert.equal(buf1.copy(buf2, 1, 1, 100), str.length - 1);
171+
assert.equal(buf2.toString(), '.hello world');
172+
});
173+
174+
it('Test copy() with empty buffer', () => {
175+
const buf1 = new DynamicBuffer();
176+
const buf2 = Buffer.alloc(5, '.');
177+
178+
assert.equal(buf1.copy(buf2), 0);
179+
assert.equal(buf2.toString(), '.....');
180+
});
181+
});

0 commit comments

Comments
 (0)