| 226 | } |
| 227 | |
| 228 | function findSequence(haystack: string[], needle: string[], from: number, endOfFile = false): number { |
| 229 | if (needle.length === 0) return from; |
| 230 | |
| 231 | const matchAt = (index: number, normalize: (value: string) => string): boolean => |
| 232 | needle.every((line, offset) => normalize(haystack[index + offset] ?? "") === normalize(line)); |
| 233 | |
| 234 | for (const normalize of [ |
| 235 | (value: string) => value, |
| 236 | (value: string) => value.trimEnd(), |
| 237 | (value: string) => value.trim(), |
| 238 | ]) { |
| 239 | const start = endOfFile ? haystack.length - needle.length : from; |
| 240 | const end = haystack.length - needle.length; |
| 241 | for (let index = start; index <= end; index += 1) { |
| 242 | if (index >= from && matchAt(index, normalize)) return index; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | function applyHunks(path: string, content: string, hunks: UpdateHunk[]): string { |
| 250 | const file = splitFile(content); |