@@ -271,16 +271,19 @@ function grepPayloadHits(
271271 regex : RegExp ,
272272 context : number ,
273273 remaining : number ,
274- ) : string [ ] {
275- if ( remaining <= 0 ) return [ ] ;
274+ ) : { lines : string [ ] ; matchCount : number } {
275+ if ( remaining <= 0 ) return { lines : [ ] , matchCount : 0 } ;
276276 const matchLines : number [ ] = [ ] ;
277277 for ( let i = 0 ; i < lines . length ; i ++ ) {
278278 if ( regex . test ( lines [ i ] ?? "" ) ) matchLines . push ( i ) ;
279279 if ( matchLines . length >= remaining ) break ;
280280 }
281- if ( matchLines . length === 0 ) return [ ] ;
281+ if ( matchLines . length === 0 ) return { lines : [ ] , matchCount : 0 } ;
282282 if ( context <= 0 ) {
283- return matchLines . map ( ( i ) => `${ ref } :${ i + 1 } :${ lines [ i ] ?? "" } ` ) ;
283+ return {
284+ lines : matchLines . map ( ( i ) => `${ ref } :${ i + 1 } :${ lines [ i ] ?? "" } ` ) ,
285+ matchCount : matchLines . length ,
286+ } ;
284287 }
285288 const matchSet = new Set ( matchLines ) ;
286289 const ranges : { start : number ; end : number } [ ] = [ ] ;
@@ -304,7 +307,7 @@ function grepPayloadHits(
304307 out . push ( `${ ref } ${ sep } ${ i + 1 } ${ sep } ${ lines [ i ] ?? "" } ` ) ;
305308 }
306309 }
307- return out ;
310+ return { lines : out , matchCount : matchLines . length } ;
308311}
309312
310313async function grepArchive (
@@ -325,13 +328,15 @@ async function grepArchive(
325328 }
326329 const occurrences = await selectOccurrences ( archive , occurrenceId ) ;
327330 const hits : string [ ] = [ ] ;
331+ let matchCount = 0 ;
328332 for ( const occ of occurrences ) {
329333 signal . throwIfAborted ( ) ;
330- if ( hits . length >= maxResults ) break ;
334+ if ( matchCount >= maxResults ) break ;
331335 if ( glob !== undefined && ! matchesArchiveName ( glob , occ ) ) continue ;
332336 const ref = formatArchiveRef ( occ . occurrenceId ) ;
333337 if ( regex . test ( metadataBlob ( occ ) ) ) {
334338 hits . push ( `${ ref } :1:${ formatHit ( occ ) } ` ) ;
339+ matchCount ++ ;
335340 continue ;
336341 }
337342 if ( occ . gap === true ) continue ;
@@ -343,9 +348,15 @@ async function grepArchive(
343348 continue ;
344349 }
345350 signal . throwIfAborted ( ) ;
346- hits . push (
347- ...grepPayloadHits ( ref , payload . split ( "\n" ) , regex , context , maxResults - hits . length ) ,
351+ const payloadHits = grepPayloadHits (
352+ ref ,
353+ payload . split ( "\n" ) ,
354+ regex ,
355+ context ,
356+ maxResults - matchCount ,
348357 ) ;
358+ hits . push ( ...payloadHits . lines ) ;
359+ matchCount += payloadHits . matchCount ;
349360 }
350361 if ( hits . length === 0 ) return `no matches for /${ pattern } /` ;
351362 return hits . join ( "\n" ) ;
0 commit comments