diff --git a/README.md b/README.md index aeb1f7f..0853723 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,18 @@ The constructor may throw: - `matches`: Returns the number of matches found. +#### Callback Parameters + +- `isMatch` (boolean): Indicates whether a match was found. +- `startIndex` (number): The start index of the data that doesn't contain the pattern. +- `endIndex` (number): The end index (exclusive) of the data that doesn't contain the pattern. +- `lookbehindBuffer` (Uint8Array | null): Buffer containing data from previous chunks that might be part of a match. +- `currentBuffer` (Uint8Array | null): The current buffer being processed. + +Note: + +- The callback will contain EITHER the `lookbehindBuffer` OR the `currentBuffer`, not both at the same time. + ## Usage ```js @@ -52,11 +64,11 @@ import { Match } from "gmatch"; const matcher = new Match( "example", - (isMatch, bufferIndex, matchIndex, lookbehind, currentBuffer) => { + (isMatch, startIndex, endIndex, lookbehindBuffer, currentBuffer) => { if (isMatch) { - console.log(`Match found at index: ${matchIndex}`); + console.log(`Match found at index: ${endIndex}`); } else { - console.log(`Processed ${matchIndex - bufferIndex} bytes`); + console.log(`Processed ${endIndex - startIndex} bytes`); } }, ); @@ -89,6 +101,6 @@ gmatch matches: 12 streamsearch matches: 12 ``` -## Acknowledgment +## Acknowledgments Inspired by the excellent streamsearch package, both of which implement [FooBarWidget's streaming Boyer-Moore-Horspool algorithm](https://github.com/FooBarWidget/boyer-moore-horspool/blob/10e25ed66f7184a982fbe9239a8f46ac4969643c/StreamBoyerMooreHorspool.h). diff --git a/src/index.js b/src/index.js index 1574732..9a8bdd5 100644 --- a/src/index.js +++ b/src/index.js @@ -110,7 +110,7 @@ const Match = class { if (index < 0) { const bytesToCutOff = this.#lookbehindSize + index; - if (bytesToCutOff > 0) { + if (bytesToCutOff) { this.#callback(false, 0, bytesToCutOff, this.#lookbehind, null); } @@ -152,6 +152,13 @@ const Match = class { index += this.#skip[char]; } + while ( + index < buffer.length && + !bufferCompare(buffer, index, this.#pattern, 0, buffer.length - index) + ) { + ++index; + } + if (index < buffer.length) { this.#lookbehind.set(buffer.subarray(index)); this.#lookbehindSize = buffer.length - index; diff --git a/test/index.js b/test/index.js index 9a257ca..b7afd8f 100644 --- a/test/index.js +++ b/test/index.js @@ -252,3 +252,41 @@ test("lookbehind test", (t, done) => { match.write("Te"); match.write("failgreat"); }); + +test("pattern test", (t, done) => { + let count = 0; + + const match = new Match("Hello, World!", (isMatch, start, end, l) => { + ++count; + + if (count === 1) { + assert.strictEqual(isMatch, false); + assert.strictEqual(start, 0); + assert.strictEqual(end, 1); + assert.strictEqual(l, null); + done(); + } + }); + + match.write("sHello, "); + match.write("World!"); +}); + +test("pattern test", (t, done) => { + let count = 0; + + const match = new Match("Hello, World!", (isMatch, start, end, l) => { + ++count; + + if (count === 1) { + assert.strictEqual(isMatch, false); + assert.strictEqual(start, 0); + assert.strictEqual(end, 1); + assert.strictEqual(l, null); + done(); + } + }); + + match.write("sHello, "); + match.write("World!"); +});