Skip to content

Commit 9a5ce3c

Browse files
committed
doc: add buf.indexOf encoding arg with example
1 parent bf12379 commit 9a5ce3c

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

doc/api/buffer.markdown

+13-4
Original file line numberDiff line numberDiff line change
@@ -490,17 +490,21 @@ Buffer. The method returns a reference to the Buffer so calls can be chained.
490490
console.log(b.toString());
491491
// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
492492

493-
### buf.indexOf(value[, byteOffset])
493+
### buf.indexOf(value[, byteOffset][, encoding])
494494

495495
* `value` String, Buffer or Number
496496
* `byteOffset` Number, Optional, Default: 0
497+
* `encoding` String, Optional, Default: `utf8`
497498
* Return: Number
498499

499500
Operates similar to [`Array#indexOf()`][] in that it returns either the
500501
starting index position of `value` in Buffer or `-1` if the Buffer does not
501-
contain `value`. The `value` can be a String, Buffer or Number. Strings are
502-
interpreted as UTF8. Buffers will use the entire Buffer (to compare a partial
503-
Buffer use [`Buffer#slice()`][]). Numbers can range from 0 to 255.
502+
contain `value`.
503+
The `value` can be a String, Buffer or Number.
504+
Strings are by default interpreted as UTF8.
505+
Buffers will use the entire Buffer (to compare a partial
506+
Buffer use [`Buffer#slice()`][]).
507+
Numbers can range from 0 to 255.
504508

505509
const buf = new Buffer('this is a buffer');
506510

@@ -517,6 +521,11 @@ Buffer use [`Buffer#slice()`][]). Numbers can range from 0 to 255.
517521
buf.indexOf(new Buffer('a buffer example').slice(0,8));
518522
// returns 8
519523

524+
const utf16Buffer = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
525+
526+
assert.equal(4, utf16Buffer.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
527+
assert.equal(6, utf16Buffer.indexOf('\u03a3', -4, 'ucs2'), 'Second Sigma');
528+
520529
### buf.includes(value[, byteOffset][, encoding])
521530

522531
* `value` String, Buffer or Number

0 commit comments

Comments
 (0)