libc/scanf: Some refactoring
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-06-19 01:05:32 +02:00
parent 21cc7e3729
commit 795b0ca8d4
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -108,6 +108,13 @@ static void write_parsed_unsigned_integer(size_t value, int flags, va_list ap)
*va_arg(ap, unsigned int*) = (unsigned int)value; *va_arg(ap, unsigned int*) = (unsigned int)value;
} }
#define WHITESPACE_CHARACTERS " \t\f\r\n\v"
static void skip_whitespace(const char** str)
{
*str += strspn(*str, WHITESPACE_CHARACTERS);
}
extern "C" extern "C"
{ {
int vsscanf(const char* str, const char* format, va_list ap) int vsscanf(const char* str, const char* format, va_list ap)
@ -131,8 +138,8 @@ extern "C"
continue; continue;
} }
format += strspn(format, " \t\f\r\n\v"); skip_whitespace(&format);
str += strspn(str, " \t\f\r\n\v"); skip_whitespace(&str);
if (*str == 0) return parsed; if (*str == 0) return parsed;
continue; continue;
} }
@ -141,7 +148,7 @@ extern "C"
format++; format++;
if (*format == '%') if (*format == '%')
{ {
str += strspn(str, " \t\f\r\n\v"); skip_whitespace(&str);
goto normal; goto normal;
} }
@ -154,8 +161,8 @@ extern "C"
switch (specifier) switch (specifier)
{ {
case 's': { case 's': {
str += strspn(str, " \t\f\r\n\v"); skip_whitespace(&str);
size_t chars = strcspn(str, " \t\f\r\n\v"); size_t chars = strcspn(str, WHITESPACE_CHARACTERS);
if (!chars) return parsed; if (!chars) return parsed;
if ((flags & FLAG_WIDTH) && chars > width) chars = width; if ((flags & FLAG_WIDTH) && chars > width) chars = width;
if (!(flags & FLAG_DISCARD)) if (!(flags & FLAG_DISCARD))
@ -197,28 +204,28 @@ extern "C"
break; break;
} }
case 'd': { case 'd': {
str += strspn(str, " \t\f\r\n\v"); skip_whitespace(&str);
ssize_t value = scan_signed_integer(&str, 10); ssize_t value = scan_signed_integer(&str, 10);
if (!(flags & FLAG_DISCARD)) write_parsed_signed_integer(value, flags, ap); if (!(flags & FLAG_DISCARD)) write_parsed_signed_integer(value, flags, ap);
parsed++; parsed++;
break; break;
} }
case 'i': { case 'i': {
str += strspn(str, " \t\f\r\n\v"); skip_whitespace(&str);
ssize_t value = scan_signed_integer(&str, 0); ssize_t value = scan_signed_integer(&str, 0);
if (!(flags & FLAG_DISCARD)) write_parsed_signed_integer(value, flags, ap); if (!(flags & FLAG_DISCARD)) write_parsed_signed_integer(value, flags, ap);
parsed++; parsed++;
break; break;
} }
case 'o': { case 'o': {
str += strspn(str, " \t\f\r\n\v"); skip_whitespace(&str);
size_t value = scan_unsigned_integer(&str, 8); size_t value = scan_unsigned_integer(&str, 8);
if (!(flags & FLAG_DISCARD)) write_parsed_unsigned_integer(value, flags, ap); if (!(flags & FLAG_DISCARD)) write_parsed_unsigned_integer(value, flags, ap);
parsed++; parsed++;
break; break;
} }
case 'u': { case 'u': {
str += strspn(str, " \t\f\r\n\v"); skip_whitespace(&str);
size_t value = scan_unsigned_integer(&str, 10); size_t value = scan_unsigned_integer(&str, 10);
if (!(flags & FLAG_DISCARD)) write_parsed_unsigned_integer(value, flags, ap); if (!(flags & FLAG_DISCARD)) write_parsed_unsigned_integer(value, flags, ap);
parsed++; parsed++;
@ -226,14 +233,14 @@ extern "C"
} }
case 'X': case 'X':
case 'x': { case 'x': {
str += strspn(str, " \t\f\r\n\v"); skip_whitespace(&str);
size_t value = scan_unsigned_integer(&str, 16); size_t value = scan_unsigned_integer(&str, 16);
if (!(flags & FLAG_DISCARD)) write_parsed_unsigned_integer(value, flags, ap); if (!(flags & FLAG_DISCARD)) write_parsed_unsigned_integer(value, flags, ap);
parsed++; parsed++;
break; break;
} }
case 'p': { case 'p': {
str += strspn(str, " \t\f\r\n\v"); skip_whitespace(&str);
size_t value = scan_unsigned_integer(&str, 16); size_t value = scan_unsigned_integer(&str, 16);
if (!(flags & FLAG_DISCARD)) *va_arg(ap, void**) = (void*)value; if (!(flags & FLAG_DISCARD)) *va_arg(ap, void**) = (void*)value;
parsed++; parsed++;