Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,14 @@ function slowIndexOf(buffer, val, byteOffset, encoding) {
}

Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
if (byteOffset > 0x7fffffff)
if (typeof byteOffset === 'string') {
encoding = byteOffset;
byteOffset = 0;
} else if (byteOffset > 0x7fffffff) {
byteOffset = 0x7fffffff;
else if (byteOffset < -0x80000000)
} else if (byteOffset < -0x80000000) {
byteOffset = -0x80000000;
}
byteOffset >>= 0;

if (typeof val === 'string') {
Expand Down
4 changes: 3 additions & 1 deletion src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,9 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
Local<String> needle = args[1].As<String>();
const char* haystack = ts_obj_data;
const size_t haystack_length = ts_obj_length;
const size_t needle_length = needle->Utf8Length();
// Extended latin-1 characters are 2 bytes in Utf8.
const size_t needle_length =
enc == BINARY ? needle->Length() : needle->Utf8Length();


if (needle_length == 0 || haystack_length == 0) {
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-buffer-indexof.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ assert.equal(
assert.equal(
Buffer(b.toString('binary'), 'binary')
.indexOf(Buffer('d', 'binary'), 0, 'binary'), 3);
assert.equal(
Buffer('aa\u00e8aa', 'binary')
.indexOf('\u00e8', 'binary'), 2);
assert.equal(
Buffer('\u00e8', 'binary')
.indexOf('\u00e8', 'binary'), 0);
assert.equal(
Buffer('\u00e8', 'binary')
.indexOf(Buffer('\u00e8', 'binary'), 'binary'), 0);


// test optional offset with passed encoding
assert.equal(new Buffer('aaaa0').indexOf('30', 'hex'), 4);
assert.equal(new Buffer('aaaa00a').indexOf('3030', 'hex'), 4);


// test usc2 encoding
Expand Down