summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorMarc Chevrier <marc.chevrier@sap.com>2018-03-16 13:12:25 (GMT)
committerBrad King <brad.king@kitware.com>2018-03-21 16:54:53 (GMT)
commit768225837db303e8b568e39ade730eea8ac6b573 (patch)
tree8438f1112f7eae9ee54afbbdd958af55f57e94c2 /Source
parentd41abae70f282c718f3109b7f76644adce41ff37 (diff)
downloadCMake-768225837db303e8b568e39ade730eea8ac6b573.zip
CMake-768225837db303e8b568e39ade730eea8ac6b573.tar.gz
CMake-768225837db303e8b568e39ade730eea8ac6b573.tar.bz2
list: Add SUBLIST sub-command
Issue: #17823
Diffstat (limited to 'Source')
-rw-r--r--Source/cmListCommand.cxx52
-rw-r--r--Source/cmListCommand.h1
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,