Skip to content

Commit

Permalink
perf: refactor parts of StreamSearch (#148)
Browse files Browse the repository at this point in the history
* move to class

* format

* small refactor

* simplify check

* space

* simplify check

* simplify check

* refactor

* simplify

* put back license

* remove duplicate subtraction

* refactor

* readability

* revert class refactor

* remove unnecessary subtraction

* simplify

* imports
  • Loading branch information
gurgunday committed Mar 16, 2024
1 parent 906dde8 commit 0740e48
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions deps/streamsearch/sbmh.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
* Based heavily on the Streaming Boyer-Moore-Horspool C++ implementation
* by Hongli Lai at: https://github.com/FooBarWidget/boyer-moore-horspool
*/
const EventEmitter = require('node:events').EventEmitter
const inherits = require('node:util').inherits

const { EventEmitter } = require('node:events')
const { inherits } = require('node:util')

function SBMH (needle) {
if (typeof needle === 'string') {
Expand Down Expand Up @@ -154,9 +155,8 @@ SBMH.prototype._sbmh_feed = function (data) {
this.emit('info', false, this._lookbehind, 0, bytesToCutOff)
}

this._lookbehind.copy(this._lookbehind, 0, bytesToCutOff,
this._lookbehind_size - bytesToCutOff)
this._lookbehind_size -= bytesToCutOff
this._lookbehind.copy(this._lookbehind, 0, bytesToCutOff, this._lookbehind_size)

data.copy(this._lookbehind, this._lookbehind_size)
this._lookbehind_size += len
Expand All @@ -166,20 +166,18 @@ SBMH.prototype._sbmh_feed = function (data) {
}
}

pos += (pos >= 0) * this._bufpos

// Lookbehind buffer is now empty. We only need to check if the
// needle is in the haystack.
if (data.indexOf(needle, pos) !== -1) {
pos = data.indexOf(needle, pos)
pos = data.indexOf(needle, pos + ((pos >= 0) * this._bufpos))

if (pos !== -1) {
++this.matches
if (pos > 0) { this.emit('info', true, data, this._bufpos, pos) } else { this.emit('info', true) }

return (this._bufpos = pos + needleLength)
} else {
pos = len - needleLength
}

pos = len - needleLength

// There was no match. If there's trailing haystack data that we cannot
// match yet using the Boyer-Moore-Horspool algorithm (because the trailing
// data is less than the needle size) then match using a modified
Expand All @@ -190,12 +188,10 @@ SBMH.prototype._sbmh_feed = function (data) {
pos < len &&
(
data[pos] !== needle[0] ||
(
(Buffer.compare(
data.subarray(pos, pos + len - pos),
needle.subarray(0, len - pos)
) !== 0)
)
Buffer.compare(
data.subarray(pos, pos + len - pos),
needle.subarray(0, len - pos)
) !== 0
)
) {
++pos
Expand All @@ -213,7 +209,7 @@ SBMH.prototype._sbmh_feed = function (data) {
}

SBMH.prototype._sbmh_lookup_char = function (data, pos) {
return (pos < 0)
return pos < 0
? this._lookbehind[this._lookbehind_size + pos]
: data[pos]
}
Expand Down

0 comments on commit 0740e48

Please sign in to comment.