diff options
author | Brad King <brad.king@kitware.com> | 2018-03-20 13:39:45 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2018-03-20 13:40:40 (GMT) |
commit | d41abae70f282c718f3109b7f76644adce41ff37 (patch) | |
tree | a1e04513a8df7dc1befba5878d1c6d341abe6581 | |
parent | 3b99c9689aae478708c83cafa552c4ce7bbf4a1b (diff) | |
parent | a58158727be4585f9fd71449e9cc9e801c59a009 (diff) | |
download | CMake-d41abae70f282c718f3109b7f76644adce41ff37.zip CMake-d41abae70f282c718f3109b7f76644adce41ff37.tar.gz CMake-d41abae70f282c718f3109b7f76644adce41ff37.tar.bz2 |
Merge topic 'list-join'
a58158727b list(): add `JOIN` sub-command
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !1846
-rw-r--r-- | Help/command/list.rst | 11 | ||||
-rw-r--r-- | Help/command/string.rst | 4 | ||||
-rw-r--r-- | Help/release/dev/list-join.rst | 5 | ||||
-rw-r--r-- | Source/cmListCommand.cxx | 31 | ||||
-rw-r--r-- | Source/cmListCommand.h | 1 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN-NoArguments-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN-NoArguments-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN-NoArguments.cmake | 1 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN-NoVariable-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN-NoVariable-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN-NoVariable.cmake | 1 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN-TooManyArguments-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN-TooManyArguments-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN-TooManyArguments.cmake | 1 | ||||
-rw-r--r-- | Tests/RunCMake/list/JOIN.cmake | 18 | ||||
-rw-r--r-- | Tests/RunCMake/list/RunCMakeTest.cmake | 5 |
16 files changed, 93 insertions, 0 deletions
diff --git a/Help/command/list.rst b/Help/command/list.rst index e6a7069..3c092bd 100644 --- a/Help/command/list.rst +++ b/Help/command/list.rst @@ -54,6 +54,17 @@ GET Returns the list of elements specified by indices from the list. +JOIN +"""" + +:: + + list(JOIN <list> <glue> <output variable>) + +Returns a string joining all list's elements using the glue string. +To join multiple strings, which are not part of a list, use ``JOIN`` operator +from :command:`string` command. + Search ^^^^^^ diff --git a/Help/command/string.rst b/Help/command/string.rst index cd94bb4..bb07d29 100644 --- a/Help/command/string.rst +++ b/Help/command/string.rst @@ -161,6 +161,10 @@ JOIN Join all the input arguments together using the glue string and store the result in the named output variable. +To join list's elements, use preferably the ``JOIN`` operator +from :command:`list` command. This allows for the elements to have +special characters like ``;`` in them. + TOLOWER """"""" diff --git a/Help/release/dev/list-join.rst b/Help/release/dev/list-join.rst new file mode 100644 index 0000000..0756b60 --- /dev/null +++ b/Help/release/dev/list-join.rst @@ -0,0 +1,5 @@ +list-join +--------- + +* The :command:`list` command learned a ``JOIN`` sub-command + to concatenate list's elements separated by a glue string. diff --git a/Source/cmListCommand.cxx b/Source/cmListCommand.cxx index ae4f0a8..62d4ea7 100644 --- a/Source/cmListCommand.cxx +++ b/Source/cmListCommand.cxx @@ -42,6 +42,9 @@ bool cmListCommand::InitialPass(std::vector<std::string> const& args, if (subCommand == "INSERT") { return this->HandleInsertCommand(args); } + if (subCommand == "JOIN") { + return this->HandleJoinCommand(args); + } if (subCommand == "REMOVE_AT") { return this->HandleRemoveAtCommand(args); } @@ -294,6 +297,34 @@ bool cmListCommand::HandleInsertCommand(std::vector<std::string> const& args) return true; } +bool cmListCommand::HandleJoinCommand(std::vector<std::string> const& args) +{ + if (args.size() != 4) { + std::ostringstream error; + error << "sub-command JOIN requires three arguments (" << args.size() - 1 + << " found)."; + this->SetError(error.str()); + return false; + } + + const std::string& listName = args[1]; + const std::string& glue = args[2]; + const std::string& variableName = args[3]; + + // expand the variable + std::vector<std::string> varArgsExpanded; + if (!this->GetList(varArgsExpanded, listName)) { + this->Makefile->AddDefinition(variableName, ""); + return true; + } + + std::string value = + cmJoin(cmMakeRange(varArgsExpanded.begin(), varArgsExpanded.end()), glue); + + this->Makefile->AddDefinition(variableName, value.c_str()); + return true; +} + bool cmListCommand::HandleRemoveItemCommand( std::vector<std::string> const& args) { diff --git a/Source/cmListCommand.h b/Source/cmListCommand.h index 2965399..d6870e6 100644 --- a/Source/cmListCommand.h +++ b/Source/cmListCommand.h @@ -37,6 +37,7 @@ protected: bool HandleAppendCommand(std::vector<std::string> const& args); bool HandleFindCommand(std::vector<std::string> const& args); bool HandleInsertCommand(std::vector<std::string> const& args); + bool HandleJoinCommand(std::vector<std::string> const& args); bool HandleRemoveAtCommand(std::vector<std::string> const& args); bool HandleRemoveItemCommand(std::vector<std::string> const& args); bool HandleRemoveDuplicatesCommand(std::vector<std::string> const& args); diff --git a/Tests/RunCMake/list/JOIN-NoArguments-result.txt b/Tests/RunCMake/list/JOIN-NoArguments-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/list/JOIN-NoArguments-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/list/JOIN-NoArguments-stderr.txt b/Tests/RunCMake/list/JOIN-NoArguments-stderr.txt new file mode 100644 index 0000000..5e1b98f --- /dev/null +++ b/Tests/RunCMake/list/JOIN-NoArguments-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at JOIN-NoArguments.cmake:1 \(list\): + list sub-command JOIN requires three arguments \(1 found\). +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\)$ diff --git a/Tests/RunCMake/list/JOIN-NoArguments.cmake b/Tests/RunCMake/list/JOIN-NoArguments.cmake new file mode 100644 index 0000000..2ab449a --- /dev/null +++ b/Tests/RunCMake/list/JOIN-NoArguments.cmake @@ -0,0 +1 @@ +list(JOIN mylist) diff --git a/Tests/RunCMake/list/JOIN-NoVariable-result.txt b/Tests/RunCMake/list/JOIN-NoVariable-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/list/JOIN-NoVariable-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/list/JOIN-NoVariable-stderr.txt b/Tests/RunCMake/list/JOIN-NoVariable-stderr.txt new file mode 100644 index 0000000..db1d773 --- /dev/null +++ b/Tests/RunCMake/list/JOIN-NoVariable-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at JOIN-NoVariable.cmake:1 \(list\): + list sub-command JOIN requires three arguments \(2 found\). +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\)$ diff --git a/Tests/RunCMake/list/JOIN-NoVariable.cmake b/Tests/RunCMake/list/JOIN-NoVariable.cmake new file mode 100644 index 0000000..b60d8f1 --- /dev/null +++ b/Tests/RunCMake/list/JOIN-NoVariable.cmake @@ -0,0 +1 @@ +list(JOIN mylist "glue") diff --git a/Tests/RunCMake/list/JOIN-TooManyArguments-result.txt b/Tests/RunCMake/list/JOIN-TooManyArguments-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/list/JOIN-TooManyArguments-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/list/JOIN-TooManyArguments-stderr.txt b/Tests/RunCMake/list/JOIN-TooManyArguments-stderr.txt new file mode 100644 index 0000000..2b09e22 --- /dev/null +++ b/Tests/RunCMake/list/JOIN-TooManyArguments-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at JOIN-TooManyArguments.cmake:1 \(list\): + list sub-command JOIN requires three arguments \(4 found\). +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\)$ diff --git a/Tests/RunCMake/list/JOIN-TooManyArguments.cmake b/Tests/RunCMake/list/JOIN-TooManyArguments.cmake new file mode 100644 index 0000000..dc651f6 --- /dev/null +++ b/Tests/RunCMake/list/JOIN-TooManyArguments.cmake @@ -0,0 +1 @@ +list(JOIN mylist "glue" out one_too_many) diff --git a/Tests/RunCMake/list/JOIN.cmake b/Tests/RunCMake/list/JOIN.cmake new file mode 100644 index 0000000..24a623e --- /dev/null +++ b/Tests/RunCMake/list/JOIN.cmake @@ -0,0 +1,18 @@ +list(JOIN undefList % out) +if(NOT out STREQUAL "") + message(FATAL_ERROR "\"list(JOIN undefList % out)\" set out to \"${out}\"") +endif() +set(myList a) +list(JOIN myList % out) +if(NOT out STREQUAL "a") + message(FATAL_ERROR "\"list(JOIN \"a\" % out)\" set out to \"${out}\"") +endif() +set(myList a b) +list(JOIN myList % out) +if(NOT out STREQUAL "a%b") + message(FATAL_ERROR "\"list(JOIN \"a;b\" % out)\" set out to \"${out}\"") +endif() +list(JOIN myList "" out) +if(NOT out STREQUAL "ab") + message(FATAL_ERROR "\"list(JOIN \"a;b\" \"\" out a)\" set out to \"${out}\"") +endif() diff --git a/Tests/RunCMake/list/RunCMakeTest.cmake b/Tests/RunCMake/list/RunCMakeTest.cmake index b002ab3..1be4f85 100644 --- a/Tests/RunCMake/list/RunCMakeTest.cmake +++ b/Tests/RunCMake/list/RunCMakeTest.cmake @@ -15,6 +15,7 @@ run_cmake(INSERT-InvalidIndex) run_cmake(REMOVE_AT-InvalidIndex) run_cmake(FILTER-REGEX-TooManyArguments) +run_cmake(JOIN-TooManyArguments) run_cmake(LENGTH-TooManyArguments) run_cmake(REMOVE_DUPLICATES-TooManyArguments) run_cmake(REVERSE-TooManyArguments) @@ -31,3 +32,7 @@ run_cmake(FILTER-REGEX-InvalidMode) run_cmake(FILTER-REGEX-InvalidOperator) run_cmake(FILTER-REGEX-Valid0) run_cmake(FILTER-REGEX-Valid1) + +run_cmake(JOIN-NoArguments) +run_cmake(JOIN-NoVariable) +run_cmake(JOIN) |