Skip to content

Commit 9503413

Browse files
committed
allow string key for method
1 parent 2beeb13 commit 9503413

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

README.markdown

+8-3
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ console.log("entries", entries);
234234
b.string(key, size)
235235
---------
236236

237-
Read <size> bytes as a utf8 string, or read until end of buffer if size not specified
237+
Read `size` bytes as a utf8 string, or read until end of buffer if size not
238+
specified. Puts the resulting string in the variable stash at `key`.
239+
If `size` is a string, use the value at `vars[size]`. The key follows the same
240+
dotted address rules as the word functions.
238241

239242
``` js
240243
var vars = binary.parse(new Buffer([97, 32, 99, 97, 116, 32, 119, 105, 110, 115]))
@@ -248,7 +251,8 @@ console.log(vars);
248251
b.cstring(key, size)
249252
---------
250253

251-
Read <size> bytes as a null-terminated utf8 string (slices off the null character and anything after it, or last character if no null found)
254+
Same as `string()`, but read as a null-terminated utf8 string (slices off the
255+
null character and anything after it, or last character if no null found)
252256

253257
``` js
254258
var vars = binary.parse(new Buffer([97, 32, 99, 97, 116, 32, 119, 105, 110, 115, 0]))
@@ -262,7 +266,8 @@ console.log(vars);
262266
b.skip(size)
263267
---------
264268

265-
Skip <size> bytes
269+
Skip <size> bytes. If `size` is a string, use the value at `vars[size]`. The
270+
key follows the same dotted address rules as the word functions.
266271

267272
``` js
268273
var vars = binary.parse(new Buffer([5, 13, 80]))

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ exports.parse = function parse (buffer) {
296296
if (size==null){
297297
size=buffer.length;
298298
}
299+
if (typeof size === 'string') {
300+
size = vars.get(size);
301+
} else if (size === undefined || size !== size) {
302+
size = 0;
303+
}
299304
vars.set(name, buffer.toString( 'utf8', offset, Math.min(buffer.length, offset + size)));
300305
offset += size;
301306
return self;

0 commit comments

Comments
 (0)