| 177 | } |
| 178 | |
| 179 | function readSection( |
| 180 | lines: string[], |
| 181 | startIndex: number, |
| 182 | ): { |
| 183 | nextContext: string[] |
| 184 | sectionChunks: Chunk[] |
| 185 | endIndex: number |
| 186 | eof: boolean |
| 187 | } { |
| 188 | const context: string[] = [] |
| 189 | let delLines: string[] = [] |
| 190 | let insLines: string[] = [] |
| 191 | const sectionChunks: Chunk[] = [] |
| 192 | |
| 193 | let mode: 'keep' | 'add' | 'delete' = 'keep' |
| 194 | let index = startIndex |
| 195 | const origIndex = index |
| 196 | |
| 197 | while (index < lines.length) { |
| 198 | const raw = lines[index]! |
| 199 | |
| 200 | if ( |
| 201 | raw.startsWith('@@') || |
| 202 | raw.startsWith(END_PATCH) || |
| 203 | raw.startsWith('*** Update File:') || |
| 204 | raw.startsWith('*** Delete File:') || |
| 205 | raw.startsWith('*** Add File:') || |
| 206 | raw.startsWith(END_FILE) |
| 207 | ) { |
| 208 | break |
| 209 | } |
| 210 | |
| 211 | if (raw === '***') { |
| 212 | break |
| 213 | } |
| 214 | |
| 215 | if (raw.startsWith('***')) { |
| 216 | throw new Error(`Invalid Line: ${raw}`) |
| 217 | } |
| 218 | |
| 219 | index += 1 |
| 220 | const lastMode = mode |
| 221 | |
| 222 | let line = raw |
| 223 | if (line === '') { |
| 224 | line = ' ' |
| 225 | } |
| 226 | |
| 227 | if (line[0] === '+') { |
| 228 | mode = 'add' |
| 229 | } else if (line[0] === '-') { |
| 230 | mode = 'delete' |
| 231 | } else if (line[0] === ' ') { |
| 232 | mode = 'keep' |
| 233 | } else { |
| 234 | throw new Error(`Invalid Line: ${line}`) |
| 235 | } |
| 236 | |