(input: str, chunks: list[Chunk], newline: str)
| 318 | |
| 319 | |
| 320 | def _apply_chunks(input: str, chunks: list[Chunk], newline: str) -> str: |
| 321 | orig_lines = input.split("\n") |
| 322 | dest_lines: list[str] = [] |
| 323 | cursor = 0 |
| 324 | |
| 325 | for chunk in chunks: |
| 326 | if chunk.orig_index > len(orig_lines): |
| 327 | raise ValueError( |
| 328 | f"applyDiff: chunk.origIndex {chunk.orig_index} > input length {len(orig_lines)}" |
| 329 | ) |
| 330 | if cursor > chunk.orig_index: |
| 331 | raise ValueError( |
| 332 | f"applyDiff: overlapping chunk at {chunk.orig_index} (cursor {cursor})" |
| 333 | ) |
| 334 | |
| 335 | dest_lines.extend(orig_lines[cursor : chunk.orig_index]) |
| 336 | cursor = chunk.orig_index |
| 337 | |
| 338 | if chunk.ins_lines: |
| 339 | dest_lines.extend(chunk.ins_lines) |
| 340 | |
| 341 | cursor += len(chunk.del_lines) |
| 342 | |
| 343 | dest_lines.extend(orig_lines[cursor:]) |
| 344 | return newline.join(dest_lines) |
| 345 | |
| 346 | |
| 347 | __all__ = ["apply_diff"] |
no outgoing calls