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.
A simple implementation of the Boyer-Moore-Horspool string search algorithm for use with Buffer
s or Uint8Array
s.
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!
$ npm install --save buffer-horspool
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 )