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

[PATCH v2 3/4] grep: copy struct in one fell swoop

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Nov 24, 2020, 21:04 UTC
Message-ID
<359355fb4eff6d99cb1baad9b72ff96e7dcda51d.1606251358.git.martin.agren@gmail.com>
In-Reply-To
<cover.1606251357.git.martin.agren@gmail.com>

We have a `struct grep_opt` with our defaults which we then copy into the caller's struct. Rather than zeroing the target struct and copying each element one by one, just copy everything at once. This leaves the code simpler and more maintainable.

We don't have any ownership issues with what we're copying now and can just greedily copy the whole thing. If and when we do need to handle such elements (`char *`?), we must and can handle it appropriately. This commit doesn't really change that.

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
 grep.c | 25 ++-----------------------
 1 file changed, 2 insertions(+), 23 deletions(-)
diff --git a/grep.c b/grep.c
index 8f2009ec9f..7d740452cd 100644
--- a/grep.c
+++ b/grep.c
@@ -66,11 +66,6 @@ static const char *color_grep_slots[] = {
 	[GREP_COLOR_SEP]	    = "separator",
 };
 
-static void color_set(char *dst, const char *color_bytes)
-{
-	xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
-}
-
 static int parse_pattern_type_arg(const char *opt, const char *arg)
 {
 	if (!strcmp(arg, "default"))
@@ -157,9 +152,6 @@ int grep_config(const char *var, const char *value, void *cb)
  */
 void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
 {
-	struct grep_opt *def = &grep_defaults;
-	int i;
-
 #if defined(USE_LIBPCRE2)
 	if (!pcre2_global_context)
 		pcre2_global_context = pcre2_general_context_create(
@@ -171,26 +163,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
 	pcre_free = free;
 #endif
 
-	memset(opt, 0, sizeof(*opt));
+	*opt = grep_defaults;
+
 	opt->repo = repo;
 	opt->prefix = prefix;
 	opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
 	opt->pattern_tail = &opt->pattern_list;
 	opt->header_tail = &opt->header_list;
-
-	opt->only_matching = def->only_matching;
-	opt->color = def->color;
-	opt->extended_regexp_option = def->extended_regexp_option;
-	opt->pattern_type_option = def->pattern_type_option;
-	opt->linenum = def->linenum;
-	opt->columnnum = def->columnnum;
-	opt->max_depth = def->max_depth;
-	opt->pathname = def->pathname;
-	opt->relative = def->relative;
-	opt->output = def->output;
-
-	for (i = 0; i < NR_GREP_COLORS; i++)
-		color_set(opt->colors[i], def->colors[i]);
 }
 
 void grep_destroy(void)
-- 
2.29.2.454.gaff20da3a2
Previous: Martin ÅgrenNext: Junio C Hamano
Message 14 of 26 in “grep: retire `init_grep_defaults()`”
  1. 0/4 grep: retire `init_grep_defaults()`Martin Ågren, Nov 21, 2020
  2. 1/4 grep: don't set up a "default" repo for grepMartin Ågren, Nov 21, 2020
  3. 2/4 grep: use designated initializers for `grep_defaults`Martin Ågren, Nov 21, 2020
  4. 3/4 grep: simplify color setupMartin Ågren, Nov 21, 2020
  5. Jeff KingNov 21, 2020
  6. Martin ÅgrenNov 21, 2020
  7. Junio C HamanoNov 21, 2020
  8. Jeff KingNov 24, 2020
  9. 4/4 MyFirstObjectWalk: drop `init_walken_defaults()`Martin Ågren, Nov 21, 2020
  10. Johannes SchindelinNov 23, 2020
  11. 0/4 grep: simplify "grep defaults" handlingMartin Ågren, Nov 24, 2020
  12. 1/4 grep: don't set up a "default" repo for grepMartin Ågren, Nov 24, 2020
  13. 2/4 grep: use designated initializers for `grep_defaults`Martin Ågren, Nov 24, 2020
  14. 3/4 grep: copy struct in one fell swoopMartin Ågren, Nov 24, 2020
  15. Junio C HamanoNov 24, 2020
  16. Martin ÅgrenNov 25, 2020
  17. Junio C HamanoNov 25, 2020
  18. Martin ÅgrenNov 26, 2020
  19. 4/4 MyFirstObjectWalk: drop `init_walken_defaults()`Martin Ågren, Nov 24, 2020
  20. Ævar Arnfjörð BjarmasonNov 25, 2020
  21. 0/4 grep: simplify "grep defaults" handlingMartin Ågren, Nov 29, 2020
  22. 1/4 grep: don't set up a "default" repo for grepMartin Ågren, Nov 29, 2020
  23. 2/4 grep: use designated initializers for `grep_defaults`Martin Ågren, Nov 29, 2020
  24. 3/4 grep: copy struct in one fell swoopMartin Ågren, Nov 29, 2020
  25. 4/4 MyFirstObjectWalk: drop `init_walken_defaults()`Martin Ågren, Nov 29, 2020
  26. Junio C HamanoDec 1, 2020

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.