apply: rename members that track line count and allocation length
The `struct image` has two members `nr` and `alloc` that track the number of lines as well as how large its array is. It is somewhat easy to confuse these members with `len` though, which tracks the length of the `buf` member. Rename these members to `line_nr` and `line_alloc` respectively to avoid confusion. This is in line with how we typically name variables that track an array in this way. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Sep 17, 2024 at 12:08 UTC
e73686f6e4a37183bff723ceff7cd74e50361fa8
1 file changed
+45
-46
apply.c
+45
-46
@@ -279,9 +279,8 @@ struct line {
279
struct image {
280
char *buf;
281
size_t len;
282
- size_t nr;
283
- size_t alloc;
282
struct line *line;
283
+ size_t line_nr, line_alloc;
284
};
285
#define IMAGE_INIT { 0 }
286
@@ -312,11 +311,11 @@ static uint32_t hash_line(const char *cp, size_t len)
311
312
static void image_add_line(struct image *img, const char *bol, size_t len, unsigned flag)
313
{
315
- ALLOC_GROW(img->line, img->nr + 1, img->alloc);
316
- img->line[img->nr].len = len;
317
- img->line[img->nr].hash = hash_line(bol, len);
318
- img->line[img->nr].flag = flag;
319
- img->nr++;
314
+ ALLOC_GROW(img->line, img->line_nr + 1, img->line_alloc);
315
+ img->line[img->line_nr].len = len;
316
+ img->line[img->line_nr].hash = hash_line(bol, len);
317
+ img->line[img->line_nr].flag = flag;
318
+ img->line_nr++;
319
}
320
321
/*
@@ -353,14 +352,14 @@ static void image_remove_first_line(struct image *img)
352
{
353
img->buf += img->line[0].len;
354
img->len -= img->line[0].len;
356
- img->nr--;
357
- if (img->nr)
358
- MOVE_ARRAY(img->line, img->line + 1, img->nr);
355
+ img->line_nr--;
356
+ if (img->line_nr)
357
+ MOVE_ARRAY(img->line, img->line + 1, img->line_nr);
358
}
359
360
static void image_remove_last_line(struct image *img)
361
{
363
- img->len -= img->line[--img->nr].len;
362
+ img->len -= img->line[--img->line_nr].len;
363
}
364
365
/* fmt must contain _one_ %s and no other substitution */
@@ -2330,9 +2329,9 @@ static void update_pre_post_images(struct image *preimage,
2329
*/
2330
image_prepare(&fixed_preimage, buf, len, 1);
2331
assert(postlen
2333
- ? fixed_preimage.nr == preimage->nr
2334
- : fixed_preimage.nr <= preimage->nr);
2335
- for (i = 0; i < fixed_preimage.nr; i++)
2332
+ ? fixed_preimage.line_nr == preimage->line_nr
2333
+ : fixed_preimage.line_nr <= preimage->line_nr);
2334
+ for (i = 0; i < fixed_preimage.line_nr; i++)
2335
fixed_preimage.line[i].flag = preimage->line[i].flag;
2336
free(preimage->line);
2337
*preimage = fixed_preimage;
@@ -2353,7 +2352,7 @@ static void update_pre_post_images(struct image *preimage,
2352
new_buf = old_buf;
2353
fixed = preimage->buf;
2354
2356
- for (i = reduced = ctx = 0; i < postimage->nr; i++) {
2355
+ for (i = reduced = ctx = 0; i < postimage->line_nr; i++) {
2356
size_t l_len = postimage->line[i].len;
2357
if (!(postimage->line[i].flag & LINE_COMMON)) {
2358
/* an added line -- no counterparts in preimage */
@@ -2367,7 +2366,7 @@ static void update_pre_post_images(struct image *preimage,
2366
old_buf += l_len;
2367
2368
/* and find the corresponding one in the fixed preimage */
2370
- while (ctx < preimage->nr &&
2369
+ while (ctx < preimage->line_nr &&
2370
!(preimage->line[ctx].flag & LINE_COMMON)) {
2371
fixed += preimage->line[ctx].len;
2372
ctx++;
@@ -2377,7 +2376,7 @@ static void update_pre_post_images(struct image *preimage,
2376
* preimage is expected to run out, if the caller
2377
* fixed addition of trailing blank lines.
2378
*/
2380
- if (preimage->nr <= ctx) {
2379
+ if (preimage->line_nr <= ctx) {
2380
reduced++;
2381
continue;
2382
}
@@ -2399,7 +2398,7 @@ static void update_pre_post_images(struct image *preimage,
2398
2399
/* Fix the length of the whole thing */
2400
postimage->len = new_buf - postimage->buf;
2402
- postimage->nr -= reduced;
2401
+ postimage->line_nr -= reduced;
2402
}
2403
2404
/*
@@ -2482,7 +2481,7 @@ static int line_by_line_fuzzy_match(struct image *img,
2481
* we are removing blank lines at the end of the file.)
2482
*/
2483
buf = preimage_eof = preimage->buf + preoff;
2485
- for ( ; i < preimage->nr; i++)
2484
+ for ( ; i < preimage->line_nr; i++)
2485
preoff += preimage->line[i].len;
2486
preimage_end = preimage->buf + preoff;
2487
for ( ; buf < preimage_end; buf++)
@@ -2522,12 +2521,12 @@ static int match_fragment(struct apply_state *state,
2521
int preimage_limit;
2522
int ret;
2523
2525
- if (preimage->nr + current_lno <= img->nr) {
2524
+ if (preimage->line_nr + current_lno <= img->line_nr) {
2525
/*
2526
* The hunk falls within the boundaries of img.
2527
*/
2529
- preimage_limit = preimage->nr;
2530
- if (match_end && (preimage->nr + current_lno != img->nr)) {
2528
+ preimage_limit = preimage->line_nr;
2529
+ if (match_end && (preimage->line_nr + current_lno != img->line_nr)) {
2530
ret = 0;
2531
goto out;
2532
}
@@ -2540,7 +2539,7 @@ static int match_fragment(struct apply_state *state,
2539
* match with img, and the remainder of the preimage
2540
* must be blank.
2541
*/
2543
- preimage_limit = img->nr - current_lno;
2542
+ preimage_limit = img->line_nr - current_lno;
2543
} else {
2544
/*
2545
* The hunk extends beyond the end of the img and
@@ -2565,7 +2564,7 @@ static int match_fragment(struct apply_state *state,
2564
}
2565
}
2566
2568
- if (preimage_limit == preimage->nr) {
2567
+ if (preimage_limit == preimage->line_nr) {
2568
/*
2569
* Do we have an exact match? If we were told to match
2570
* at the end, size must be exactly at current+fragsize,
@@ -2637,7 +2636,7 @@ static int match_fragment(struct apply_state *state,
2636
2637
/* First count added lines in postimage */
2638
postlen = 0;
2640
- for (i = 0; i < postimage->nr; i++) {
2639
+ for (i = 0; i < postimage->line_nr; i++) {
2640
if (!(postimage->line[i].flag & LINE_COMMON))
2641
postlen += postimage->line[i].len;
2642
}
@@ -2699,7 +2698,7 @@ static int match_fragment(struct apply_state *state,
2698
* empty or only contain whitespace (if WS_BLANK_AT_EOL is
2699
* false).
2700
*/
2702
- for ( ; i < preimage->nr; i++) {
2701
+ for ( ; i < preimage->line_nr; i++) {
2702
size_t fixstart = fixed.len; /* start of the fixed preimage */
2703
size_t oldlen = preimage->line[i].len;
2704
int j;
@@ -2754,7 +2753,7 @@ static int find_pos(struct apply_state *state,
2753
* than `match_beginning`.
2754
*/
2755
if (state->allow_overlap && match_beginning && match_end &&
2757
- img->nr - preimage->nr != 0)
2756
+ img->line_nr - preimage->line_nr != 0)
2757
match_beginning = 0;
2758
2759
/*
@@ -2765,15 +2764,15 @@ static int find_pos(struct apply_state *state,
2764
if (match_beginning)
2765
line = 0;
2766
else if (match_end)
2768
- line = img->nr - preimage->nr;
2767
+ line = img->line_nr - preimage->line_nr;
2768
2769
/*
2770
* Because the comparison is unsigned, the following test
2771
* will also take care of a negative line number that can
2772
* result when match_end and preimage is larger than the target.
2773
*/
2775
- if ((size_t) line > img->nr)
2776
- line = img->nr;
2774
+ if ((size_t) line > img->line_nr)
2775
+ line = img->line_nr;
2776
2777
current = 0;
2778
for (i = 0; i < line; i++)
@@ -2796,7 +2795,7 @@ static int find_pos(struct apply_state *state,
2795
return current_lno;
2796
2797
again:
2799
- if (backwards_lno == 0 && forwards_lno == img->nr)
2798
+ if (backwards_lno == 0 && forwards_lno == img->line_nr)
2799
break;
2800
2801
if (i & 1) {
@@ -2809,7 +2808,7 @@ static int find_pos(struct apply_state *state,
2808
current = backwards;
2809
current_lno = backwards_lno;
2810
} else {
2812
- if (forwards_lno == img->nr) {
2811
+ if (forwards_lno == img->line_nr) {
2812
i++;
2813
goto again;
2814
}
@@ -2852,9 +2851,9 @@ static void update_image(struct apply_state *state,
2851
* to the number of lines in the preimage that falls
2852
* within the boundaries.
2853
*/
2855
- preimage_limit = preimage->nr;
2856
- if (preimage_limit > img->nr - applied_pos)
2857
- preimage_limit = img->nr - applied_pos;
2854
+ preimage_limit = preimage->line_nr;
2855
+ if (preimage_limit > img->line_nr - applied_pos)
2856
+ preimage_limit = img->line_nr - applied_pos;
2857
2858
for (i = 0; i < applied_pos; i++)
2859
applied_at += img->line[i].len;
@@ -2877,22 +2876,22 @@ static void update_image(struct apply_state *state,
2876
result[img->len] = '\0';
2877
2878
/* Adjust the line table */
2880
- nr = img->nr + postimage->nr - preimage_limit;
2881
- if (preimage_limit < postimage->nr)
2879
+ nr = img->line_nr + postimage->line_nr - preimage_limit;
2880
+ if (preimage_limit < postimage->line_nr)
2881
/*
2882
* NOTE: this knows that we never call image_remove_first_line()
2883
* on anything other than pre/post image.
2884
*/
2885
REALLOC_ARRAY(img->line, nr);
2887
- if (preimage_limit != postimage->nr)
2888
- MOVE_ARRAY(img->line + applied_pos + postimage->nr,
2886
+ if (preimage_limit != postimage->line_nr)
2887
+ MOVE_ARRAY(img->line + applied_pos + postimage->line_nr,
2888
img->line + applied_pos + preimage_limit,
2890
- img->nr - (applied_pos + preimage_limit));
2891
- COPY_ARRAY(img->line + applied_pos, postimage->line, postimage->nr);
2889
+ img->line_nr - (applied_pos + preimage_limit));
2890
+ COPY_ARRAY(img->line + applied_pos, postimage->line, postimage->line_nr);
2891
if (!state->allow_overlap)
2893
- for (i = 0; i < postimage->nr; i++)
2892
+ for (i = 0; i < postimage->line_nr; i++)
2893
img->line[applied_pos + i].flag |= LINE_PATCHED;
2895
- img->nr = nr;
2894
+ img->line_nr = nr;
2895
}
2896
2897
/*
@@ -3024,8 +3023,8 @@ static int apply_one_fragment(struct apply_state *state,
3023
newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
3024
old--;
3025
strbuf_setlen(&newlines, newlines.len - 1);
3027
- preimage.line[preimage.nr - 1].len--;
3028
- postimage.line[postimage.nr - 1].len--;
3026
+ preimage.line[preimage.line_nr - 1].len--;
3027
+ postimage.line[postimage.line_nr - 1].len--;
3028
}
3029
3030
leading = frag->leading;
@@ -3096,7 +3095,7 @@ static int apply_one_fragment(struct apply_state *state,
3095
3096
if (applied_pos >= 0) {
3097
if (new_blank_lines_at_end &&
3099
- preimage.nr + applied_pos >= img->nr &&
3098
+ preimage.line_nr + applied_pos >= img->line_nr &&
3099
(ws_rule & WS_BLANK_AT_EOF) &&
3100
state->ws_error_action != nowarn_ws_error) {
3101
record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,