https://github.com/jhermsmeier/node-buffer-horspool
A simple implementation of the Boyer-Moore-Horspool string search algorithm for use with buffers or typed arrays
https://github.com/jhermsmeier/node-buffer-horspool
boyer-moore-horspool nodejs search-algorithm
Last synced: 3 months ago
JSON representation
A simple implementation of the Boyer-Moore-Horspool string search algorithm for use with buffers or typed arrays
- Host: GitHub
- URL: https://github.com/jhermsmeier/node-buffer-horspool
- Owner: jhermsmeier
- License: mit
- Created: 2021-10-15T16:29:25.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-05T12:08:29.000Z (over 3 years ago)
- Last Synced: 2024-04-25T02:42:45.853Z (about 1 year ago)
- Topics: boyer-moore-horspool, nodejs, search-algorithm
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Buffer-Horspool
[](https://npmjs.com/package/buffer-horspool)
[](https://npmjs.com/package/buffer-horspool)
[](https://npmjs.com/package/buffer-horspool)## Note
Node's builtin [`buffer.indexOf()`] and [typed array `.indexOf()`] are way
faster than this nowadays – this was originally written before either of those
existed, so you probably don't want to use this, unless you know you need it.[`buffer.indexOf()`]: https://nodejs.org/api/buffer.html#buffer_buf_indexof_value_byteoffset_encoding
[typed array `.indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/indexOf## What is this?
A simple implementation of the [Boyer-Moore-Horspool string search algorithm] for use with `Buffer`s or `Uint8Array`s.
[Boyer-Moore-Horspool string search algorithm]: https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm
## Why is this?
I needed it for one of my projects in the days before `buffer.indexOf()` existed, wrote a [gist](https://gist.github.com/jhermsmeier/2138865),
others kept finding it useful, so now it's on [npm](https://www.npmjs.com/package/buffer-horspool).Shoutout to [@dubiousjim](https://github.com/dubiousjim) for finding a bug in the original gist!
## Installation
```console
$ npm install --save buffer-horspool
```## Usage
```js
var horspool = require( 'buffer-horspool' )
```Just like the native `.indexOf()` methods, given a haystack and a needle,
either the index at which the substring is found is returned, otherwise `-1`:```js
var index = horspool.indexOf( haystack: Buffer|Uint8Array, needle: Buffer|Uint8Array, startOffset: Number )
```