Skip to content

Commit

Permalink
Added support for "%[]" sscanf syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 24, 2023
1 parent a844d90 commit 361952b
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
71 changes: 71 additions & 0 deletions src/stdlib/SDL_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,39 @@ int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
return vsscanf(text, fmt, ap);
}
#else
static SDL_bool CharacterMatchesSet(char c, const char *set, size_t set_len)
{
SDL_bool invert = SDL_FALSE;
SDL_bool result = SDL_FALSE;

if (*set == '^') {
invert = SDL_TRUE;
++set;
--set_len;
}
while (set_len > 0 && !result) {
if (set_len >= 3 && set[1] == '-') {
char low_char = SDL_min(set[0], set[2]);
char high_char = SDL_max(set[0], set[2]);
if (c >= low_char && c <= high_char) {
result = SDL_TRUE;
}
set += 3;
set_len -= 3;
} else {
if (c == *set) {
result = SDL_TRUE;
}
++set;
--set_len;
}
}
if (invert) {
result = result ? SDL_FALSE : SDL_TRUE;
}
return result;
}

/* NOLINTNEXTLINE(readability-non-const-parameter) */
int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
{
Expand Down Expand Up @@ -1472,6 +1505,44 @@ int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
}
done = SDL_TRUE;
break;
case '[':
{
const char *set = fmt + 1;
while (*fmt && *fmt != ']') {
++fmt;
}
if (*fmt) {
size_t set_len = (fmt - set);
if (suppress) {
while (CharacterMatchesSet(*text, set, set_len)) {
++text;
if (count) {
if (--count == 0) {
break;
}
}
}
} else {
SDL_bool had_match = SDL_FALSE;
char *valuep = va_arg(ap, char *);
while (CharacterMatchesSet(*text, set, set_len)) {
had_match = SDL_TRUE;
*valuep++ = *text++;
if (count) {
if (--count == 0) {
break;
}
}
}
*valuep = '\0';
if (had_match) {
++retval;
}
}
}
}
done = SDL_TRUE;
break;
default:
done = SDL_TRUE;
break;
Expand Down
78 changes: 77 additions & 1 deletion test/testautomation_stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ static int stdlib_sscanf(void *arg)
long long_output, expected_long_output;
long long long_long_output, expected_long_long_output;
size_t size_output, expected_size_output;
char text[128];
char text[128], text2[128];

expected_output = output = 123;
expected_result = -1;
Expand Down Expand Up @@ -649,6 +649,82 @@ static int stdlib_sscanf(void *arg)
SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc def", "%s", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%s\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%s", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%s\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc,def") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%[cba]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[cba]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%[a-z]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[z-a]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%[^,]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[^,]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 0;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%[A-Z]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[A-Z]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "") == 0, "Check output, expected: \"\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 2;
text[0] = '\0';
text2[0] = '\0';
result = SDL_sscanf("abc,def", "%[abc],%[def]", text, text2);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc],%%[def]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 2;
text[0] = '\0';
text2[0] = '\0';
result = SDL_sscanf("abc,def", "%[abc]%*[,]%[def]", text, text2);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc]%%*[,]%%[def]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 2;
text[0] = '\0';
text2[0] = '\0';
result = SDL_sscanf("abc def", "%[abc] %[def]", text, text2);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%[abc] %%[def]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc123XYZ", "%[a-zA-Z0-9]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc123XYZ\", \"%%[a-zA-Z0-9]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc123XYZ") == 0, "Check output, expected: \"abc123XYZ\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);

return TEST_COMPLETED;
}

Expand Down

0 comments on commit 361952b

Please sign in to comment.