diff --git a/storm/String.cpp b/storm/String.cpp index 5631e44..806e3ab 100644 --- a/storm/String.cpp +++ b/storm/String.cpp @@ -469,81 +469,59 @@ void SStrTokenize(const char** string, char* buffer, size_t bufferchars, const c STORM_ASSERT(whitespace); STORM_VALIDATE(whitespace, ERROR_INVALID_PARAMETER); + int32_t checkquotes = SStrChr(whitespace, '"') != nullptr; + int32_t inquotes = 0; int32_t usedquotes = 0; - auto curstring = *string; + const char* currsource = *string; - auto v17 = false; - for (const char* w = whitespace; w && *w; w++) { - if (*w == '"') { - v17 = true; - break; - } - } - - while (*curstring && SStrChr(whitespace, *curstring)) { - if (v17 && *curstring == '"') { - inquotes = 1; + while (*currsource && SStrChr(whitespace, *currsource)) { + if (checkquotes && *currsource == '"') { usedquotes = 1; - curstring++; + inquotes = 1; + currsource++; break; } - curstring++; + currsource++; } - uint32_t bufferlen = 0; + uint32_t destchars = 0; - if (*curstring) { - auto curbuffer = buffer; - - while (v17 && *curstring == '"') { - if (bufferlen && !inquotes) { - goto LABEL_35; + while(*currsource) { + if (checkquotes && *currsource == '"') { + if (destchars && !inquotes) { + break; } - curstring++; usedquotes = 1; - inquotes = inquotes == 0; + inquotes = !inquotes; + currsource++; if (!inquotes) { - goto LABEL_35; - } -LABEL_32: - if (!*curstring) { - goto LABEL_35; + break; } } - - if (inquotes) { -LABEL_29: - if (curbuffer - buffer < bufferchars) { - bufferlen++; - *curbuffer = *curstring; - curbuffer++; + else { + if (!inquotes && SStrChr(whitespace, *currsource)) { + currsource++; + break; } - curstring++; - - goto LABEL_32; + if (destchars + 1 < bufferchars) { + buffer[destchars] = *currsource; + destchars++; + } + currsource++; } - - auto v14 = SStrChr(whitespace, *curstring); - - if (!v14) { - goto LABEL_29; - } - - curstring++; } -LABEL_35: - if (bufferlen < bufferchars) { - buffer[bufferlen] = 0; + if (destchars < bufferchars) { + buffer[destchars] = 0; } - *string = curstring; + *string = currsource; if (quoted) { *quoted = usedquotes; diff --git a/test/String.cpp b/test/String.cpp index ee2bf16..f51e47e 100644 --- a/test/String.cpp +++ b/test/String.cpp @@ -291,6 +291,48 @@ TEST_CASE("SStrTokenize", "[string]") { SStrTokenize(&string, buffer, 1000, " ,", nullptr); 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 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 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]") {