forked from Bananymous/banan-os
LibC: Fix strftime implementation
This implementation now passes every musl-libc test :)
This commit is contained in:
parent
c1a32a4041
commit
b6d0950ee9
|
@ -187,7 +187,7 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
|
|
||||||
struct conversion_t
|
struct conversion_t
|
||||||
{
|
{
|
||||||
char flag = '\0';
|
int flag = '\0';
|
||||||
int width = -1;
|
int width = -1;
|
||||||
char modifier = '\0';
|
char modifier = '\0';
|
||||||
};
|
};
|
||||||
|
@ -207,9 +207,9 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
};
|
};
|
||||||
|
|
||||||
const auto append_string =
|
const auto append_string =
|
||||||
[&](const char* string) -> bool
|
[&s, &len, &maxsize](const char* string) -> bool
|
||||||
{
|
{
|
||||||
size_t string_len = strlen(string);
|
const size_t string_len = strlen(string);
|
||||||
if (len + string_len >= maxsize)
|
if (len + string_len >= maxsize)
|
||||||
return false;
|
return false;
|
||||||
strcpy(s + len, string);
|
strcpy(s + len, string);
|
||||||
|
@ -218,7 +218,7 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
};
|
};
|
||||||
|
|
||||||
const auto append_string_from_list =
|
const auto append_string_from_list =
|
||||||
[&]<size_t LIST_SIZE>(int index, const char* const (&list)[LIST_SIZE]) -> bool
|
[&append_string]<size_t LIST_SIZE>(int index, const char* const (&list)[LIST_SIZE]) -> bool
|
||||||
{
|
{
|
||||||
const char* string = "INVALID";
|
const char* string = "INVALID";
|
||||||
if (index >= 0 && index < (int)LIST_SIZE)
|
if (index >= 0 && index < (int)LIST_SIZE)
|
||||||
|
@ -227,15 +227,43 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
};
|
};
|
||||||
|
|
||||||
const auto append_value =
|
const auto append_value =
|
||||||
[&](const char* format, int value) -> bool
|
[&s, &len, &maxsize]<typename T>(const char* format, T value) -> bool
|
||||||
{
|
{
|
||||||
int written = snprintf(s + len, maxsize - len, format, value);
|
const int written = snprintf(s + len, maxsize - len, format, value);
|
||||||
if (len + written >= maxsize)
|
if (len + written >= maxsize)
|
||||||
return false;
|
return false;
|
||||||
len += written;
|
len += written;
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const auto append_value_weird =
|
||||||
|
[&append_string, &append_value](long long value, int flag, int width) -> bool
|
||||||
|
{
|
||||||
|
char format[32];
|
||||||
|
char* ptr = format;
|
||||||
|
*ptr++ = '%';
|
||||||
|
if (flag == '+')
|
||||||
|
*ptr++ = '+';
|
||||||
|
*ptr++ = '0';
|
||||||
|
if (width != -1)
|
||||||
|
ptr += sprintf(ptr, "%d", width);
|
||||||
|
*ptr++ = 'l';
|
||||||
|
*ptr++ = 'l';
|
||||||
|
*ptr++ = 'd';
|
||||||
|
*ptr++ = '\0';
|
||||||
|
|
||||||
|
// idk why but musl libc test says that +4Y -> 2016 and +10F -> 2016-01-03
|
||||||
|
// i have no idea why the + is not printed in those cases :)
|
||||||
|
if (width < 11 && flag == '+')
|
||||||
|
{
|
||||||
|
char temp_buffer[12];
|
||||||
|
int nprint = sprintf(temp_buffer, format, value);
|
||||||
|
return append_string(temp_buffer + (nprint == width + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
return append_value(format, value);
|
||||||
|
};
|
||||||
|
|
||||||
while (*format && len < maxsize)
|
while (*format && len < maxsize)
|
||||||
{
|
{
|
||||||
if (*format != '%')
|
if (*format != '%')
|
||||||
|
@ -247,20 +275,14 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
format++;
|
format++;
|
||||||
|
|
||||||
conversion_t conversion;
|
conversion_t conversion;
|
||||||
switch (*format)
|
if (*format == '0' || *format == '+')
|
||||||
{
|
conversion.flag = *format++;
|
||||||
case '+':
|
|
||||||
case '0':
|
|
||||||
conversion.flag = *format;
|
|
||||||
format++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (isdigit(*format))
|
if (isdigit(*format))
|
||||||
{
|
{
|
||||||
conversion.width = 0;
|
conversion.width = 0;
|
||||||
while (isdigit(*format))
|
while (isdigit(*format))
|
||||||
{
|
{
|
||||||
conversion.width = (conversion.width * 10) + (*format + '0');
|
conversion.width = (conversion.width * 10) + (*format - '0');
|
||||||
format++;
|
format++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -300,16 +322,15 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
break;
|
break;
|
||||||
case 'C':
|
case 'C':
|
||||||
{
|
{
|
||||||
if (conversion.flag == '\0')
|
if (conversion.width == -1)
|
||||||
conversion.flag = ' ';
|
|
||||||
if (conversion.flag == '+' && conversion.width <= 2)
|
|
||||||
conversion.flag = '0';
|
|
||||||
if (conversion.width < 2)
|
|
||||||
conversion.width = 2;
|
conversion.width = 2;
|
||||||
|
|
||||||
char new_format[32];
|
char new_format[32];
|
||||||
sprintf(new_format, "%%%c%dd", conversion.flag, conversion.width);
|
if (conversion.flag == '+')
|
||||||
if (!append_value(new_format, timeptr->tm_year % 100))
|
sprintf(new_format, "%%+0%dd", conversion.width);
|
||||||
|
else
|
||||||
|
sprintf(new_format, "%%0%dd", conversion.width);
|
||||||
|
if (!append_value(new_format, (1900 + timeptr->tm_year) / 100))
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -328,15 +349,19 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
break;
|
break;
|
||||||
case 'F':
|
case 'F':
|
||||||
{
|
{
|
||||||
if (conversion.flag == '\0')
|
// remove trailing "-mm-dd" from width
|
||||||
conversion.flag = '+';
|
if (conversion.width >= 6)
|
||||||
if (conversion.width == -1)
|
conversion.width -= 6;
|
||||||
conversion.width = 10;
|
|
||||||
if (conversion.width < 6)
|
|
||||||
conversion.width = 6;
|
|
||||||
|
|
||||||
char new_format[32];
|
char new_format[32];
|
||||||
sprintf(new_format, "%%%c%dY-%%m-%%d", conversion.flag, conversion.width - 6);
|
char* ptr = new_format;
|
||||||
|
|
||||||
|
*ptr++ = '%';
|
||||||
|
if (conversion.flag)
|
||||||
|
*ptr++ = conversion.flag;
|
||||||
|
if (conversion.width != -1)
|
||||||
|
ptr += sprintf(ptr, "%d", conversion.width);
|
||||||
|
strcpy(ptr, "Y-%m-%d");
|
||||||
|
|
||||||
if (size_t ret = strftime(s + len, maxsize - len, new_format, timeptr))
|
if (size_t ret = strftime(s + len, maxsize - len, new_format, timeptr))
|
||||||
len += ret;
|
len += ret;
|
||||||
|
@ -380,6 +405,13 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
len += ret;
|
len += ret;
|
||||||
else return 0;
|
else return 0;
|
||||||
break;
|
break;
|
||||||
|
case 's':
|
||||||
|
{
|
||||||
|
struct tm tm_copy = *timeptr;
|
||||||
|
if (!append_value("%llu", mktime(&tm_copy)))
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 'S':
|
case 'S':
|
||||||
if (!append_value("%02d", timeptr->tm_sec))
|
if (!append_value("%02d", timeptr->tm_sec))
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -406,22 +438,10 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
{
|
{
|
||||||
// Adapted from GNU libc implementation
|
// Adapted from GNU libc implementation
|
||||||
|
|
||||||
constexpr auto is_leap_year =
|
|
||||||
[](int year) -> bool
|
|
||||||
{
|
|
||||||
if (year % 400 == 0)
|
|
||||||
return true;
|
|
||||||
if (year % 100 == 0)
|
|
||||||
return false;
|
|
||||||
if (year % 4 == 0)
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
constexpr auto iso_week_days =
|
constexpr auto iso_week_days =
|
||||||
[](int yday, int wday) -> int
|
[](int yday, int wday) -> int
|
||||||
{
|
{
|
||||||
return yday - (wday + 382) % 7 + 3;
|
return yday - (yday - wday + 382) % 7 + 3;
|
||||||
};
|
};
|
||||||
|
|
||||||
int year = timeptr->tm_year + 1900;
|
int year = timeptr->tm_year + 1900;
|
||||||
|
@ -449,20 +469,13 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
break;
|
||||||
case 'G':
|
case 'G':
|
||||||
{
|
if (conversion.flag == '\0' && 1900 + year > 9999)
|
||||||
if (conversion.flag == '\0')
|
conversion.flag = '+';
|
||||||
conversion.flag = ' ';
|
|
||||||
if (conversion.flag == '+' && conversion.width <= 4)
|
|
||||||
conversion.flag = '0';
|
|
||||||
if (conversion.width == -1)
|
if (conversion.width == -1)
|
||||||
conversion.width = 0;
|
conversion.width = 4;
|
||||||
|
if (!append_value_weird(year, conversion.flag, conversion.width))
|
||||||
char new_format[32];
|
|
||||||
sprintf(new_format, "%%%c%dd", conversion.flag, conversion.width);
|
|
||||||
if (!append_value(new_format, year))
|
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case 'V':
|
case 'V':
|
||||||
if (!append_value("%02d", days / 7 + 1))
|
if (!append_value("%02d", days / 7 + 1))
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -489,24 +502,17 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
||||||
else return 0;
|
else return 0;
|
||||||
break;
|
break;
|
||||||
case 'y':
|
case 'y':
|
||||||
if (!append_value("%d", timeptr->tm_yday % 100))
|
if (!append_value("%02d", timeptr->tm_year % 100))
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
break;
|
||||||
case 'Y':
|
case 'Y':
|
||||||
{
|
if (conversion.flag == '\0' && timeptr->tm_year > 9999 - 1900)
|
||||||
if (conversion.flag == '\0')
|
conversion.flag = '+';
|
||||||
conversion.flag = ' ';
|
|
||||||
if (conversion.flag == '+' && conversion.width <= 4)
|
|
||||||
conversion.flag = '0';
|
|
||||||
if (conversion.width == -1)
|
if (conversion.width == -1)
|
||||||
conversion.width = 0;
|
conversion.width = 4;
|
||||||
|
if (!append_value_weird(1900ll + timeptr->tm_year, conversion.flag, conversion.width))
|
||||||
char new_format[32];
|
|
||||||
sprintf(new_format, "%%%c%dd", conversion.flag, conversion.width);
|
|
||||||
if (!append_value(new_format, 1900 + timeptr->tm_year))
|
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case 'z':
|
case 'z':
|
||||||
// FIXME: support timezones
|
// FIXME: support timezones
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue