git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/6] apply: reorder functions to move image-related things together

From
Patrick Steinhardt <ps@pks.im>
Date
Sep 16, 2024, 07:10 UTC
Message-ID
<7b6903ecdd471b9bc0dbf2d64847d4db492b4f25.1726470385.git.ps@pks.im>
In-Reply-To
<cover.1726470385.git.ps@pks.im>

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>
---
 apply.c | 106 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 53 insertions(+), 53 deletions(-)
diff --git a/apply.c b/apply.c
index 6e1060a952..9dd2f4d215 100644
--- a/apply.c
+++ b/apply.c
@@ -285,6 +285,13 @@ struct image {
 	struct line *line;
 };
 
+static void clear_image(struct image *image)
+{
+	free(image->buf);
+	free(image->line_allocated);
+	memset(image, 0, sizeof(*image));
+}
+
 static uint32_t hash_line(const char *cp, size_t len)
 {
 	size_t i;
@@ -297,42 +304,6 @@ static uint32_t hash_line(const char *cp, size_t len)
 	return h;
 }
 
-/*
- * Compare lines s1 of length n1 and s2 of length n2, ignoring
- * whitespace difference. Returns 1 if they match, 0 otherwise
- */
-static int fuzzy_matchlines(const char *s1, size_t n1,
-			    const char *s2, size_t n2)
-{
-	const char *end1 = s1 + n1;
-	const char *end2 = s2 + n2;
-
-	/* ignore line endings */
-	while (s1 < end1 && (end1[-1] == '\r' || end1[-1] == '\n'))
-		end1--;
-	while (s2 < end2 && (end2[-1] == '\r' || end2[-1] == '\n'))
-		end2--;
-
-	while (s1 < end1 && s2 < end2) {
-		if (isspace(*s1)) {
-			/*
-			 * Skip whitespace. We check on both buffers
-			 * because we don't want "a b" to match "ab".
-			 */
-			if (!isspace(*s2))
-				return 0;
-			while (s1 < end1 && isspace(*s1))
-				s1++;
-			while (s2 < end2 && isspace(*s2))
-				s2++;
-		} else if (*s1++ != *s2++)
-			return 0;
-	}
-
-	/* If we reached the end on one side only, lines don't match. */
-	return s1 == end1 && s2 == end2;
-}
-
 static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
 {
 	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,
 	image->line = image->line_allocated;
 }
 
-static void clear_image(struct image *image)
+static void remove_first_line(struct image *img)
 {
-	free(image->buf);
-	free(image->line_allocated);
-	memset(image, 0, sizeof(*image));
+	img->buf += img->line[0].len;
+	img->len -= img->line[0].len;
+	img->line++;
+	img->nr--;
+}
+
+static void remove_last_line(struct image *img)
+{
+	img->len -= img->line[--img->nr].len;
 }
 
 /* fmt must contain _one_ %s and no other substitution */
@@ -2419,6 +2396,42 @@ static void update_pre_post_images(struct image *preimage,
 	postimage->nr -= reduced;
 }
 
+/*
+ * Compare lines s1 of length n1 and s2 of length n2, ignoring
+ * whitespace difference. Returns 1 if they match, 0 otherwise
+ */
+static int fuzzy_matchlines(const char *s1, size_t n1,
+			    const char *s2, size_t n2)
+{
+	const char *end1 = s1 + n1;
+	const char *end2 = s2 + n2;
+
+	/* ignore line endings */
+	while (s1 < end1 && (end1[-1] == '\r' || end1[-1] == '\n'))
+		end1--;
+	while (s2 < end2 && (end2[-1] == '\r' || end2[-1] == '\n'))
+		end2--;
+
+	while (s1 < end1 && s2 < end2) {
+		if (isspace(*s1)) {
+			/*
+			 * Skip whitespace. We check on both buffers
+			 * because we don't want "a b" to match "ab".
+			 */
+			if (!isspace(*s2))
+				return 0;
+			while (s1 < end1 && isspace(*s1))
+				s1++;
+			while (s2 < end2 && isspace(*s2))
+				s2++;
+		} else if (*s1++ != *s2++)
+			return 0;
+	}
+
+	/* If we reached the end on one side only, lines don't match. */
+	return s1 == end1 && s2 == end2;
+}
+
 static int line_by_line_fuzzy_match(struct image *img,
 				    struct image *preimage,
 				    struct image *postimage,
@@ -2804,19 +2817,6 @@ static int find_pos(struct apply_state *state,
 	return -1;
 }
 
-static void remove_first_line(struct image *img)
-{
-	img->buf += img->line[0].len;
-	img->len -= img->line[0].len;
-	img->line++;
-	img->nr--;
-}
-
-static void remove_last_line(struct image *img)
-{
-	img->len -= img->line[--img->nr].len;
-}
-
 /*
  * The change from "preimage" and "postimage" has been found to
  * apply at applied_pos (counts in line numbers) in "img".
-- 
2.46.0.551.gc5ee8f2d1c.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 2 of 20 in “apply: fix leaking buffer of `struct image`”
  1. 0/6 apply: fix leaking buffer of `struct image`Patrick Steinhardt, Sep 16, 2024
  2. 1/6 apply: reorder functions to move image-related things togetherPatrick Steinhardt, Sep 16, 2024
  3. 2/6 apply: rename functions operating on `struct image`Patrick Steinhardt, Sep 16, 2024
  4. 3/6 apply: introduce macro and function to init imagesPatrick Steinhardt, Sep 16, 2024
  5. 4/6 apply: refactor code to drop `line_allocated`Patrick Steinhardt, Sep 16, 2024
  6. Junio C HamanoSep 16, 2024
  7. Patrick SteinhardtSep 17, 2024
  8. Junio C HamanoSep 16, 2024
  9. 5/6 apply: rename members that track line count and allocation lengthPatrick Steinhardt, Sep 16, 2024
  10. 6/6 apply: refactor `struct image` to use a `struct strbuf`Patrick Steinhardt, Sep 16, 2024
  11. Junio C HamanoSep 16, 2024
  12. 0/6 apply: fix leaking buffer of `struct image`Patrick Steinhardt, Sep 17, 2024
  13. 1/6 apply: reorder functions to move image-related things togetherPatrick Steinhardt, Sep 17, 2024
  14. 2/6 apply: rename functions operating on `struct image`Patrick Steinhardt, Sep 17, 2024
  15. 3/6 apply: introduce macro and function to init imagesPatrick Steinhardt, Sep 17, 2024
  16. 4/6 apply: refactor code to drop `line_allocated`Patrick Steinhardt, Sep 17, 2024
  17. 5/6 apply: rename members that track line count and allocation lengthPatrick Steinhardt, Sep 17, 2024
  18. 6/6 apply: refactor `struct image` to use a `struct strbuf`Patrick Steinhardt, Sep 17, 2024
  19. Patrick SteinhardtSep 17, 2024
  20. Junio C HamanoSep 17, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.