Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

end the indexOf search when there are not enough chars left #6

Open
wants to merge 1 commit into from

1 participant

@ekashida

An optimization to end the search early if we don't need to look any further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 6 deletions.
  1. +6 −6 index.js
  2. +4 −0 test/buffers.js
View
12 index.js
@@ -220,14 +220,14 @@ Buffers.prototype.indexOf = function (needle, offset) {
// for each character in virtual buffer
for (;;) {
- while (j >= this.buffers[i].length) {
+ // if more chars in the search string than are remaining in the buffer
+ if (match === 0 && needle.length > this.length - pos) {
+ return -1;
+ }
+
+ if (j >= this.buffers[i].length) {
j = 0;
i++;
-
- if (i >= this.buffers.length) {
- // search string not found
- return -1;
- }
}
var char = this.buffers[i][j];
View
4 test/buffers.js
@@ -221,6 +221,10 @@ test('indexOf', function (t) {
t.equal( bufs.indexOf("ello", 2), -1 );
t.equal( bufs.indexOf("e"), 1 );
t.equal( bufs.indexOf("e", 2), 13 );
+ t.equal( bufs.indexOf("u?"), 17 );
+ t.equal( bufs.indexOf("u?", 4), 17 );
+ t.equal( bufs.indexOf("?"), 18 );
+ t.equal( bufs.indexOf("?", 4), 18 );
t.equal( bufs.indexOf(new Buffer([0x65]), 2), 13 );
t.end();
});
Something went wrong with that request. Please try again.