diff options
author | Craig Scott <craig.scott@crascit.com> | 2018-03-22 19:27:12 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2018-03-22 19:27:24 (GMT) |
commit | 9f2ec9d241b58aa2184c11bd6d863885d1fcdec7 (patch) | |
tree | 5589ebbd92f2d49857deb492c2c03eed57f2ed2d /Source | |
parent | 82df2fe17e81fa4efa9717ec9d83d2915d56aa33 (diff) | |
parent | 768225837db303e8b568e39ade730eea8ac6b573 (diff) | |
download | CMake-9f2ec9d241b58aa2184c11bd6d863885d1fcdec7.zip CMake-9f2ec9d241b58aa2184c11bd6d863885d1fcdec7.tar.gz CMake-9f2ec9d241b58aa2184c11bd6d863885d1fcdec7.tar.bz2 |
Merge topic 'list-sublist'
768225837d list: Add SUBLIST sub-command
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !1874
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmListCommand.cxx | 52 | ||||
-rw-r--r-- | Source/cmListCommand.h | 1 |
2 files changed, 53 insertions, 0 deletions
diff --git a/Source/cmListCommand.cxx b/Source/cmListCommand.cxx index 62d4ea7..821e6d1 100644 --- a/Source/cmListCommand.cxx +++ b/Source/cmListCommand.cxx @@ -57,6 +57,9 @@ bool cmListCommand::InitialPass(std::vector<std::string> const& args, if (subCommand == "SORT") { return this->HandleSortCommand(args); } + if (subCommand == "SUBLIST") { + return this->HandleSublistCommand(args); + } if (subCommand == "REVERSE") { return this->HandleReverseCommand(args); } @@ -427,6 +430,55 @@ bool cmListCommand::HandleSortCommand(std::vector<std::string> const& args) return true; } +bool cmListCommand::HandleSublistCommand(std::vector<std::string> const& args) +{ + if (args.size() != 5) { + std::ostringstream error; + error << "sub-command SUBLIST requires four arguments (" << args.size() - 1 + << " found)."; + this->SetError(error.str()); + return false; + } + + const std::string& listName = args[1]; + const std::string& variableName = args[args.size() - 1]; + + // expand the variable + std::vector<std::string> varArgsExpanded; + if (!this->GetList(varArgsExpanded, listName) || varArgsExpanded.empty()) { + this->Makefile->AddDefinition(variableName, ""); + return true; + } + + const int start = atoi(args[2].c_str()); + const int length = atoi(args[3].c_str()); + + using size_type = decltype(varArgsExpanded)::size_type; + + if (start < 0 || size_type(start) >= varArgsExpanded.size()) { + std::ostringstream error; + error << "begin index: " << start << " is out of range 0 - " + << varArgsExpanded.size() - 1; + this->SetError(error.str()); + return false; + } + if (length < -1) { + std::ostringstream error; + error << "length: " << length << " should be -1 or greater"; + this->SetError(error.str()); + return false; + } + + const size_type end = + (length == -1 || size_type(start + length) > varArgsExpanded.size()) + ? varArgsExpanded.size() + : size_type(start + length); + std::vector<std::string> sublist(varArgsExpanded.begin() + start, + varArgsExpanded.begin() + end); + this->Makefile->AddDefinition(variableName, cmJoin(sublist, ";").c_str()); + return true; +} + bool cmListCommand::HandleRemoveAtCommand(std::vector<std::string> const& args) { if (args.size() < 3) { diff --git a/Source/cmListCommand.h b/Source/cmListCommand.h index d6870e6..d69d8f9 100644 --- a/Source/cmListCommand.h +++ b/Source/cmListCommand.h @@ -42,6 +42,7 @@ protected: bool HandleRemoveItemCommand(std::vector<std::string> const& args); bool HandleRemoveDuplicatesCommand(std::vector<std::string> const& args); bool HandleSortCommand(std::vector<std::string> const& args); + bool HandleSublistCommand(std::vector<std::string> const& args); bool HandleReverseCommand(std::vector<std::string> const& args); bool HandleFilterCommand(std::vector<std::string> const& args); bool FilterRegex(std::vector<std::string> const& args, bool includeMatches, |