mirror of
https://github.com/thunderbrewhq/squall.git
synced 2026-02-04 08:59:07 +00:00
chore(str): kill gotos in SStrTokenize (#42)
This commit is contained in:
parent
aaa44dd400
commit
0854138653
2 changed files with 70 additions and 50 deletions
|
|
@ -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 == '"') {
|
usedquotes = 1;
|
||||||
v17 = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (*curstring && SStrChr(whitespace, *curstring)) {
|
|
||||||
if (v17 && *curstring == '"') {
|
|
||||||
inquotes = 1;
|
inquotes = 1;
|
||||||
usedquotes = 1;
|
currsource++;
|
||||||
curstring++;
|
|
||||||
|
|
||||||
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) {
|
else {
|
||||||
goto LABEL_35;
|
if (!inquotes && SStrChr(whitespace, *currsource)) {
|
||||||
|
currsource++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (destchars + 1 < bufferchars) {
|
||||||
|
buffer[destchars] = *currsource;
|
||||||
|
destchars++;
|
||||||
|
}
|
||||||
|
currsource++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inquotes) {
|
if (destchars < bufferchars) {
|
||||||
LABEL_29:
|
buffer[destchars] = 0;
|
||||||
if (curbuffer - buffer < bufferchars) {
|
|
||||||
bufferlen++;
|
|
||||||
*curbuffer = *curstring;
|
|
||||||
curbuffer++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
curstring++;
|
*string = currsource;
|
||||||
|
|
||||||
goto LABEL_32;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto v14 = SStrChr(whitespace, *curstring);
|
|
||||||
|
|
||||||
if (!v14) {
|
|
||||||
goto LABEL_29;
|
|
||||||
}
|
|
||||||
|
|
||||||
curstring++;
|
|
||||||
}
|
|
||||||
|
|
||||||
LABEL_35:
|
|
||||||
if (bufferlen < bufferchars) {
|
|
||||||
buffer[bufferlen] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
*string = curstring;
|
|
||||||
|
|
||||||
if (quoted) {
|
if (quoted) {
|
||||||
*quoted = usedquotes;
|
*quoted = usedquotes;
|
||||||
|
|
|
||||||
|
|
@ -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, " \"", "ed);
|
||||||
|
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, " ", "ed);
|
||||||
|
std::string result = buffer;
|
||||||
|
REQUIRE(result == token.first);
|
||||||
|
REQUIRE(quoted == token.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("SStrToFloat", "[string]") {
|
TEST_CASE("SStrToFloat", "[string]") {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue