Skip to content

Commit 2dde0f0

Browse files
skomskirvagg
authored andcommitted
doc: add buf.indexOf encoding param with example
PR-URL: #3373 Reviewed-By: Trevor Norris <[email protected]>
1 parent b2c8b7f commit 2dde0f0

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

doc/api/buffer.markdown

+12-4
Original file line numberDiff line numberDiff line change
@@ -532,17 +532,18 @@ console.log(b.toString());
532532
// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
533533
```
534534

535-
### buf.indexOf(value[, byteOffset])
535+
### buf.indexOf(value[, byteOffset][, encoding])
536536

537537
* `value` String, Buffer or Number
538538
* `byteOffset` Number, Optional, Default: 0
539+
* `encoding` String, Optional, Default: `utf8`
539540
* Return: Number
540541

541542
Operates similar to [`Array#indexOf()`][] in that it returns either the
542543
starting index position of `value` in Buffer or `-1` if the Buffer does not
543-
contain `value`. The `value` can be a String, Buffer or Number. Strings are
544-
interpreted as UTF8. Buffers will use the entire Buffer (to compare a partial
545-
Buffer use [`Buffer#slice()`][]). Numbers can range from 0 to 255.
544+
contain `value`. The `value` can be a String, Buffer or Number. Strings are by
545+
default interpreted as UTF8. Buffers will use the entire Buffer (to compare a
546+
partial Buffer use [`Buffer#slice()`][]). Numbers can range from 0 to 255.
546547

547548
```js
548549
const buf = new Buffer('this is a buffer');
@@ -559,6 +560,13 @@ buf.indexOf(new Buffer('a buffer example'));
559560
// returns -1
560561
buf.indexOf(new Buffer('a buffer example').slice(0,8));
561562
// returns 8
563+
564+
const utf16Buffer = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
565+
566+
utf16Buffer.indexOf('\u03a3', 0, 'ucs2');
567+
// returns 4
568+
utf16Buffer.indexOf('\u03a3', -4, 'ucs2');
569+
// returns 6
562570
```
563571

564572
### buf.includes(value[, byteOffset][, encoding])

0 commit comments

Comments
 (0)