From 84b335c2868d245841f08656b96cad3d9df10422 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 17 Jun 2022 12:25:04 -0400 Subject: cmArgumentParser: Track pending keyword explicitly Avoid allocating missing keyword vector unnecessarily. --- Source/cmArgumentParser.cxx | 16 +++++++++++----- Source/cmArgumentParser.h | 3 +++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Source/cmArgumentParser.cxx b/Source/cmArgumentParser.cxx index 355cfdc..df008a2 100644 --- a/Source/cmArgumentParser.cxx +++ b/Source/cmArgumentParser.cxx @@ -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; } } diff --git a/Source/cmArgumentParser.h b/Source/cmArgumentParser.h index 7cbef92..2d96f07 100644 --- a/Source/cmArgumentParser.h +++ b/Source/cmArgumentParser.h @@ -71,6 +71,7 @@ public: for (cm::string_view arg : args) { this->Consume(arg); } + this->FinishKeyword(); } private: @@ -80,11 +81,13 @@ private: std::vector* ParsedKeywords = nullptr; void* Result = nullptr; + cm::string_view Keyword; std::string* CurrentString = nullptr; std::vector* CurrentList = nullptr; bool ExpectValue = false; void Consume(cm::string_view arg); + void FinishKeyword(); template friend class ::cmArgumentParser; -- cgit v0.12