chore(str): kill gotos in SStrTokenize (#42)

This commit is contained in:
Adam Heinermann 2025-04-20 07:15:32 -07:00 committed by GitHub
parent aaa44dd400
commit 0854138653
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 70 additions and 50 deletions

View file

@ -469,81 +469,59 @@ void SStrTokenize(const char** string, char* buffer, size_t bufferchars, const c
STORM_ASSERT(whitespace); STORM_ASSERT(whitespace);
STORM_VALIDATE(whitespace, ERROR_INVALID_PARAMETER); STORM_VALIDATE(whitespace, ERROR_INVALID_PARAMETER);
int32_t checkquotes = SStrChr(whitespace, '"') != nullptr;
int32_t inquotes = 0; int32_t inquotes = 0;
int32_t usedquotes = 0; int32_t usedquotes = 0;
auto curstring = *string; const char* currsource = *string;
auto v17 = false; while (*currsource && SStrChr(whitespace, *currsource)) {
for (const char* w = whitespace; w && *w; w++) { if (checkquotes && *currsource == '"') {
if (*w == '"') {
v17 = true;
break;
}
}
while (*curstring && SStrChr(whitespace, *curstring)) {
if (v17 && *curstring == '"') {
inquotes = 1;
usedquotes = 1; usedquotes = 1;
curstring++; inquotes = 1;
currsource++;
break; break;
} }
curstring++; currsource++;
} }
uint32_t bufferlen = 0; uint32_t destchars = 0;
if (*curstring) { while(*currsource) {
auto curbuffer = buffer; if (checkquotes && *currsource == '"') {
if (destchars && !inquotes) {
while (v17 && *curstring == '"') { break;
if (bufferlen && !inquotes) {
goto LABEL_35;
} }
curstring++;
usedquotes = 1; usedquotes = 1;
inquotes = inquotes == 0; inquotes = !inquotes;
currsource++;
if (!inquotes) { if (!inquotes) {
goto LABEL_35; break;
}
LABEL_32:
if (!*curstring) {
goto LABEL_35;
} }
} }
else {
if (inquotes) { if (!inquotes && SStrChr(whitespace, *currsource)) {
LABEL_29: currsource++;
if (curbuffer - buffer < bufferchars) { break;
bufferlen++;
*curbuffer = *curstring;
curbuffer++;
} }
curstring++; if (destchars + 1 < bufferchars) {
buffer[destchars] = *currsource;
goto LABEL_32; destchars++;
}
currsource++;
} }
auto v14 = SStrChr(whitespace, *curstring);
if (!v14) {
goto LABEL_29;
}
curstring++;
} }
LABEL_35: if (destchars < bufferchars) {
if (bufferlen < bufferchars) { buffer[destchars] = 0;
buffer[bufferlen] = 0;
} }
*string = curstring; *string = currsource;
if (quoted) { if (quoted) {
*quoted = usedquotes; *quoted = usedquotes;

View file

@ -291,6 +291,48 @@ TEST_CASE("SStrTokenize", "[string]") {
SStrTokenize(&string, buffer, 1000, " ,", nullptr); SStrTokenize(&string, buffer, 1000, " ,", nullptr);
REQUIRE(!SStrCmp(buffer, "", STORM_MAX_STR)); REQUIRE(!SStrCmp(buffer, "", STORM_MAX_STR));
} }
SECTION("identifies quoted tokens") {
auto string = "foo bar \"baz bazinga\" bodonkers \"donga dongs\"";
char buffer[100] = { 0 };
std::pair<const char*, int> tokens[] = {
std::make_pair("foo", 0),
std::make_pair("bar", 0),
std::make_pair("baz bazinga", 1),
std::make_pair("bodonkers", 0),
std::make_pair("donga dongs", 1)
};
for (auto& token : tokens) {
int quoted = 0;
SStrTokenize(&string, buffer, 1000, " \"", &quoted);
std::string result = buffer;
REQUIRE(result == token.first);
REQUIRE(quoted == token.second);
}
}
SECTION("doesn't identify quoted tokens if excluded from whitespace") {
auto string = "foo bar \"baz bazinga\" bodonkers \"donga dongs\"";
char buffer[100] = { 0 };
std::pair<const char*, int> tokens[] = {
std::make_pair("foo", 0),
std::make_pair("bar", 0),
std::make_pair("\"baz", 0),
std::make_pair("bazinga\"", 0),
std::make_pair("bodonkers", 0),
std::make_pair("\"donga", 0),
std::make_pair("dongs\"", 0)
};
for (auto& token : tokens) {
int quoted = 0;
SStrTokenize(&string, buffer, 1000, " ", &quoted);
std::string result = buffer;
REQUIRE(result == token.first);
REQUIRE(quoted == token.second);
}
}
} }
TEST_CASE("SStrToFloat", "[string]") { TEST_CASE("SStrToFloat", "[string]") {