Skip to content

Rename skip() to seek() #122

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

Merged
merged 1 commit into from
Jun 13, 2019
Merged
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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ pointed by another field.
input buffer. Can be a number, string or a function.

### saveOffset(name [,options])
Save the current buffer offset as key `name`. This function is only useful when called after another function which would advance the internal buffer offset.
Save the current buffer offset as key `name`. This function is only useful
when called after another function which would advance the internal buffer
offset.

```javascript
var parser = new Parser()
Expand All @@ -304,14 +306,15 @@ var parser = new Parser()
.saveOffset('currentOffset')
// finally, use the saved offset to figure out
// how many bytes we need to skip
.skip(function() {
.seek(function() {
return this.seekOffset - this.currentOffset;
})
... // the parser would continue here
```

### skip(length)
Skip `length` bytes.
### seek(relOffset)
Move the buffer offset for `relOffset` bytes from the current position. Use a
negative `relOffset` value to rewind the offset. Previously named `skip(length)`.

### endianess(endianess)
Define what endianess to use in this parser. `endianess` can be either
Expand Down
2 changes: 1 addition & 1 deletion example/elf32.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var ELF32SectionHeaderTable = new Parser().array('items', {
},
});

var ELF32SectionHeaderStringTable = new Parser().skip(1).array('items', {
var ELF32SectionHeaderStringTable = new Parser().seek(1).array('items', {
type: new Parser().string('name', { zeroTerminated: true }),
lengthInBytes: function(vars) {
var shstr = vars.section_headers.items[vars.shstrndx];
Expand Down
2 changes: 1 addition & 1 deletion example/jpg.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var SOF0 = Parser.start()
var Ignore = Parser.start()
.endianess('big')
.uint16('length')
.skip(function() {
.seek(function() {
return this.length - 2;
});

Expand Down
2 changes: 1 addition & 1 deletion example/mbr.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var partitionTable = new Parser()
.uint32le('endLBA');

var mbrParser = new Parser()
.skip(446)
.seek(446)
.array('partitionTables', {
type: partitionTable,
length: 4,
Expand Down
4 changes: 2 additions & 2 deletions example/tar.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ var tarHeader = new Parser()
.string('devmajor', { length: 8, stripNull: true, formatter: oct2int })
.string('devminor', { length: 8, stripNull: true, formatter: oct2int })
.string('prefix', { length: 155, stripNull: true })
.skip(12);
.seek(12);

var tarItem = new Parser()
.nest({
type: tarHeader,
})
.skip(function() {
.seek(function() {
return Math.ceil(this.size / 512) * 512;
});

Expand Down
24 changes: 14 additions & 10 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ComplexTypes =
| 'array'
| 'choice'
| 'nest'
| 'skip'
| 'seek'
| 'pointer'
| 'saveOffset'
| '';
Expand Down Expand Up @@ -150,7 +150,7 @@ const CAPITILIZED_TYPE_NAMES: { [key in Types]: string } = {
array: 'Array',
choice: 'Choice',
nest: 'Nest',
skip: 'Skip',
seek: 'Seek',
pointer: 'Pointer',
saveOffset: 'SaveOffset',
'': '',
Expand Down Expand Up @@ -402,12 +402,16 @@ export class Parser {
return this;
}

skip(length: number, options: ParserOptions) {
skip(length: number, options?: ParserOptions) {
return this.seek(length, options);
}

seek(relOffset: number, options?: ParserOptions) {
if (options && options.assert) {
throw new Error('assert option on skip is not allowed.');
throw new Error('assert option on seek is not allowed.');
}

return this.setNextParser('skip', '', { length: length });
return this.setNextParser('seek', '', { length: relOffset });
}

string(varName: string, options: ParserOptions) {
Expand Down Expand Up @@ -675,8 +679,8 @@ export class Parser {
}
size = this.options.length * elementSize;

// if this a skip
} else if (this.type === 'skip') {
// if this a seek
} else if (this.type === 'seek') {
size = this.options.length;

// if this is a nested parser
Expand Down Expand Up @@ -753,8 +757,8 @@ export class Parser {
case 'buffer':
this.generateBuffer(ctx);
break;
case 'skip':
this.generateSkip(ctx);
case 'seek':
this.generateSeek(ctx);
break;
case 'nest':
this.generateNest(ctx);
Expand Down Expand Up @@ -874,7 +878,7 @@ export class Parser {
}
}

private generateSkip(ctx: Context) {
private generateSeek(ctx: Context) {
const length = ctx.generateOption(this.options.length);
ctx.pushCode(`offset += ${length};`);
}
Expand Down
2 changes: 1 addition & 1 deletion test/composite_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ describe('Composite parser', function() {
.int8('a')
.int32le('b')
.string('msg', { length: 10 })
.skip(2)
.seek(2)
.array('data', {
length: 3,
type: 'int8',
Expand Down
4 changes: 2 additions & 2 deletions test/primitive_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ describe('Primitive parser', function() {
big: 12345678,
});
});
it('should skip when specified', function() {
it('should seek offset', function() {
var parser = Parser.start()
.uint8('a')
.skip(3)
.seek(3)
.uint16le('b')
.uint32be('c');

Expand Down