Browse Source

ENH: Fix INSERT to allow inserting to empty list

Andy Cedilnik 19 years ago
parent
commit
ed1ea24cef
2 changed files with 23 additions and 16 deletions
  1. 17 14
      Source/cmListCommand.cxx
  2. 6 2
      Tests/CMakeTests/ListTest.cmake.in

+ 17 - 14
Source/cmListCommand.cxx

@@ -205,27 +205,30 @@ bool cmListCommand::HandleInsertCommand(std::vector<std::string> const& args)
   // expand the variable
   int item = atoi(args[2].c_str());
   std::vector<std::string> varArgsExpanded;
-  if ( !this->GetList(varArgsExpanded, listName.c_str()) && (item > 0 || item < -1))
+  if ( !this->GetList(varArgsExpanded, listName.c_str()) && item != 0)
     {
     cmOStringStream str;
-    str << "index: " << item << " out of range (-1, 0)";
+    str << "index: " << item << " out of range (0, 0)";
     this->SetError(str.str().c_str());
     return false;
     }
 
-  size_t nitem = varArgsExpanded.size();
-  if ( item < 0 )
+  if ( varArgsExpanded.size() != 0 )
     {
-    item = (int)nitem + item;
-    }
-  if ( item < 0 || nitem <= (size_t)item )
-    {
-    cmOStringStream str;
-    str << "index: " << item << " out of range (-" 
-      << varArgsExpanded.size() << ", " 
-      << varArgsExpanded.size()-1 << ")";
-    this->SetError(str.str().c_str());
-    return false;
+    size_t nitem = varArgsExpanded.size();
+    if ( item < 0 )
+      {
+      item = (int)nitem + item;
+      }
+    if ( item < 0 || nitem <= (size_t)item )
+      {
+      cmOStringStream str;
+      str << "index: " << item << " out of range (-" 
+        << varArgsExpanded.size() << ", " 
+        << (varArgsExpanded.size() == 0?0:(varArgsExpanded.size()-1)) << ")";
+      this->SetError(str.str().c_str());
+      return false;
+      }
     }
   size_t cc;
   size_t cnt = 0;

+ 6 - 2
Tests/CMakeTests/ListTest.cmake.in

@@ -1,6 +1,6 @@
 MACRO(TEST command expected)
   IF("x${result}" STREQUAL "x${expected}")
-    MESSAGE("TEST \"${command}\" success: \"${result}\" expected: \"${expected}\"")
+    #MESSAGE("TEST \"${command}\" success: \"${result}\" expected: \"${expected}\"")
   ELSE("x${result}" STREQUAL "x${expected}")
     MESSAGE(SEND_ERROR "${CMAKE_CURRENT_LIST_LINE}: TEST \"${command}\" failed: \"${result}\" expected: \"${expected}\"")
   ENDIF("x${result}" STREQUAL "x${expected}")
@@ -41,7 +41,11 @@ TEST("APPEND result brad" "andy;brad")
 
 LIST(APPEND "nonexiting_list3" brad)
 SET(result "${nonexiting_list3}")
-TEST("APPEND \"nonexiting_list1\" brad" "brad")
+TEST("APPEND \"nonexiting_list3\" brad" "brad")
+
+LIST(INSERT "nonexiting_list4" 0 andy bill brad ken)
+SET(result "${nonexiting_list4}")
+TEST("APPEND \"nonexiting_list4\" andy bill brad ken" "andy;bill;brad;ken")
 
 SET(result andy brad)
 LIST(INSERT result -1 bill ken)