buffer-horspool

A simple implementation of the Boyer-Moore-Horspool string search algorithm for use with buffers or typed arrays

Usage no npm install needed!

<script type="module">
  import bufferHorspool from 'https://cdn.skypack.dev/buffer-horspool';
</script>

README

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.

What is this?

A simple implementation of the Boyer-Moore-Horspool string search algorithm for use with Buffers or Uint8Arrays.

Why is this?

I needed it for one of my projects in the days before buffer.indexOf() existed, wrote a gist, others kept finding it useful, so now it's on npm.

Shoutout to @dubiousjim for finding a bug in the original gist!

Installation

$ npm install --save buffer-horspool

Usage

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:

var index = horspool.indexOf( haystack: Buffer|Uint8Array, needle: Buffer|Uint8Array, startOffset: Number )