Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Jul 5, 2024
1 parent 5df34ea commit 2d76049
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Match } = require("../src/index");
const StreamSearch = require("streamsearch");
const { Bench } = require("tinybench");

const bench = new Bench({ time: 1000 });
const bench = new Bench({ time: 2500 });

const pattern = "example";
const longText =
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ const Match = class extends Writable {
const { buffer, pattern } = this;
const bufferLength = buffer.length;
const patternLength = pattern.length;
const difference = bufferLength - patternLength;

if (bufferLength < patternLength) {
if (difference < 0) {
return;
}

const { table, processedBytes } = this;
const difference = bufferLength - patternLength;
const patternLastIndex = patternLength - 1;
let i = 0;

for (let i = 0; i <= difference; ) {
do {
let j = patternLastIndex;

while (j >= 0 && buffer[i + j] === pattern[j]) {
Expand All @@ -83,7 +84,7 @@ const Match = class extends Writable {
}

i += table[buffer[patternLength + i]];
}
} while (i <= difference);

this.buffer =
patternLength === 1
Expand Down

0 comments on commit 2d76049

Please sign in to comment.