From: Timo Sirainen Date: Tue, 29 Nov 2016 21:21:17 +0000 (+0200) Subject: lib: Optimize printf_format_fix_noalloc() X-Git-Tag: 2.3.0.rc1~2395 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=51292e327a91a257b8baea83560b3a8323cac8c5;p=thirdparty%2Fdovecot%2Fcore.git lib: Optimize printf_format_fix_noalloc() Using strchr() is faster than looping through the characters manually. Since this function is being called a lot, it's worth optimizing. --- diff --git a/src/lib/printf-format-fix.c b/src/lib/printf-format-fix.c index 2ba9bde0b5..da1b164244 100644 --- a/src/lib/printf-format-fix.c +++ b/src/lib/printf-format-fix.c @@ -35,28 +35,27 @@ fix_format_real(const char *fmt, const char *p, size_t *len_r) static const char * printf_format_fix_noalloc(const char *format, size_t *len_r) { - const char *p; - const char *ret = format; - - for (p = format; *p != '\0'; ) { - if (*p++ == '%') { - switch (*p) { - case 'n': - i_panic("%%n modifier used"); - case 'm': - if (ret != format) - i_panic("%%m used twice"); - ret = fix_format_real(format, p-1, len_r); - break; - case '\0': - i_panic("%% modifier missing in '%s'", format); - } - p++; + const char *ret, *p, *p2; + + p = ret = format; + while ((p2 = strchr(p, '%')) != NULL) { + p = p2+1; + switch (*p) { + case 'n': + i_panic("%%n modifier used"); + case 'm': + if (ret != format) + i_panic("%%m used twice"); + ret = fix_format_real(format, p-1, len_r); + break; + case '\0': + i_panic("%% modifier missing in '%s'", format); } + p++; } if (ret == format) - *len_r = p - format; + *len_r = p - format + strlen(p); return ret; }