提交 f5e4efc4 编写于 作者: R Rich Felker

begin refactoring strftime to make adding field widths easier

上级 ecf4e24d
...@@ -44,25 +44,18 @@ static int week_num(const struct tm *tm) ...@@ -44,25 +44,18 @@ static int week_num(const struct tm *tm)
return val; return val;
} }
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, 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)
{ {
nl_item item; nl_item item;
int val; int val;
const char *fmt; const char *fmt;
size_t l; size_t l;
for (l=0; *f && l<n; f++) {
if (*f != '%') { if (n<2) return 0;
literal:
s[l++] = *f; switch (f) {
continue;
}
do_fmt:
switch (*++f) {
case '%':
goto literal;
case 'E':
case 'O':
goto do_fmt;
case 'a': case 'a':
item = ABDAY_1 + tm->tm_wday; item = ABDAY_1 + tm->tm_wday;
goto nl_strcat; goto nl_strcat;
...@@ -103,7 +96,7 @@ do_fmt: ...@@ -103,7 +96,7 @@ do_fmt:
val = tm->tm_year + 1900; val = tm->tm_year + 1900;
if (tm->tm_yday < 3 && week_num(tm) != 1) val--; if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
else if (tm->tm_yday > 360 && week_num(tm) == 1) val++; else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
if (*f=='g') { if (f=='g') {
fmt = "%02d"; fmt = "%02d";
val %= 100; val %= 100;
} }
...@@ -131,8 +124,9 @@ do_fmt: ...@@ -131,8 +124,9 @@ do_fmt:
fmt = "%02d"; fmt = "%02d";
goto number; goto number;
case 'n': case 'n':
s[l++] = '\n'; s[0] = '\n';
continue; 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;
...@@ -147,8 +141,9 @@ do_fmt: ...@@ -147,8 +141,9 @@ do_fmt:
fmt = "%02d"; fmt = "%02d";
goto number; goto number;
case 't': case 't':
s[l++] = '\t'; s[0] = '\t';
continue; s[1] = 0;
return 1;
case 'T': case 'T':
fmt = "%H:%M:%S"; fmt = "%H:%M:%S";
goto recu_strftime; goto recu_strftime;
...@@ -188,24 +183,39 @@ do_fmt: ...@@ -188,24 +183,39 @@ do_fmt:
goto number; goto number;
case 'z': case 'z':
val = -tm->__tm_gmtoff; val = -tm->__tm_gmtoff;
l += snprintf(s+l, n-l, "%+.2d%.2d", val/3600, abs(val%3600)/60); return snprintf(s, n, "%+.2d%.2d", val/3600, abs(val%3600)/60);
continue;
case 'Z': case 'Z':
l += snprintf(s+l, n-l, "%s", tm->__tm_zone); return snprintf(s, n, "%s", tm->__tm_zone);
continue; case '%':
s[0] = '%';
s[1] = 0;
return 1;
default: default:
return 0; return 0;
} }
number: number:
l += snprintf(s+l, n-l, fmt, val); return snprintf(s, n, fmt, val);
continue;
nl_strcat: nl_strcat:
l += snprintf(s+l, n-l, "%s", __nl_langinfo_l(item, loc)); return snprintf(s, n, "%s", __nl_langinfo_l(item, loc));
continue;
nl_strftime: nl_strftime:
fmt = __nl_langinfo_l(item, loc); fmt = __nl_langinfo_l(item, loc);
recu_strftime: recu_strftime:
l += __strftime_l(s+l, n-l, fmt, tm, loc); return __strftime_l(s, n, fmt, tm, 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;
for (l=0; *f && l<n; f++) {
if (*f != '%') {
s[l++] = *f;
continue;
}
f++;
if (*f == 'E' || *f == 'O') f++;
k = __strftime_fmt_1(s+l, n-l, *f, tm, loc);
if (!k) return 0;
l += k;
} }
if (l >= n) return 0; if (l >= n) return 0;
s[l] = 0; s[l] = 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册