Commit 5ef8d77a implemented color_parse_mem, a function for
parsing colors from a non-NUL-terminated string, by simply
allocating a new NUL-terminated string and calling
color_parse. This had a small but measurable speed impact on
a user format that used the advanced color parsing. E.g.,

  # uses quick parsing
  $ time ./git log --pretty=tformat:'%Credfoo%Creset' >/dev/null
  real    0m0.673s
  user    0m0.652s
  sys     0m0.016s

  # uses color_parse_mem
  $ time ./git log --pretty=tformat:'%C(red)foo%C(reset)' >/dev/null
  real    0m0.692s
  user    0m0.660s
  sys     0m0.032s

This patch implements color_parse_mem as the primary
function, with color_parse as a wrapper for strings. This
gives comparable timings to the first case above.

Original patch by René. Commit message and debugging by Jeff
King.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2009-01-19 23:30:30 -05:00 коммит произвёл Junio C Hamano
Родитель c002922adc
Коммит 2c2dc7c82c
1 изменённых файлов: 21 добавлений и 17 удалений

38
color.c
Просмотреть файл

@ -40,30 +40,41 @@ 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 color 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)
@ -191,10 +202,3 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
va_end(args);
return r;
}
void color_parse_mem(const char *value, int len, const char *var, char *dst)
{
char *tmp = xmemdupz(value, len);
color_parse(tmp, var, dst);
free(tmp);
}