cmArgumentParser: Track pending keyword explicitly

Avoid allocating missing keyword vector unnecessarily.
This commit is contained in:
Brad King
2022-06-17 12:25:04 -04:00
parent a77b9c0ece
commit 84b335c286
2 changed files with 14 additions and 5 deletions
+11 -5
View File
@@ -78,13 +78,12 @@ void Instance::Consume(cm::string_view arg)
{
auto const it = this->Bindings.Find(arg);
if (it != this->Bindings.end()) {
this->FinishKeyword();
this->Keyword = it->first;
if (this->ParsedKeywords != nullptr) {
this->ParsedKeywords->emplace_back(it->first);
}
it->second(*this);
if (this->ExpectValue && this->KeywordsMissingValue != nullptr) {
this->KeywordsMissingValue->emplace_back(it->first);
}
return;
}
@@ -98,11 +97,18 @@ void Instance::Consume(cm::string_view arg)
this->UnparsedArguments->emplace_back(arg);
}
this->ExpectValue = false;
}
void Instance::FinishKeyword()
{
if (this->Keyword.empty()) {
return;
}
if (this->ExpectValue) {
if (this->KeywordsMissingValue != nullptr) {
this->KeywordsMissingValue->pop_back();
this->KeywordsMissingValue->emplace_back(this->Keyword);
}
this->ExpectValue = false;
}
}
+3
View File
@@ -71,6 +71,7 @@ public:
for (cm::string_view arg : args) {
this->Consume(arg);
}
this->FinishKeyword();
}
private:
@@ -80,11 +81,13 @@ private:
std::vector<cm::string_view>* ParsedKeywords = nullptr;
void* Result = nullptr;
cm::string_view Keyword;
std::string* CurrentString = nullptr;
std::vector<std::string>* CurrentList = nullptr;
bool ExpectValue = false;
void Consume(cm::string_view arg);
void FinishKeyword();
template <typename Result>
friend class ::cmArgumentParser;