MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / seekSequence

Function seekSequence

src/core/tools/apply-patch/seek-sequence.ts:110–153  ·  view source on GitHub ↗
(lines: string[], pattern: string[], start: number, eof: boolean)

Source from the content-addressed store, hash-verified

108 * @returns The starting index of the match, or null if not found
109 */
110export function seekSequence(lines: string[], pattern: string[], start: number, eof: boolean): number | null {
111 if (pattern.length === 0) {
112 return start
113 }
114
115 // When the pattern is longer than available input, there's no possible match
116 if (pattern.length > lines.length) {
117 return null
118 }
119
120 const searchStart = eof && lines.length >= pattern.length ? lines.length - pattern.length : start
121
122 const maxStart = lines.length - pattern.length
123
124 // Pass 1: Exact match
125 for (let i = searchStart; i <= maxStart; i++) {
126 if (exactMatch(lines, pattern, i)) {
127 return i
128 }
129 }
130
131 // Pass 2: Trim-end match
132 for (let i = searchStart; i <= maxStart; i++) {
133 if (trimEndMatch(lines, pattern, i)) {
134 return i
135 }
136 }
137
138 // Pass 3: Trim both sides match
139 for (let i = searchStart; i <= maxStart; i++) {
140 if (trimMatch(lines, pattern, i)) {
141 return i
142 }
143 }
144
145 // Pass 4: Unicode-normalized match
146 for (let i = searchStart; i <= maxStart; i++) {
147 if (normalizedMatch(lines, pattern, i)) {
148 return i
149 }
150 }
151
152 return null
153}

Callers 2

computeReplacementsFunction · 0.90

Calls 4

exactMatchFunction · 0.85
trimEndMatchFunction · 0.85
trimMatchFunction · 0.85
normalizedMatchFunction · 0.85

Tested by

no test coverage detected