Skip to content

Commit 7dbf542

Browse files
authored
Merge pull request #111 from keichi/update-docs
Update docs
2 parents 5101915 + 24c5565 commit 7dbf542

File tree

2 files changed

+42
-46
lines changed

2 files changed

+42
-46
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2013-2017 Keichi Takahashi <keichi.t@me.com>
1+
Copyright (c) 2013-2019 Keichi Takahashi <keichi.t@me.com>
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,29 @@ data. Binary-parser dynamically generates and compiles the parser code
1010
on-the-fly, which runs as fast as a hand-written parser (which takes much more
1111
time and effort to write). Supported data types are:
1212

13-
- [Integers](#uint8-16-32-64le-bename-options) (supports 8, 16, 32 and 64bit
14-
signed and unsigned integers)
15-
- [Floating point numbers](#float-doublele-bename-options) (supports 32 and 64
16-
bit floating point values)
17-
- [Bit fields](#bit1-32name-options) (supports bit fields with length from 1
18-
to 32 bits)
19-
- [Strings](#stringname-options) (supports various encodings, fixed-length and
20-
variable-length, zero terminated string)
21-
- [Arrays](#arrayname-options) (supports user-defined element type,
22-
fixed-length and variable-length)
23-
- [Choices](#choicename-options)
13+
- [Integers](#uint8-16-32-64le-bename-options) (8, 16, 32 and 64 bit signed
14+
and unsigned integers)
15+
- [Floating point numbers](#float-doublele-bename-options) (32 and 64 bit
16+
floating point values)
17+
- [Bit fields](#bit1-32name-options) (bit fields with length from 1 to 32
18+
bits)
19+
- [Strings](#stringname-options) (fixed-length, variable-length and zero
20+
terminated strings with various encodings)
21+
- [Arrays](#arrayname-options) (fixed-length and variable-length arrays of
22+
builtin or user-defined element types)
23+
- [Choices](#choicename-options) (supports integer keys)
2424
- [Pointers](#pointername-options)
25-
- User defined types
25+
- User defined types (arbitrary combination of builtin types)
2626

27-
Binary-parser is inspired by [BinData](https://github.com/dmendel/bindata)
27+
Binary-parser was inspired by [BinData](https://github.com/dmendel/bindata)
2828
and [binary](https://github.com/substack/node-binary).
2929

30-
## Installation
31-
32-
```shell
33-
$ npm install binary-parser
34-
```
35-
3630
## Quick Start
37-
1. Create an empty Parser object with `new Parser()`.
38-
2. Chain builder methods to build the desired parser. (See
39-
[API](https://github.com/Keichi/binary-parser#api) for detailed document of
40-
each methods)
41-
3. Call `Parser.prototype.parse` with an `Buffer` object passed as argument.
31+
1. Create an empty Parser object with `new Parser()` or `Parser.start()`.
32+
2. Chain methods to build your desired parser. (See
33+
[API](https://github.com/keichi/binary-parser#api) for detailed document of
34+
each method)
35+
3. Call `Parser.prototype.parse` with an `Buffer` object passed as an argument.
4236
4. Parsed result will be returned as an object.
4337

4438
```javascript
@@ -98,7 +92,7 @@ signed number, with `u` prefixed as an unsigned number. The runtime type
9892
returned by the 8, 16, 32 bit methods is `number` while the type
9993
returned by the 64 bit is `bigint`.
10094

101-
**NOTE:** [u]int64{be,le} methods only work if your runtime is node v12.0.0 or
95+
**Note:** [u]int64{be,le} methods only work if your runtime is node v12.0.0 or
10296
greater. Lower version will throw a runtime error.
10397

10498
```javascript
@@ -119,9 +113,8 @@ methods from `bit1` to `bit32` each corresponding to 1-bit-length to
119113
32-bits-length bit field.
120114

121115
### {float, double}{le, be}(name[, options])
122-
Parse bytes as an floating-point value and store it in a variable named
123-
`name`. `name` should consist only of alphanumeric characters and start with
124-
an alphabet.
116+
Parse bytes as a floating-point value and stores it to a variable named
117+
`name`.
125118

126119
```javascript
127120
var parser = new Parser()
@@ -137,7 +130,7 @@ characters and start with an alphabet. `options` is an object which can have
137130
the following keys:
138131

139132
- `encoding` - (Optional, defaults to `utf8`) Specify which encoding to use.
140-
`"utf8"`, `"ascii"`, `"hex"` and else are valid. See
133+
Supported encodings include `"utf8"`, `"ascii"` and `"hex"`. See
141134
[`Buffer.toString`](http://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end)
142135
for more info.
143136
- `length ` - (Optional) Length of the string. Can be a number, string or a
@@ -204,7 +197,7 @@ var parser = new Parser()
204197
type: "int32",
205198
length: function() {
206199
return this.dataLength - 1;
207-
} // other fields are available through this
200+
} // other fields are available through `this`
208201
})
209202

210203
// Statically sized array
@@ -225,7 +218,7 @@ var parser = new Parser()
225218
type: "int32",
226219
lengthInBytes: function() {
227220
return this.dataLengthInBytes - 4;
228-
} // other fields are available through this
221+
} // other fields are available through `this`
229222
})
230223

231224
// Dynamically sized array (with stop-check on parsed item)
@@ -253,7 +246,7 @@ an object which can have the following keys:
253246
`choices` Can be a string pointing to another field or a function.
254247
- `choices` - (Required) An object which key is an integer and value is the
255248
parser which is executed when `tag` equals the key value.
256-
- `defaultChoice` - (Optional) In case of the tag value doesn't match any of
249+
- `defaultChoice` - (Optional) In case if the tag value doesn't match any of
257250
`choices`, this parser is used.
258251

259252
```javascript
@@ -264,15 +257,15 @@ var parser3 = ...;
264257
var parser = new Parser().uint8("tagValue").choice("data", {
265258
tag: "tagValue",
266259
choices: {
267-
1: parser1, // When tagValue == 1, execute parser1
268-
4: parser2, // When tagValue == 4, execute parser2
269-
5: parser3 // When tagValue == 5, execute parser3
260+
1: parser1, // if tagValue == 1, execute parser1
261+
4: parser2, // if tagValue == 4, execute parser2
262+
5: parser3 // if tagValue == 5, execute parser3
270263
}
271264
});
272265
```
273266

274267
Combining `choice` with `array` is an idiom to parse
275-
[TLV](http://en.wikipedia.org/wiki/Type-length-value)-based formats.
268+
[TLV](http://en.wikipedia.org/wiki/Type-length-value)-based binary formats.
276269

277270
### nest([name,] options)
278271
Execute an inner parser and store its result to key `name`. If `name` is null
@@ -283,14 +276,16 @@ current object. `options` is an object which can have the following keys:
283276

284277
### pointer(name [,options])
285278
Jump to `offset`, execute parser for `type` and rewind to previous offset.
279+
Useful for parsing binary formats such as ELF where the offset of a field is
280+
pointed by another field.
286281

287-
- `type` - (Required) A `Parser` object.
288-
- `offset` - (Required) Note that this indicates absolute offset from the
289-
start of the input buffer. Can be a string `[u]int{8, 16, 32, 64}{le, be}`
290-
or an user defined Parser object.
282+
- `type` - (Required) Can be a string `[u]int{8, 16, 32, 64}{le, be}`
283+
or an user defined Parser object.
284+
- `offset` - (Required) Indicates absolute offset from the beginning of the
285+
input buffer. Can be a number, string or a function.
291286

292287
### skip(length)
293-
Skip parsing for `length` bytes.
288+
Skip `length` bytes.
294289

295290
### endianess(endianess)
296291
Define what endianess to use in this parser. `endianess` can be either
@@ -368,9 +363,10 @@ will contain two similar parts of the code included, while with the named
368363
approach, it will include a function with a name, and will just call this
369364
function for every case of usage.
370365

371-
NB: This style could lead to circular references and infinite recursion, to
372-
avoid this, ensure that every possible path has its end. Also, this recursion
373-
is not tail-optimized, so could lead to memory leaks when it goes too deep.
366+
**Note**: This style could lead to circular references and infinite recursion,
367+
to avoid this, ensure that every possible path has its end. Also, this
368+
recursion is not tail-optimized, so could lead to memory leaks when it goes
369+
too deep.
374370

375371
An example of referencing other patches:
376372

@@ -407,7 +403,7 @@ executed for the first time.
407403

408404
### getCode()
409405
Dynamically generates the code for this parser and returns it as a string.
410-
Usually used for debugging.
406+
Useful for debugging the generated code.
411407

412408
### Common options
413409
These options can be used in all parsers.

0 commit comments

Comments
 (0)