MCPcopy Create free account
hub / github.com/anomalyco/opencode / tryMatch

Function tryMatch

packages/opencode/src/patch/index.ts:429–458  ·  view source on GitHub ↗
(lines: string[], pattern: string[], startIndex: number, compare: Comparator, eof: boolean)

Source from the content-addressed store, hash-verified

427type Comparator = (a: string, b: string) => boolean
428
429function tryMatch(lines: string[], pattern: string[], startIndex: number, compare: Comparator, eof: boolean): number {
430 // If EOF anchor, try matching from end of file first
431 if (eof) {
432 const fromEnd = lines.length - pattern.length
433 if (fromEnd >= startIndex) {
434 let matches = true
435 for (let j = 0; j < pattern.length; j++) {
436 if (!compare(lines[fromEnd + j], pattern[j])) {
437 matches = false
438 break
439 }
440 }
441 if (matches) return fromEnd
442 }
443 }
444
445 // Forward search from startIndex
446 for (let i = startIndex; i <= lines.length - pattern.length; i++) {
447 let matches = true
448 for (let j = 0; j < pattern.length; j++) {
449 if (!compare(lines[i + j], pattern[j])) {
450 matches = false
451 break
452 }
453 }
454 if (matches) return i
455 }
456
457 return -1
458}
459
460function seekSequence(lines: string[], pattern: string[], startIndex: number, eof = false): number {
461 if (pattern.length === 0) return -1

Callers 1

seekSequenceFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected