From 7240ad44413a096f6611afbcf356acedd8867fa7 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Thu, 21 Jan 2016 13:51:09 -0700 Subject: [PATCH 1/2] buffer: allow encoding param to collapse Currently the signature is indexOf(val[, byteOffset[, encoding]]) Instead allow indexOf(val[, byteOffset][, encoding]) so that byteOffset does not need to be passed. PR-URL: https://github.com/nodejs/node/pull/4803 Reviewed-By: James M Snell --- lib/buffer.js | 8 ++++++-- test/parallel/test-buffer-indexof.js | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 6514d0eb6dcf5d..881bdc2e1183d1 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -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') { diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index aac5b18f718cb3..83776be80a37ee 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -111,6 +111,11 @@ assert.equal( .indexOf(Buffer('d', 'binary'), 0, 'binary'), 3); +// 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 var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); From 54cd2e1e5e2f67886a78b13dc17473e84b938d16 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Thu, 21 Jan 2016 14:00:59 -0700 Subject: [PATCH 2/2] buffer: properly retrieve binary length of needle If the needle contains an extended latin-1 character then using String::Utf8Length() will be too large and the search will return early. Instead use String::Length() when encoding is BINARY. PR-URL: https://github.com/nodejs/node/pull/4803 Reviewed-By: James M Snell --- src/node_buffer.cc | 4 +++- test/parallel/test-buffer-indexof.js | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index dca75a817b7414..988e41dbc9aa22 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -849,7 +849,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; - 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) { diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index 83776be80a37ee..b075c3a10d8f4c 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -109,6 +109,15 @@ 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