2626 * Based heavily on the Streaming Boyer-Moore-Horspool C++ implementation
2727 * by Hongli Lai at: https://github.com/FooBarWidget/boyer-moore-horspool
2828 */
29- const EventEmitter = require ( 'node:events' ) . EventEmitter
30- const inherits = require ( 'node:util' ) . inherits
29+
30+ const { EventEmitter } = require ( 'node:events' )
31+ const { inherits } = require ( 'node:util' )
3132
3233function SBMH ( needle ) {
3334 if ( typeof needle === 'string' ) {
@@ -154,9 +155,8 @@ SBMH.prototype._sbmh_feed = function (data) {
154155 this . emit ( 'info' , false , this . _lookbehind , 0 , bytesToCutOff )
155156 }
156157
157- this . _lookbehind . copy ( this . _lookbehind , 0 , bytesToCutOff ,
158- this . _lookbehind_size - bytesToCutOff )
159158 this . _lookbehind_size -= bytesToCutOff
159+ this . _lookbehind . copy ( this . _lookbehind , 0 , bytesToCutOff , this . _lookbehind_size )
160160
161161 data . copy ( this . _lookbehind , this . _lookbehind_size )
162162 this . _lookbehind_size += len
@@ -166,20 +166,18 @@ SBMH.prototype._sbmh_feed = function (data) {
166166 }
167167 }
168168
169- pos += ( pos >= 0 ) * this . _bufpos
170-
171169 // Lookbehind buffer is now empty. We only need to check if the
172170 // needle is in the haystack.
173- if ( data . indexOf ( needle , pos ) !== - 1 ) {
174- pos = data . indexOf ( needle , pos )
171+ pos = data . indexOf ( needle , pos + ( ( pos >= 0 ) * this . _bufpos ) )
172+
173+ if ( pos !== - 1 ) {
175174 ++ this . matches
176175 if ( pos > 0 ) { this . emit ( 'info' , true , data , this . _bufpos , pos ) } else { this . emit ( 'info' , true ) }
177-
178176 return ( this . _bufpos = pos + needleLength )
179- } else {
180- pos = len - needleLength
181177 }
182178
179+ pos = len - needleLength
180+
183181 // There was no match. If there's trailing haystack data that we cannot
184182 // match yet using the Boyer-Moore-Horspool algorithm (because the trailing
185183 // data is less than the needle size) then match using a modified
@@ -190,12 +188,10 @@ SBMH.prototype._sbmh_feed = function (data) {
190188 pos < len &&
191189 (
192190 data [ pos ] !== needle [ 0 ] ||
193- (
194- ( Buffer . compare (
195- data . subarray ( pos , pos + len - pos ) ,
196- needle . subarray ( 0 , len - pos )
197- ) !== 0 )
198- )
191+ Buffer . compare (
192+ data . subarray ( pos , pos + len - pos ) ,
193+ needle . subarray ( 0 , len - pos )
194+ ) !== 0
199195 )
200196 ) {
201197 ++ pos
@@ -213,7 +209,7 @@ SBMH.prototype._sbmh_feed = function (data) {
213209}
214210
215211SBMH . prototype . _sbmh_lookup_char = function ( data , pos ) {
216- return ( pos < 0 )
212+ return pos < 0
217213 ? this . _lookbehind [ this . _lookbehind_size + pos ]
218214 : data [ pos ]
219215}
0 commit comments