* Update the preimage, and the common lines in postimage, * from buffer buf of length len. */
| 2431 | * from buffer buf of length len. |
| 2432 | */ |
| 2433 | static void update_pre_post_images(struct image *preimage, |
| 2434 | struct image *postimage, |
| 2435 | char *buf, size_t len) |
| 2436 | { |
| 2437 | struct image fixed_preimage = IMAGE_INIT; |
| 2438 | size_t insert_pos = 0; |
| 2439 | int i, ctx, reduced; |
| 2440 | const char *fixed; |
| 2441 | |
| 2442 | /* |
| 2443 | * Update the preimage with whitespace fixes. Note that we |
| 2444 | * are not losing preimage->buf -- apply_one_fragment() will |
| 2445 | * free "oldlines". |
| 2446 | */ |
| 2447 | image_prepare(&fixed_preimage, buf, len, 1); |
| 2448 | for (i = 0; i < fixed_preimage.line_nr; i++) |
| 2449 | fixed_preimage.line[i].flag = preimage->line[i].flag; |
| 2450 | image_clear(preimage); |
| 2451 | *preimage = fixed_preimage; |
| 2452 | fixed = preimage->buf.buf; |
| 2453 | |
| 2454 | /* |
| 2455 | * Adjust the common context lines in postimage. |
| 2456 | */ |
| 2457 | for (i = reduced = ctx = 0; i < postimage->line_nr; i++) { |
| 2458 | size_t l_len = postimage->line[i].len; |
| 2459 | |
| 2460 | if (!(postimage->line[i].flag & LINE_COMMON)) { |
| 2461 | /* an added line -- no counterparts in preimage */ |
| 2462 | insert_pos += l_len; |
| 2463 | continue; |
| 2464 | } |
| 2465 | |
| 2466 | /* and find the corresponding one in the fixed preimage */ |
| 2467 | while (ctx < preimage->line_nr && |
| 2468 | !(preimage->line[ctx].flag & LINE_COMMON)) { |
| 2469 | fixed += preimage->line[ctx].len; |
| 2470 | ctx++; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * preimage is expected to run out, if the caller |
| 2475 | * fixed addition of trailing blank lines. |
| 2476 | */ |
| 2477 | if (preimage->line_nr <= ctx) { |
| 2478 | reduced++; |
| 2479 | continue; |
| 2480 | } |
| 2481 | |
| 2482 | /* and copy it in, while fixing the line length */ |
| 2483 | l_len = preimage->line[ctx].len; |
| 2484 | strbuf_splice(&postimage->buf, insert_pos, postimage->line[i].len, |
| 2485 | fixed, l_len); |
| 2486 | insert_pos += l_len; |
| 2487 | fixed += l_len; |
| 2488 | postimage->line[i].len = l_len; |
| 2489 | ctx++; |
| 2490 | } |
no test coverage detected