diff --git a/Source/cmListCommand.cxx b/Source/cmListCommand.cxx index b546d20fb..f0df03b77 100644 --- a/Source/cmListCommand.cxx +++ b/Source/cmListCommand.cxx @@ -42,6 +42,10 @@ bool cmListCommand::InitialPass(std::vector const& args) { return this->HandleAppendCommand(args); } + if(subCommand == "CONTAINS") + { + return this->HandleContainsCommand(args); + } if(subCommand == "INSERT") { return this->HandleInsertCommand(args); @@ -199,6 +203,39 @@ bool cmListCommand::HandleAppendCommand(std::vector const& args) return true; } +//---------------------------------------------------------------------------- +bool cmListCommand::HandleContainsCommand(std::vector const& args) +{ + if(args.size() != 4) + { + this->SetError("sub-command CONTAINS requires three arguments."); + return false; + } + + const std::string& listName = args[1]; + const std::string& variableName = args[args.size() - 1]; + // expand the variable + std::vector varArgsExpanded; + if ( !this->GetList(varArgsExpanded, listName.c_str()) ) + { + this->Makefile->AddDefinition(variableName.c_str(), "FALSE"); + return true; + } + + std::vector::iterator it; + for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it ) + { + if ( *it == args[2] ) + { + this->Makefile->AddDefinition(variableName.c_str(), "TRUE"); + return true; + } + } + + this->Makefile->AddDefinition(variableName.c_str(), "FALSE"); + return true; +} + //---------------------------------------------------------------------------- bool cmListCommand::HandleInsertCommand(std::vector const& args) { diff --git a/Source/cmListCommand.h b/Source/cmListCommand.h index 2dbe1d80b..d8a4688c7 100644 --- a/Source/cmListCommand.h +++ b/Source/cmListCommand.h @@ -68,20 +68,24 @@ public: " LIST(GET [ ...] " ")\n" " LIST(APPEND [ ...])\n" + " LIST(CONTAINS )\n" " LIST(INSERT [ ...])\n" " LIST(REMOVE_ITEM [ ...])\n" " LIST(REMOVE_AT [ ...])\n" - " LIST(SORT )\n" " LIST(REVERSE )\n" + " LIST(SORT )\n" "LENGTH will return a given list's length.\n" "GET will return list of elements specified by indices from the list.\n" "APPEND will append elements to the list.\n" + "CONTAINS will return TRUE if the element specified is in the list.\n" "INSERT will insert elements to the list to the specified location.\n" "When specifying an index, negative value corresponds to index from the" " end of the list.\n" "REMOVE_AT and REMOVE_ITEM will remove items from the list. The " "difference is that REMOVE_ITEM will remove the given items, while " "REMOVE_AT will remove the items at the given indices.\n" + "REVERSE reverses the contents of the list in-place.\n" + "SORT sorts the list in-place alphabetically.\n" ; } @@ -90,6 +94,7 @@ protected: bool HandleLengthCommand(std::vector const& args); bool HandleGetCommand(std::vector const& args); bool HandleAppendCommand(std::vector const& args); + bool HandleContainsCommand(std::vector const& args); bool HandleInsertCommand(std::vector const& args); bool HandleRemoveAtCommand(std::vector const& args); bool HandleRemoveItemCommand(std::vector const& args); diff --git a/Tests/CMakeTests/ListTest.cmake.in b/Tests/CMakeTests/ListTest.cmake.in index 58a8435a0..38fea144c 100644 --- a/Tests/CMakeTests/ListTest.cmake.in +++ b/Tests/CMakeTests/ListTest.cmake.in @@ -66,3 +66,17 @@ TEST("REMOVE_ITEM result bob" "andy;bill;brad;ken") SET(result andy bill bob brad ken peter) LIST(REMOVE_AT result 2 -1) TEST("REMOVE_AT result 2 -1" "andy;bill;brad;ken") + +LIST(CONTAINS mylist ken result) +TEST("CONTAINS mylist ken result" "TRUE") + +LIST(CONTAINS mylist nobody result) +TEST("CONTAINS mylist nobody result" "FALSE") + +SET(result ken bill andy brad) +LIST(SORT result) +TEST("SORT result" "andy;bill;brad;ken") + +SET(result andy bill brad ken) +LIST(REVERSE result) +TEST("REVERSE result" "ken;brad;bill;andy")