]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Optimize printf_format_fix_noalloc()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 29 Nov 2016 21:21:17 +0000 (23:21 +0200)
committerGitLab <gitlab@git.dovecot.net>
Sun, 18 Dec 2016 10:35:20 +0000 (12:35 +0200)
Using strchr() is faster than looping through the characters manually.
Since this function is being called a lot, it's worth optimizing.

src/lib/printf-format-fix.c

index 2ba9bde0b5717f61d6c317cd1a37893da2547a5b..da1b164244fe238f6a87667ac40dd6847d34c878 100644 (file)
@@ -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;
 }