From 00d234aea5873cabb628dd9869bdd745e47830d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Wed, 4 Dec 2024 08:16:06 +0100 Subject: [PATCH 1/3] perf: always check data when looking for last char of needle --- deps/streamsearch/sbmh.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/streamsearch/sbmh.js b/deps/streamsearch/sbmh.js index 79d3150..ee04a12 100644 --- a/deps/streamsearch/sbmh.js +++ b/deps/streamsearch/sbmh.js @@ -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 && From 00bfc197c097dbc7445ccc41b944c49fdb7fa6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Wed, 4 Dec 2024 12:12:49 +0100 Subject: [PATCH 2/3] add another test --- test/streamsearch.test.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/test/streamsearch.test.js b/test/streamsearch.test.js index f565496..b9f71af 100644 --- a/test/streamsearch.test.js +++ b/test/streamsearch.test.js @@ -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) @@ -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('shello'), 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) From 3609c4575e5e55638921d319b2f73230b402c1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Wed, 4 Dec 2024 12:27:29 +0100 Subject: [PATCH 3/3] fix typo --- test/streamsearch.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/streamsearch.test.js b/test/streamsearch.test.js index b9f71af..75146f7 100644 --- a/test/streamsearch.test.js +++ b/test/streamsearch.test.js @@ -243,7 +243,7 @@ test('streamsearch', async t => { t.plan(9) const expected = [ [false, Buffer.from('t\0\0'), 0, 1], - [false, Buffer.from('shello'), 0, 7] + [false, Buffer.from('eshello'), 0, 7] ] const needle = 'test' const s = new Streamsearch(needle)