apply: reorder functions to move image-related things together
While most of the functions relating to `struct image` are relatively close to one another, `fuzzy_matchlines()` sits in between those even though it is rather unrelated. Reorder functions such that `struct image`-related functions are next to each other. While at it, move `clear_image()` to the top such that it is close to the struct definition itself. This makes this lifecycle-related thing easy to discover. 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:07 UTC
1f2df6f9a56118c282f09f263962acba55080c28
1 file changed
+53
-53
apply.c
+53
-53
@@ -285,6 +285,13 @@ struct image {
285
struct line *line;
286
};
287
288
+static void clear_image(struct image *image)
289
+{
290
+ free(image->buf);
291
+ free(image->line_allocated);
292
+ memset(image, 0, sizeof(*image));
293
+}
294
+
295
static uint32_t hash_line(const char *cp, size_t len)
296
{
297
size_t i;
@@ -297,42 +304,6 @@ static uint32_t hash_line(const char *cp, size_t len)
304
return h;
305
}
306
300
-/*
301
- * Compare lines s1 of length n1 and s2 of length n2, ignoring
302
- * whitespace difference. Returns 1 if they match, 0 otherwise
303
- */
304
-static int fuzzy_matchlines(const char *s1, size_t n1,
305
- const char *s2, size_t n2)
306
-{
307
- const char *end1 = s1 + n1;
308
- const char *end2 = s2 + n2;
309
-
310
- /* ignore line endings */
311
- while (s1 < end1 && (end1[-1] == '\r' || end1[-1] == '\n'))
312
- end1--;
313
- while (s2 < end2 && (end2[-1] == '\r' || end2[-1] == '\n'))
314
- end2--;
315
-
316
- while (s1 < end1 && s2 < end2) {
317
- if (isspace(*s1)) {
318
- /*
319
- * Skip whitespace. We check on both buffers
320
- * because we don't want "a b" to match "ab".
321
- */
322
- if (!isspace(*s2))
323
- return 0;
324
- while (s1 < end1 && isspace(*s1))
325
- s1++;
326
- while (s2 < end2 && isspace(*s2))
327
- s2++;
328
- } else if (*s1++ != *s2++)
329
- return 0;
330
- }
331
-
332
- /* If we reached the end on one side only, lines don't match. */
333
- return s1 == end1 && s2 == end2;
334
-}
335
-
307
static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
308
{
309
ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
@@ -373,11 +344,17 @@ static void prepare_image(struct image *image, char *buf, size_t len,
344
image->line = image->line_allocated;
345
}
346
376
-static void clear_image(struct image *image)
347
+static void remove_first_line(struct image *img)
348
{
378
- free(image->buf);
379
- free(image->line_allocated);
380
- memset(image, 0, sizeof(*image));
349
+ img->buf += img->line[0].len;
350
+ img->len -= img->line[0].len;
351
+ img->line++;
352
+ img->nr--;
353
+}
354
+
355
+static void remove_last_line(struct image *img)
356
+{
357
+ img->len -= img->line[--img->nr].len;
358
}
359
360
/* fmt must contain _one_ %s and no other substitution */
@@ -2419,6 +2396,42 @@ static void update_pre_post_images(struct image *preimage,
2396
postimage->nr -= reduced;
2397
}
2398
2399
+/*
2400
+ * Compare lines s1 of length n1 and s2 of length n2, ignoring
2401
+ * whitespace difference. Returns 1 if they match, 0 otherwise
2402
+ */
2403
+static int fuzzy_matchlines(const char *s1, size_t n1,
2404
+ const char *s2, size_t n2)
2405
+{
2406
+ const char *end1 = s1 + n1;
2407
+ const char *end2 = s2 + n2;
2408
+
2409
+ /* ignore line endings */
2410
+ while (s1 < end1 && (end1[-1] == '\r' || end1[-1] == '\n'))
2411
+ end1--;
2412
+ while (s2 < end2 && (end2[-1] == '\r' || end2[-1] == '\n'))
2413
+ end2--;
2414
+
2415
+ while (s1 < end1 && s2 < end2) {
2416
+ if (isspace(*s1)) {
2417
+ /*
2418
+ * Skip whitespace. We check on both buffers
2419
+ * because we don't want "a b" to match "ab".
2420
+ */
2421
+ if (!isspace(*s2))
2422
+ return 0;
2423
+ while (s1 < end1 && isspace(*s1))
2424
+ s1++;
2425
+ while (s2 < end2 && isspace(*s2))
2426
+ s2++;
2427
+ } else if (*s1++ != *s2++)
2428
+ return 0;
2429
+ }
2430
+
2431
+ /* If we reached the end on one side only, lines don't match. */
2432
+ return s1 == end1 && s2 == end2;
2433
+}
2434
+
2435
static int line_by_line_fuzzy_match(struct image *img,
2436
struct image *preimage,
2437
struct image *postimage,
@@ -2804,19 +2817,6 @@ static int find_pos(struct apply_state *state,
2817
return -1;
2818
}
2819
2807
-static void remove_first_line(struct image *img)
2808
-{
2809
- img->buf += img->line[0].len;
2810
- img->len -= img->line[0].len;
2811
- img->line++;
2812
- img->nr--;
2813
-}
2814
-
2815
-static void remove_last_line(struct image *img)
2816
-{
2817
- img->len -= img->line[--img->nr].len;
2818
-}
2819
-
2820
/*
2821
* The change from "preimage" and "postimage" has been found to
2822
* apply at applied_pos (counts in line numbers) in "img".