diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 90c5ee9d7797ca..dccd6faeef5330 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -827,9 +827,9 @@ void IndexOfString(const FunctionCallbackInfo& args) { Local needle = args[1].As(); const char* haystack = ts_obj_data; const size_t haystack_length = ts_obj_length; - // Extended latin-1 characters are 2 bytes in Utf8. + const size_t needle_length = - enc == BINARY ? needle->Length() : needle->Utf8Length(); + StringBytes::Size(args.GetIsolate(), needle, enc); if (needle_length == 0 || haystack_length == 0) { diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index 999d6590a9c69d..b085b4b1ea66ff 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -233,6 +233,12 @@ var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2'); assert.equal(-1, allCharsBufferUtf8.indexOf('notfound')); assert.equal(-1, allCharsBufferUcs2.indexOf('notfound')); +// Needle is longer than haystack, but only because it's encoded as UTF-16 +assert.strictEqual(new Buffer('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1); + +assert.strictEqual(new Buffer('aaaa').indexOf('a'.repeat(4), 'utf8'), 0); +assert.strictEqual(new Buffer('aaaa').indexOf('你好', 'ucs2'), -1); + { // Find substrings in Utf8. const lengths = [1, 3, 15]; // Single char, simple and complex.