Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add readSkip and writeSkip methods #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,38 @@ buff.writeOffset = 10;
console.log(buff.writeOffset) // 10
```

### buff.readSkip(length)
- ```length``` *{number}* The length of data to skip.

Skips reading for a specified `length` from the current read offset.

This method is just sugar for incrementing the read offset in a chainable way.

Examples:
```javascript
buff.readOffset = 5;

buff.readSkip(10).readString(4);

console.log(buff.readOffset) // 19
```

### buff.writeSkip(length)
- ```length``` *{number}* The length of data to skip.

Skips writing for a specified `length` from the current write offset. The buffer will be zero-padded for the skipped range.

This method is just sugar for incrementing the write offset in a chainable way.

Examples:
```javascript
buff.writeOffset = 5;

buff.writeSkip(10).writeString('cool');

console.log(buff.writeOffset) // 19
```

### buff.encoding
### buff.encoding(encoding)
- ```encoding``` *{string}* The new string encoding to set.
Expand Down
31 changes: 31 additions & 0 deletions src/smartbuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,37 @@ class SmartBuffer {
return this;
}

/**
* Skips reading for a specified `length` from the current read position.
*
* This method is just sugar for incrementing the read offset in a chainable way.
*
* @param length { Number } The length of data to skip.
*
* @return this
*/
readSkip(length: number): SmartBuffer {
checkLengthValue(length);
this.readOffset += length;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I had this so that it would be more similar to writeSkip:

Suggested change
this.readOffset += length;
this.readBuffer(length); // Ignore the returned buffer value

But it did not throw an error if length went beyond the bounds of the buffer, so I changed it.

Let me know if you would prefer it to not throw an error though.

return this;
}

/**
* Skips writing for a specified `length` from the current write position.
* The buffer will be zero-padded for the skipped range.
*
* This method is just sugar for incrementing the write offset in a chainable way.
*
* @param length { Number } The length of data to skip.
*
* @return this
*/
writeSkip(length: number): SmartBuffer {
checkLengthValue(length);
this.writeBuffer(Buffer.alloc(length));
return this;
}

/**
* Clears the SmartBuffer instance to its original empty state.
*/
Expand Down
68 changes: 52 additions & 16 deletions test/smartbuffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,26 +614,44 @@ describe('Reading/Writing To/From SmartBuffer', () => {
});

describe('Skipping around data', () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to explain the diff noise in this file:

There was already this Skipping around data test block that had tests manipulating the readOffset to skip. I replaced this block's tests with ones for readSkip/writeSkip, and moved the old tests* to the Setting write and read offsets block, since that felt like the more appropriate place for them.

* Actually, in the case of the Should throw an error when attempting to skip more bytes than actually exist test, I removed it completely because you already had other tests that covered it.

let writer = new SmartBuffer();
writer.writeStringNT('hello');
writer.writeUInt16LE(6699);
writer.writeStringNT('world!');
it('Should skip writing for a given length', () => {
const skipLength = 4;

it('Should equal the UInt16 that was written above', () => {
let reader = SmartBuffer.fromBuffer(writer.toBuffer());
reader.readOffset += 6;
assert.strictEqual(reader.readUInt16LE(), 6699);
reader.readOffset = 0;
assert.strictEqual(reader.readStringNT(), 'hello');
reader.readOffset -= 6;
const writer = new SmartBuffer();
writer.writeStringNT('hello');
assert.strictEqual(writer.writeOffset, 6);
writer.writeSkip(skipLength);
assert.strictEqual(writer.writeOffset, 6 + skipLength);

const reader = SmartBuffer.fromBuffer(writer.toBuffer());
assert.strictEqual(reader.readStringNT(), 'hello');
assert.deepEqual(reader.readBuffer(skipLength), Buffer.alloc(skipLength));

assert.throws(() => {
writer.writeSkip(-1);
});
});

it('Should throw an error when attempting to skip more bytes than actually exist.', () => {
let reader = SmartBuffer.fromBuffer(writer.toBuffer());
it('Should skip reading for a given length', () => {
const skipLength = 4;

const writer = new SmartBuffer();
writer.writeStringNT('hello');
writer.writeStringNT('world!');

const reader = SmartBuffer.fromBuffer(writer.toBuffer());
assert.strictEqual(reader.readStringNT(), 'hello');
assert.strictEqual(reader.readOffset, 6);
reader.readSkip(skipLength);
assert.strictEqual(reader.readOffset, 6 + skipLength);
assert.strictEqual(reader.readStringNT(), 'd!');

assert.throws(() => {
reader.readOffset = 10000;
reader.readSkip(-1);
});

assert.throws(() => {
reader.readSkip(1000);
});
});
});
Expand All @@ -652,17 +670,35 @@ describe('Setting write and read offsets', () => {
assert.strictEqual(writer.readOffset, 10);
});

it('should throw an error when given an offset that is out of bounds', () => {
it('should throw an error when given a read offset that is out of bounds', () => {
assert.throws(() => {
writer.readOffset = -1;
});

assert.throws(() => {
writer.readOffset = 1000;
});
});

it('should throw an error when given an offset that is out of bounds', () => {
it('should throw an error when given a write offset that is out of bounds', () => {
assert.throws(() => {
writer.writeOffset = -1;
});

assert.throws(() => {
writer.writeOffset = 1000;
});
});

it('should skip around data', () => {
const reader = SmartBuffer.fromBuffer(writer.toBuffer());
reader.readOffset += 10;
assert.strictEqual(reader.readStringNT(), 'mynameisjosh');
reader.readOffset = 0;
assert.strictEqual(reader.readString(5), 'hello');
reader.readOffset -= 5;
assert.strictEqual(reader.readStringNT(), 'hellotheremynameisjosh');
});
});

describe('Setting encoding', () => {
Expand Down