strftime cleanup: avoid recomputing strlen when it's known

This commit is contained in:
Rich Felker 2013-08-22 19:36:30 -04:00
commit 87e133b352

View file

@ -46,12 +46,11 @@ static int week_num(const struct tm *tm)
size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t); size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);
const char *__strftime_fmt_1(char (*s)[100], int f, const struct tm *tm, locale_t loc) const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc)
{ {
nl_item item; nl_item item;
int val; int val;
const char *fmt; const char *fmt;
size_t l;
switch (f) { switch (f) {
case 'a': case 'a':
@ -122,6 +121,7 @@ const char *__strftime_fmt_1(char (*s)[100], int f, const struct tm *tm, locale_
fmt = "%02d"; fmt = "%02d";
goto number; goto number;
case 'n': case 'n':
*l = 1;
return "\n"; return "\n";
case 'p': case 'p':
item = tm->tm_hour >= 12 ? PM_STR : AM_STR; item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
@ -137,6 +137,7 @@ const char *__strftime_fmt_1(char (*s)[100], int f, const struct tm *tm, locale_
fmt = "%02d"; fmt = "%02d";
goto number; goto number;
case 't': case 't':
*l = 1;
return "\t"; return "\t";
case 'T': case 'T':
fmt = "%H:%M:%S"; fmt = "%H:%M:%S";
@ -177,25 +178,30 @@ const char *__strftime_fmt_1(char (*s)[100], int f, const struct tm *tm, locale_
goto number; goto number;
case 'z': case 'z':
val = -tm->__tm_gmtoff; val = -tm->__tm_gmtoff;
snprintf(*s, sizeof *s, "%+.2d%.2d", val/3600, abs(val%3600)/60); *l = snprintf(*s, sizeof *s, "%+.2d%.2d", val/3600, abs(val%3600)/60);
return *s; return *s;
case 'Z': case 'Z':
return tm->__tm_zone; fmt = tm->__tm_zone;
goto string;
case '%': case '%':
*l = 1;
return "%"; return "%";
default: default:
return 0; return 0;
} }
number: number:
snprintf(*s, sizeof *s, fmt, val); *l = snprintf(*s, sizeof *s, fmt, val);
return *s; return *s;
nl_strcat: nl_strcat:
return __nl_langinfo_l(item, loc); fmt = __nl_langinfo_l(item, loc);
string:
*l = strlen(fmt);
return fmt;
nl_strftime: nl_strftime:
fmt = __nl_langinfo_l(item, loc); fmt = __nl_langinfo_l(item, loc);
recu_strftime: recu_strftime:
l = __strftime_l(*s, sizeof *s, fmt, tm, loc); *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
if (!l) return 0; if (!*l) return 0;
return *s; return *s;
} }
@ -215,8 +221,8 @@ size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const st
} }
f++; f++;
if (*f == 'E' || *f == 'O') f++; if (*f == 'E' || *f == 'O') f++;
t = __strftime_fmt_1(&buf, *f, tm, loc); t = __strftime_fmt_1(&buf, &k, *f, tm, loc);
if (!t || (k = strlen(t)) >= n-l) if (!t || k >= n-l)
return 0; return 0;
memcpy(s+l, t, k); memcpy(s+l, t, k);
l += k; l += k;