Skip to content

Commit

Permalink
fix: lower lookbehind size to needleLength - 1 (#175)
Browse files Browse the repository at this point in the history
* lower lookbehind length

* use needleLastCharIndex
  • Loading branch information
gurgunday authored Dec 2, 2024
1 parent 74c3e23 commit eece7f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions deps/streamsearch/sbmh.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function SBMH (needle) {
}

const needleLength = needle.length
const needleLastCharIndex = needleLength - 1

if (needleLength === 0) {
throw new Error('The needle cannot be an empty String/Buffer.')
Expand All @@ -58,12 +59,12 @@ function SBMH (needle) {
this._needle = needle
this._bufpos = 0

this._lookbehind = Buffer.alloc(needleLength)
this._lookbehind = Buffer.alloc(needleLastCharIndex)

// Populate occurrence table with analysis of the needle,
// ignoring last letter.
for (var i = 0; i < needleLength - 1; ++i) { // eslint-disable-line no-var
this._occ[needle[i]] = needleLength - 1 - i
for (var i = 0; i < needleLastCharIndex; ++i) { // eslint-disable-line no-var
this._occ[needle[i]] = needleLastCharIndex - i
}
}
inherits(SBMH, EventEmitter)
Expand Down
8 changes: 4 additions & 4 deletions test/streamsearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ test('streamsearch', async t => {

const expected = [
[false, Buffer.from('bar\r'), 0, 3],
[false, Buffer.from('\r\0\0'), 0, 1],
[false, Buffer.from('\r\0'), 0, 1],
[false, Buffer.from('\n\r\nhello'), 0, 8]
]
const needle = '\r\n\n'
Expand Down Expand Up @@ -339,7 +339,7 @@ test('streamsearch', async t => {
t.plan(13)

const expected = [
[false, Buffer.from('\n\n\0'), 0, 1],
[false, Buffer.from('\n\n'), 0, 1],
[true, undefined, undefined, undefined],
[false, Buffer.from('\r\nhello'), 1, 7]
]
Expand Down Expand Up @@ -374,8 +374,8 @@ test('streamsearch', async t => {

const expected = [
[false, Buffer.from('bar\r'), 0, 3],
[false, Buffer.from('\r\n\0'), 0, 2],
[false, Buffer.from('\r\n\0'), 0, 1],
[false, Buffer.from('\r\n'), 0, 2],
[false, Buffer.from('\r\n'), 0, 1],
[false, Buffer.from('hello'), 0, 5]
]
const needle = '\r\n\n'
Expand Down

0 comments on commit eece7f3

Please # to comment.