cmGeneratorExpression: Improve parsing in StripEmptyListElements

The char-by-char parsing causes lots of reallocations which shouldn't be
necessary. To improve this, fast-path strings without a semicolon,
reserve space in the result, and insert into the result in chunks.
This commit is contained in:
Ben Boeckel 2014-02-09 05:09:52 -05:00
parent 68eb175744
commit 7c565d2fd5
1 changed files with 10 additions and 3 deletions

View File

@ -157,17 +157,24 @@ cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression()
std::string cmGeneratorExpression::StripEmptyListElements( std::string cmGeneratorExpression::StripEmptyListElements(
const std::string &input) const std::string &input)
{ {
if (input.find(';') == input.npos)
{
return input;
}
std::string result; std::string result;
result.reserve(input.size());
const char *c = input.c_str(); const char *c = input.c_str();
const char *last = c;
bool skipSemiColons = true; bool skipSemiColons = true;
for ( ; *c; ++c) for ( ; *c; ++c)
{ {
if(c[0] == ';') if(*c == ';')
{ {
if(skipSemiColons) if(skipSemiColons)
{ {
continue; result.append(last, c - last);
last = c + 1;
} }
skipSemiColons = true; skipSemiColons = true;
} }
@ -175,8 +182,8 @@ std::string cmGeneratorExpression::StripEmptyListElements(
{ {
skipSemiColons = false; skipSemiColons = false;
} }
result += *c;
} }
result.append(last);
if (!result.empty() && *(result.end() - 1) == ';') if (!result.empty() && *(result.end() - 1) == ';')
{ {