From 2f23ecdb8aa5bc1a86158df551f5649c439c7c52 Mon Sep 17 00:00:00 2001 From: Alexander Neundorf Date: Wed, 15 Aug 2007 10:26:50 -0400 Subject: [PATCH] ENH: change LIST(CONTAINS ...) TO LIST(FIND ...), which returns the index and which is more useful, because then you can also access the item behind the one you were looking, useful for writing macros with optional keywords with parameters Alex --- Source/cmListCommand.cxx | 18 +++++++++++------- Source/cmListCommand.h | 7 ++++--- Tests/CMakeTests/ListTest.cmake.in | 10 ++++++---- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Source/cmListCommand.cxx b/Source/cmListCommand.cxx index f0df03b77c..c00f6ebb51 100644 --- a/Source/cmListCommand.cxx +++ b/Source/cmListCommand.cxx @@ -42,9 +42,9 @@ bool cmListCommand::InitialPass(std::vector const& args) { return this->HandleAppendCommand(args); } - if(subCommand == "CONTAINS") + if(subCommand == "FIND") { - return this->HandleContainsCommand(args); + return this->HandleFindCommand(args); } if(subCommand == "INSERT") { @@ -204,11 +204,11 @@ bool cmListCommand::HandleAppendCommand(std::vector const& args) } //---------------------------------------------------------------------------- -bool cmListCommand::HandleContainsCommand(std::vector const& args) +bool cmListCommand::HandleFindCommand(std::vector const& args) { if(args.size() != 4) { - this->SetError("sub-command CONTAINS requires three arguments."); + this->SetError("sub-command FIND requires three arguments."); return false; } @@ -218,21 +218,25 @@ bool cmListCommand::HandleContainsCommand(std::vector const& args) std::vector varArgsExpanded; if ( !this->GetList(varArgsExpanded, listName.c_str()) ) { - this->Makefile->AddDefinition(variableName.c_str(), "FALSE"); + this->Makefile->AddDefinition(variableName.c_str(), "-1"); return true; } std::vector::iterator it; + unsigned int index = 0; for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it ) { if ( *it == args[2] ) { - this->Makefile->AddDefinition(variableName.c_str(), "TRUE"); + char indexString[32]; + sprintf(indexString, "%d", index); + this->Makefile->AddDefinition(variableName.c_str(), indexString); return true; } + index++; } - this->Makefile->AddDefinition(variableName.c_str(), "FALSE"); + this->Makefile->AddDefinition(variableName.c_str(), "-1"); return true; } diff --git a/Source/cmListCommand.h b/Source/cmListCommand.h index d8a4688c7d..a99604b801 100644 --- a/Source/cmListCommand.h +++ b/Source/cmListCommand.h @@ -68,7 +68,7 @@ public: " LIST(GET [ ...] " ")\n" " LIST(APPEND [ ...])\n" - " LIST(CONTAINS )\n" + " LIST(FIND )\n" " LIST(INSERT [ ...])\n" " LIST(REMOVE_ITEM [ ...])\n" " LIST(REMOVE_AT [ ...])\n" @@ -77,7 +77,8 @@ public: "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" + "FIND will return the index of the element specified in the list or -1 " + "if it wasn't found.\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" @@ -94,7 +95,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 HandleFindCommand(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 38fea144cf..eea0da095d 100644 --- a/Tests/CMakeTests/ListTest.cmake.in +++ b/Tests/CMakeTests/ListTest.cmake.in @@ -67,11 +67,13 @@ 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") +# ken is at index 2, nobody is not in the list so -1 should be returned +SET(mylist andy bill ken brad) +LIST(FIND mylist ken result) +TEST("FIND mylist ken result" "2") -LIST(CONTAINS mylist nobody result) -TEST("CONTAINS mylist nobody result" "FALSE") +LIST(FIND mylist nobody result) +TEST("FIND mylist nobody result" "-1") SET(result ken bill andy brad) LIST(SORT result)