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

Re: [PATCH 1/2] color: make it easier for non-config to parse color specs

From
Jeff King <peff@peff.net>
Date
Jan 18, 2009, 18:06 UTC
Message-ID
<20090118180610.GB22718@coredump.intra.peff.net>
In-Reply-To
<49736A9C.1040601@lsrfire.ath.cx>
On Sun, Jan 18, 2009 at 06:45:00PM +0100, René Scharfe wrote:
> > need a "memcasecmp" here.
> 
> 	if (!strncasecmp(value, "reset", len)) {

Thanks. I knew there must be some stock function to do this, but for some reason I just could not think of strncasecmp.

>    die("bad color value '%.*s' for variable '%s', len, value, var);

Except that we've been shortening "len" during the course of the function, so it is generally 0 at this point.

Show 12 quoted lines
> >   $ time ./git log --pretty=tformat:'%Credfoo%Creset' >/dev/null
> >   real    0m0.673s
> >   user    0m0.652s
> >   sys     0m0.016s
> >   $ time ./git log --pretty=tformat:'%C(red)foo%C(reset)' >/dev/null
> >   real    0m0.692s
> >   user    0m0.660s
> >   sys     0m0.032s
> > 
> > That's about 1 microsecond per commit.
> 
> Hmm, not too much overhead, agreed, but it would still be nice to avoid it.
Here are the numbers with your fixes:
  $ time ./git log --pretty=tformat:'%C(red)foo%C(reset)' >/dev/null
  real    0m0.677s
  user    0m0.668s
  sys     0m0.008s

which puts the difference well into the noise region (I actually did get one run with your patch that was just as fast as the original).

Here is an updated version of patch 1/2, squashing in your color_mem_parse update, your follow-on fix, and a fix for the die().

-- >8 -- color: make it easier for non-config to parse color specs

We have very featureful color-parsing routines which are used for color.diff.* and other options. Let's make it easier to use those routines from other parts of the code.

This patch converts color_parse to color_parse_mem, taking a length-bounded string instead of a NUL-terminated one. We keep color_parse as a wrapper which takes a normal string. Thanks to René Scharfe for rewriting the color_parse implementation.

This also changes the error string for an invalid color not to mention the word "config", since it is not always appropriate (and when it is, the context is obvious since the offending config variable is given).

Finally, while we are in the area, we clean up the parameter
names in the declaration of color_parse; the var and value
parameters were reversed from the actual implementation.
---
 color.c |   31 +++++++++++++++++++++----------
 color.h |    3 ++-
 2 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/color.c b/color.c
index fc0b72a..915d7a9 100644
--- a/color.c
+++ b/color.c
@@ -41,29 +41,40 @@ static int parse_attr(const char *name, int len)
 
 void color_parse(const char *value, const char *var, char *dst)
 {
+	color_parse_mem(value, strlen(value), var, dst);
+}
+
+void color_parse_mem(const char *value, int value_len, const char *var,
+		char *dst)
+{
 	const char *ptr = value;
+	int len = value_len;
 	int attr = -1;
 	int fg = -2;
 	int bg = -2;
 
-	if (!strcasecmp(value, "reset")) {
+	if (!strncasecmp(value, "reset", len)) {
 		strcpy(dst, "\033[m");
 		return;
 	}
 
 	/* [fg [bg]] [attr] */
-	while (*ptr) {
+	while (len > 0) {
 		const char *word = ptr;
-		int val, len = 0;
+		int val, wordlen = 0;
 
-		while (word[len] && !isspace(word[len]))
-			len++;
+		while (len > 0 && !isspace(word[wordlen])) {
+			wordlen++;
+			len--;
+		}
 
-		ptr = word + len;
-		while (*ptr && isspace(*ptr))
+		ptr = word + wordlen;
+		while (len > 0 && isspace(*ptr)) {
 			ptr++;
+			len--;
+		}
 
-		val = parse_color(word, len);
+		val = parse_color(word, wordlen);
 		if (val >= -1) {
 			if (fg == -2) {
 				fg = val;
@@ -75,7 +86,7 @@ void color_parse(const char *value, const char *var, char *dst)
 			}
 			goto bad;
 		}
-		val = parse_attr(word, len);
+		val = parse_attr(word, wordlen);
 		if (val < 0 || attr != -1)
 			goto bad;
 		attr = val;
@@ -115,7 +126,7 @@ void color_parse(const char *value, const char *var, char *dst)
 	*dst = 0;
 	return;
 bad:
-	die("bad config value '%s' for variable '%s'", value, var);
+	die("bad color value '%.*s' for variable '%s'", value_len, value, var);
 }
 
 int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
diff --git a/color.h b/color.h
index 6cf5c88..7066099 100644
--- a/color.h
+++ b/color.h
@@ -16,7 +16,8 @@ extern int git_use_color_default;
 int git_color_default_config(const char *var, const char *value, void *cb);
 
 int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
-void color_parse(const char *var, const char *value, char *dst);
+void color_parse(const char *value, const char *var, char *dst);
+void color_parse_mem(const char *value, int len, const char *var, char *dst);
 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
 
-- 
1.6.1.266.g3b9d0.dirty
Previous: René ScharfeNext: Jeff King
Message 10 of 40 in “Re: [PATCH 3/3] Adds a #!bash to the top of bash completions so that editors can recognize, it as a bash script. Also adds a few simple comments above commands that, take arguments. The comments are meant to remind editors of potential, problems that”
  1. BazJan 16, 2009
  2. Jeff KingJan 17, 2009
  3. Markus HeidelbergJan 17, 2009
  4. Jeff KingJan 17, 2009
  5. 1/2 color: make it easier for non-config to parse color specsJeff King, Jan 17, 2009
  6. René ScharfeJan 18, 2009
  7. Jeff KingJan 18, 2009
  8. Jeff KingJan 18, 2009
  9. René ScharfeJan 18, 2009
  10. Jeff KingJan 18, 2009
  11. 2/2 expand --pretty=format color optionsJeff King, Jan 17, 2009
  12. René ScharfeJan 18, 2009
  13. Jeff KingJan 18, 2009
  14. Jeff KingJan 18, 2009
  15. Jeff KingJan 18, 2009
  16. 1/2 handle color.ui at a central placeMarkus Heidelberg, Jan 18, 2009
  17. 2/2 move the color variables to color.cMarkus Heidelberg, Jan 18, 2009
  18. Jeff KingJan 20, 2009
  19. Markus HeidelbergJan 21, 2009
  20. Jeff KingJan 22, 2009
  21. Markus HeidelbergJan 22, 2009
  22. Junio C HamanoJan 23, 2009
  23. Markus HeidelbergJan 24, 2009
  24. Johannes SchindelinJan 24, 2009
  25. Markus HeidelbergJan 24, 2009
  26. Junio C HamanoJan 24, 2009
  27. Jeff KingJan 24, 2009
  28. Junio C HamanoJan 24, 2009
  29. Jeff KingJan 24, 2009
  30. Markus HeidelbergJan 25, 2009
  31. Junio C HamanoJan 19, 2009
  32. Jeff KingJan 20, 2009
  33. Johannes SchindelinJan 20, 2009
  34. Johannes SchindelinJan 20, 2009
  35. Jeff KingJan 20, 2009
  36. Johannes SchindelinJan 20, 2009
  37. Jeff KingJan 20, 2009
  38. Cyellow, was Re: [a way-too-long line]Johannes Schindelin, Jan 17, 2009
  39. Jeff KingJan 17, 2009
  40. Johannes SchindelinJan 17, 2009

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.