|
1 | 1 | JS and WASM implementations of https://github.com/srijs/rust-gearhash |
2 | 2 |
|
3 | | -Using [AssemblyScript](https://www.assemblyscript.org/) to generate a lean WASM. |
| 3 | +Using [AssemblyScript](https://www.assemblyscript.org/) to generate a lean WASM. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```javascript |
| 8 | +import { nextMatch } from '@huggingface/gearhash-wasm'; |
| 9 | + |
| 10 | +// Create a Uint8Array of data to search through |
| 11 | +const data = new Uint8Array(1000000); // Example: 1MB of data |
| 12 | +// ... fill data with your content ... |
| 13 | + |
| 14 | +// Search for a pattern with a specific mask |
| 15 | +const mask = 0x0000d90003530000n; // Example mask as a BigInt |
| 16 | +const matchResult = nextMatch(data, mask); |
| 17 | + |
| 18 | +// matchIndex will be the position where the pattern was found |
| 19 | +// or -1 if no match was found |
| 20 | +``` |
| 21 | + |
| 22 | +The `nextMatch` function takes two parameters: |
| 23 | +- `data`: A Uint8Array containing the data to search through |
| 24 | +- `mask`: A BigInt representing the pattern mask to search for |
| 25 | + |
| 26 | +The function returns an object with the `position` (i32) and `hash` (u64) properties |
| 27 | + |
| 28 | +You can continuously feed data like this: |
| 29 | + |
| 30 | +```javascript |
| 31 | +let hash = 0n; |
| 32 | +const mask = 0x0000d90003530000n; |
| 33 | + |
| 34 | +let position = 0; |
| 35 | +for await (const chunk of dataSource) { |
| 36 | + let index = 0; |
| 37 | + while (1) { |
| 38 | + let match = nextMatch(chunk.subArray(index), mask, hash); |
| 39 | + |
| 40 | + if (match.position !== -1) { |
| 41 | + console.log({ |
| 42 | + position: match.position + position, |
| 43 | + hash: match.hash |
| 44 | + }) |
| 45 | + |
| 46 | + index += match.position; |
| 47 | + position = 0; |
| 48 | + hash = 0n; |
| 49 | + } else { |
| 50 | + position += chunk.length - index; |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +console.log(position, "bytes without a match, ending hash: ", hash); |
| 57 | +``` |
0 commit comments