提交 45849d3d 编写于 作者: R Rich Felker

more strftime refactoring

make __strftime_fmt_1 return a string (possibly in the caller-provided
temp buffer) rather than writing into the output buffer. this approach
makes more sense when padding to a minimum field width might be
required, and it's also closer to what wcsftime wants.
上级 f5e4efc4
...@@ -46,15 +46,13 @@ static int week_num(const struct tm *tm) ...@@ -46,15 +46,13 @@ 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);
int __strftime_fmt_1(char *s, size_t n, int f, const struct tm *tm, locale_t loc) const char *__strftime_fmt_1(char (*s)[100], 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; size_t l;
if (n<2) return 0;
switch (f) { switch (f) {
case 'a': case 'a':
item = ABDAY_1 + tm->tm_wday; item = ABDAY_1 + tm->tm_wday;
...@@ -124,9 +122,7 @@ int __strftime_fmt_1(char *s, size_t n, int f, const struct tm *tm, locale_t loc ...@@ -124,9 +122,7 @@ int __strftime_fmt_1(char *s, size_t n, int f, const struct tm *tm, locale_t loc
fmt = "%02d"; fmt = "%02d";
goto number; goto number;
case 'n': case 'n':
s[0] = '\n'; return "\n";
s[1] = 0;
return 1;
case 'p': case 'p':
item = tm->tm_hour >= 12 ? PM_STR : AM_STR; item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
goto nl_strcat; goto nl_strcat;
...@@ -141,9 +137,7 @@ int __strftime_fmt_1(char *s, size_t n, int f, const struct tm *tm, locale_t loc ...@@ -141,9 +137,7 @@ int __strftime_fmt_1(char *s, size_t n, int f, const struct tm *tm, locale_t loc
fmt = "%02d"; fmt = "%02d";
goto number; goto number;
case 't': case 't':
s[0] = '\t'; return "\t";
s[1] = 0;
return 1;
case 'T': case 'T':
fmt = "%H:%M:%S"; fmt = "%H:%M:%S";
goto recu_strftime; goto recu_strftime;
...@@ -183,43 +177,51 @@ int __strftime_fmt_1(char *s, size_t n, int f, const struct tm *tm, locale_t loc ...@@ -183,43 +177,51 @@ int __strftime_fmt_1(char *s, size_t n, int f, const struct tm *tm, locale_t loc
goto number; goto number;
case 'z': case 'z':
val = -tm->__tm_gmtoff; val = -tm->__tm_gmtoff;
return snprintf(s, n, "%+.2d%.2d", val/3600, abs(val%3600)/60); snprintf(*s, sizeof *s, "%+.2d%.2d", val/3600, abs(val%3600)/60);
return *s;
case 'Z': case 'Z':
return snprintf(s, n, "%s", tm->__tm_zone); return tm->__tm_zone;
case '%': case '%':
s[0] = '%'; return "%";
s[1] = 0;
return 1;
default: default:
return 0; return 0;
} }
number: number:
return snprintf(s, n, fmt, val); snprintf(*s, sizeof *s, fmt, val);
return *s;
nl_strcat: nl_strcat:
return snprintf(s, n, "%s", __nl_langinfo_l(item, loc)); return __nl_langinfo_l(item, loc);
nl_strftime: nl_strftime:
fmt = __nl_langinfo_l(item, loc); fmt = __nl_langinfo_l(item, loc);
recu_strftime: recu_strftime:
return __strftime_l(s, n, fmt, tm, loc); l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
if (!l) return 0;
return *s;
} }
size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc) size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
{ {
size_t l, k; size_t l, k;
for (l=0; *f && l<n; f++) { char buf[100];
const char *t;
for (l=0; l+1<n; f++) {
if (!*f) {
s[l] = 0;
return l;
}
if (*f != '%') { if (*f != '%') {
s[l++] = *f; s[l++] = *f;
continue; continue;
} }
f++; f++;
if (*f == 'E' || *f == 'O') f++; if (*f == 'E' || *f == 'O') f++;
k = __strftime_fmt_1(s+l, n-l, *f, tm, loc); t = __strftime_fmt_1(&buf, *f, tm, loc);
if (!k) return 0; if (!t || (k = strlen(t)) >= n-l)
return 0;
memcpy(s+l, t, k);
l += k; l += k;
} }
if (l >= n) return 0; return 0;
s[l] = 0;
return l;
} }
size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm) size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册