Skip to content

Commit fb4d05a

Browse files
authored
perf: always check data when looking for last char of needle (#180)
* perf: always check data when looking for last char of needle * add another test * fix typo
1 parent 77cab92 commit fb4d05a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

deps/streamsearch/sbmh.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ SBMH.prototype._sbmh_feed = function (data) {
114114
// or until
115115
// the character to look at lies outside the haystack.
116116
while (pos < 0 && pos <= len - needleLength) {
117-
ch = this._sbmh_lookup_char(data, pos + needleLastCharIndex)
117+
ch = data[pos + needleLastCharIndex]
118118

119119
if (
120120
ch === needleLastChar &&

test/streamsearch.test.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { test } = require('node:test')
44
const Streamsearch = require('../deps/streamsearch/sbmh')
55

66
test('streamsearch', async t => {
7-
t.plan(18)
7+
t.plan(19)
88

99
await t.test('should throw an error if the needle is not a String or Buffer', t => {
1010
t.plan(1)
@@ -239,6 +239,34 @@ test('streamsearch', async t => {
239239
s.push(chunks[1])
240240
})
241241

242+
await t.test('should process two chunks with an overflowing needle /2', t => {
243+
t.plan(9)
244+
const expected = [
245+
[false, Buffer.from('t\0\0'), 0, 1],
246+
[false, Buffer.from('eshello'), 0, 7]
247+
]
248+
const needle = 'test'
249+
const s = new Streamsearch(needle)
250+
const chunks = [
251+
Buffer.from('t'),
252+
Buffer.from('eshello')
253+
]
254+
let i = 0
255+
s.on('info', (isMatched, data, start, end) => {
256+
t.assert.deepStrictEqual(isMatched, expected[i][0])
257+
t.assert.deepStrictEqual(data, expected[i][1])
258+
t.assert.deepStrictEqual(start, expected[i][2])
259+
t.assert.deepStrictEqual(end, expected[i][3])
260+
i++
261+
if (i >= 2) {
262+
t.assert.ok('pass')
263+
}
264+
})
265+
266+
s.push(chunks[0])
267+
s.push(chunks[1])
268+
})
269+
242270
await t.test('should process two chunks with a potentially overflowing needle', t => {
243271
t.plan(13)
244272

0 commit comments

Comments
 (0)