Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: always check data when looking for last char of needle #180

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deps/streamsearch/sbmh.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ SBMH.prototype._sbmh_feed = function (data) {
// or until
// the character to look at lies outside the haystack.
while (pos < 0 && pos <= len - needleLength) {
ch = this._sbmh_lookup_char(data, pos + needleLastCharIndex)
ch = data[pos + needleLastCharIndex]

if (
ch === needleLastChar &&
Expand Down
30 changes: 29 additions & 1 deletion test/streamsearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { test } = require('node:test')
const Streamsearch = require('../deps/streamsearch/sbmh')

test('streamsearch', async t => {
t.plan(18)
t.plan(19)

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

await t.test('should process two chunks with an overflowing needle /2', t => {
t.plan(9)
const expected = [
[false, Buffer.from('t\0\0'), 0, 1],
[false, Buffer.from('eshello'), 0, 7]
]
const needle = 'test'
const s = new Streamsearch(needle)
const chunks = [
Buffer.from('t'),
Buffer.from('eshello')
]
let i = 0
s.on('info', (isMatched, data, start, end) => {
t.assert.deepStrictEqual(isMatched, expected[i][0])
t.assert.deepStrictEqual(data, expected[i][1])
t.assert.deepStrictEqual(start, expected[i][2])
t.assert.deepStrictEqual(end, expected[i][3])
i++
if (i >= 2) {
t.assert.ok('pass')
}
})

s.push(chunks[0])
s.push(chunks[1])
})

await t.test('should process two chunks with a potentially overflowing needle', t => {
t.plan(13)

Expand Down
Loading