Skip to content

Commit

Permalink
fix: lookbehind reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Aug 13, 2024
1 parent a7e4a1b commit b2e37aa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,30 @@ 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
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`);
}
},
);
Expand Down Expand Up @@ -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).
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
});

0 comments on commit b2e37aa

Please sign in to comment.