You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`length ` - (Optional) Length of the string. Can be a number, string or a
133
+
function. Use number for statically sized arrays, string to reference
134
+
another variable and function to do some calculation.
135
+
-`zeroTerminated` - (Optional, defaults to `false`) If true, then this parser
136
+
reads until it reaches zero.
137
+
-`greedy` - (Optional, defaults to `false`) If true, then this parser reads
138
+
until it reaches the end of the buffer. Will consume zero-bytes.
139
+
-`stripNull` - (Optional, must be used with `length`) If true, then strip
140
+
null characters from end of the string
131
141
132
142
### buffer(name [,options])
133
-
Parse bytes as a buffer. `name` should consist only of alpha numeric characters and start
134
-
with an alphabet. `options` is an object; following options are available:
135
-
136
-
-`clone` - (Optional, defaults to `false`) By default, `buffer(name [,options])` returns a new buffer which references
137
-
the same memory as the parser input, but offset and cropped by a certain range. If this option is true, input buffer
138
-
will be cloned and a new buffer referncing another memory is returned.
139
-
-`length ` - (either `length` or `readUntil` is required) Length of the buffer. Can be a number, string or a function.
140
-
Use number for statically sized buffers, string to reference another variable and
141
-
function to do some calculation.
142
-
-`readUntil` - (either `length` or `readUntil` is required) If `'eof'`, then this parser
143
-
will read till it reaches end of the `Buffer` object.
144
-
143
+
Parse bytes as a buffer. `name` should consist only of alpha numeric
144
+
characters and start with an alphabet. `options` is an object; following
145
+
options are available:
146
+
147
+
-`clone` - (Optional, defaults to `false`) By default,
148
+
`buffer(name [,options])` returns a new buffer which references the same
149
+
memory as the parser input, but offset and cropped by a certain range. If
150
+
this option is true, input buffer will be cloned and a new buffer referncing
151
+
another memory is returned.
152
+
-`length ` - (either `length` or `readUntil` is required) Length of the
153
+
buffer. Can be a number, string or a function. Use number for statically
154
+
sized buffers, string to reference another variable and function to do some
155
+
calculation.
156
+
-`readUntil` - (either `length` or `readUntil` is required) If `'eof'`, then
157
+
this parser will read till it reaches end of the `Buffer` object.
145
158
146
159
### array(name [,options])
147
-
Parse bytes as an array. `options` is an object; following options are available:
148
-
149
-
-`type` - (Required) Type of the array element. Can be a string or an user defined Parser object.
150
-
If it's a string, you have to choose from [u]int{8, 16, 32}{le, be}.
151
-
-`length` - (either `length`, `lengthInBytes`, or `readUntil` is required) Length of the array. Can be a number, string or a function.
152
-
Use number for statically sized arrays.
153
-
-`lengthInBytes` - (either `length`, `lengthInBytes`, or `readUntil` is required) Length of the array expressed in bytes. Can be a number, string or a function.
154
-
Use number for statically sized arrays.
155
-
-`readUntil` - (either `length`, `lengthInBytes`, or `readUntil` is required) If `'eof'`, then this parser
156
-
reads until the end of `Buffer` object. If function it reads until the function returns true.
160
+
Parse bytes as an array. `options` is an object; following options are
161
+
available:
162
+
163
+
-`type` - (Required) Type of the array element. Can be a string or an user
164
+
defined Parser object. If it's a string, you have to choose from [u]int{8,
165
+
16, 32}{le, be}.
166
+
-`length` - (either `length`, `lengthInBytes`, or `readUntil` is required)
167
+
Length of the array. Can be a number, string or a function. Use number for
168
+
statically sized arrays.
169
+
-`lengthInBytes` - (either `length`, `lengthInBytes`, or `readUntil` is
170
+
required) Length of the array expressed in bytes. Can be a number, string or
171
+
a function. Use number for statically sized arrays.
172
+
-`readUntil` - (either `length`, `lengthInBytes`, or `readUntil` is required)
173
+
If `'eof'`, then this parser reads until the end of `Buffer` object. If
174
+
function it reads until the function returns true.
157
175
158
176
```javascript
159
177
var parser =newParser()
@@ -209,15 +227,17 @@ var parser = new Parser()
209
227
```
210
228
211
229
### choice(name [,options])
212
-
Choose one parser from several choices according to a field value.
213
-
Combining `choice` with `array` is useful for parsing a typical
-`tag` - (Required) The value used to determine which parser to use from the
236
+
`choices` Can be a string pointing to another field or a function.
237
+
-`choices` - (Required) An object which key is an integer and value is the
238
+
parser which is executed when `tag` equals the key value.
239
+
-`defaultChoice` - (Optional) In case of the tag value doesn't match any of
240
+
`choices` use this parser.
221
241
222
242
```javascript
223
243
var parser1 =...;
@@ -246,8 +266,8 @@ Nest a parser in this position. Parse result of the nested parser is stored in t
246
266
Skip parsing for `length` bytes.
247
267
248
268
### endianess(endianess)
249
-
Define what endianess to use in this parser. `endianess` can be either`'little'` or `'big'`.
250
-
The default endianess of `Parser` is set to big-endian.
269
+
Define what endianess to use in this parser. `endianess` can be either
270
+
`'little'` or `'big'`. The default endianess of `Parser` is set to big-endian.
251
271
252
272
```javascript
253
273
var parser =newParser()
@@ -261,7 +281,9 @@ var parser = new Parser()
261
281
```
262
282
263
283
### namely(alias)
264
-
Set an alias to this parser, so there will be an opportunity to refer to it by name in methods like `.array`, `.nest` and `.choice`, instead of requirement to have an instance of it.
284
+
Set an alias to this parser, so there will be an opportunity to refer to it by
285
+
name in methods like `.array`, `.nest` and `.choice`, instead of requirement
286
+
to have an instance of it.
265
287
266
288
Especially, the parser may reference itself:
267
289
@@ -306,11 +328,18 @@ var buffer = new Buffer([ 2,
306
328
parser.parse(buffer);
307
329
```
308
330
309
-
For most of the cases there is almost no difference to the instance-way of referencing, but this method provides the way to parse recursive trees, where each node could reference the node of the same type from the inside.
331
+
For most of the cases there is almost no difference to the instance-way of
332
+
referencing, but this method provides the way to parse recursive trees, where
333
+
each node could reference the node of the same type from the inside.
310
334
311
-
Also, when you reference a parser using its instance twice, the generated code will contain two similar parts of the code included, while with the named approach, it will include a function with a name, and will just call this function for every case of usage.
335
+
Also, when you reference a parser using its instance twice, the generated code
336
+
will contain two similar parts of the code included, while with the named
337
+
approach, it will include a function with a name, and will just call this
338
+
function for every case of usage.
312
339
313
-
NB: This style could lead to circular references and infinite recursion, to avoid this, ensure that every possible path has its end. Also, this recursion is not tail-optimized, so could lead to memory leaks when it goes too deep.
340
+
NB: This style could lead to circular references and infinite recursion, to
341
+
avoid this, ensure that every possible path has its end. Also, this recursion
342
+
is not tail-optimized, so could lead to memory leaks when it goes too deep.
314
343
315
344
An example of referencing other patches:
316
345
@@ -344,9 +373,9 @@ parser.parse(buffer);
344
373
```
345
374
346
375
### compile()
347
-
Compile this parser on-the-fly and cache its result. Usually, there is no need to
348
-
call this method directly, since it's called when `parse(buffer)` is executed
349
-
for the first time.
376
+
Compile this parser on-the-fly and cache its result. Usually, there is no need
377
+
to call this method directly, since it's called when `parse(buffer)` is
378
+
executed for the first time.
350
379
351
380
### getCode()
352
381
Dynamically generates the code for this parser and returns it as a string.
@@ -355,7 +384,8 @@ Usually used for debugging.
355
384
### Common options
356
385
These are common options that can be specified in all parsers.
357
386
358
-
-`formatter` - Function that transforms the parsed value into a more desired form.
387
+
-`formatter` - Function that transforms the parsed value into a more desired
388
+
form.
359
389
```javascript
360
390
var parser =newParser()
361
391
.array('ipv4', {
@@ -365,11 +395,12 @@ These are common options that can be specified in all parsers.
365
395
});
366
396
```
367
397
368
-
-`assert`- Do assertion on the parsed result (useful for checking magic numbers and so on).
369
-
If `assert` is a `string` or `number`, the actual parsed result will be compared with it
370
-
with`===` (strict equality check), and an exception is thrown if they mismatch.
371
-
On the other hand, if`assert` is a function, that function is executed with one argument
372
-
(parsedresult) and if it returns false, an exception is thrown.
398
+
-`assert`- Do assertion on the parsed result (useful for checking magic
399
+
numbers and so on). If`assert` is a `string` or `number`, the actual parsed
400
+
result will be compared with it with`===` (strict equality check), and an
401
+
exception is thrown if they mismatch. On the other hand, if`assert` is a
402
+
function, that function is executed with one argument (parsedresult) and if
403
+
it returns false, an exception is thrown.
373
404
374
405
```javascript
375
406
// simple maginc number validation
@@ -393,7 +424,8 @@ On the other hand, if `assert` is a function, that function is executed with one
393
424
See `example`for more complex examples.
394
425
395
426
## Support
396
-
Please report issues to the [issue tracker](https://github.com/Keichi/binary-parser/issues) if you
397
-
have any difficulties using thismodule, found a bug, or request a newfeature.
427
+
Please report issues to the
428
+
[issue tracker](https://github.com/Keichi/binary-parser/issues) if you have
429
+
any difficulties using thismodule, found a bug, or request a newfeature.
398
430
399
431
Pull requests with fixes and improvements are welcomed!
0 commit comments