Attached Files | 0002-Added-tests-for-the-FIND-sub-command-of-STRING.patch [^] (4,051 bytes) 2011-02-15 05:27 [Show Content] [Hide Content]From 2900cc049dd0caa4293c5f258c3fb54ffccb0d56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20Hu=CC=88tz?= <tim@huetz.biz>
Date: Tue, 15 Feb 2011 11:15:23 +0100
Subject: [PATCH 2/2] Added tests for the FIND sub-command of STRING
Implemented some new test for verifying the FIND
sub-command of the STRING command.
---
Tests/CMakeTests/StringTest.cmake.in | 2 +-
Tests/CMakeTests/StringTestScript.cmake | 60 +++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 1 deletions(-)
diff --git a/Tests/CMakeTests/StringTest.cmake.in b/Tests/CMakeTests/StringTest.cmake.in
index 54fee7f..2a19ad0 100644
--- a/Tests/CMakeTests/StringTest.cmake.in
+++ b/Tests/CMakeTests/StringTest.cmake.in
@@ -1,7 +1,7 @@
# Execute each test listed in StringTestScript.cmake:
#
set(scriptname "@CMAKE_CURRENT_SOURCE_DIR@/StringTestScript.cmake")
-set(number_of_tests_expected 52)
+set(number_of_tests_expected 66)
include("@CMAKE_CURRENT_SOURCE_DIR@/ExecuteScriptTests.cmake")
execute_all_script_tests(${scriptname} number_of_tests_executed)
diff --git a/Tests/CMakeTests/StringTestScript.cmake b/Tests/CMakeTests/StringTestScript.cmake
index 3703856..785b089 100644
--- a/Tests/CMakeTests/StringTestScript.cmake
+++ b/Tests/CMakeTests/StringTestScript.cmake
@@ -193,6 +193,66 @@ elseif(testname STREQUAL random_with_various_alphabets) # pass
# alphabet of many colors - use all the crazy keyboard characters
string(RANDOM LENGTH 78 ALPHABET "~`!@#$%^&*()_-+={}[]\\|:\\;'\",.<>/?" v)
message(STATUS "v='${v}'")
+
+elseif(testname STREQUAL string_find_with_no_parameter) # fail
+ string(FIND)
+
+elseif(testname STREQUAL string_find_with_one_parameter) # fail
+ string(FIND "CMake is great.")
+
+elseif(testname STREQUAL string_find_with_two_parameters) # fail
+ string(FIND "CMake is great." "a")
+
+elseif(testname STREQUAL string_find_with_three_parameters) # pass
+ string(FIND "CMake is great." "a" v)
+ message(STATUS "v='${v}'")
+
+elseif(testname STREQUAL string_find_with_four_parameters) # fail
+ string(FIND "CMake is great." "a" v v2)
+
+elseif(testname STREQUAL string_find_reverse_with_no_parameter) # fail
+ string(FIND REVERSE)
+
+elseif(testname STREQUAL string_find_reverse_with_one_parameter) # fail
+ string(FIND REVERSE "CMake is great.")
+
+elseif(testname STREQUAL string_find_reverse_with_two_parameters) # fail
+ string(FIND REVERSE "CMake is great." "a")
+
+elseif(testname STREQUAL string_find_reverse_with_three_parameters) # pass
+ string(FIND REVERSE "CMake is great." "a" v)
+ message(STATUS "v='${v}'")
+
+elseif(testname STREQUAL string_find_reverse_with_four_parameters) # fail
+ string(FIND REVERSE "CMake is great." "a" v v2)
+
+elseif(testname STREQUAL string_find_with_no_possible_result) # pass
+ string(FIND "CMake is a great application." "z" v)
+ message(STATUS "v='${v}'")
+ if(NOT(-1 EQUAL ${v}))
+ message(SEND_ERROR "FIND sub-command should return -1 but returned ${v}.")
+ endif(NOT(-1 EQUAL ${v}))
+
+elseif(testname STREQUAL string_find_reverse_with_no_possible_result) # pass
+ string(FIND REVERSE "CMake is a great application." "z" v)
+ message(STATUS "v='${v}'")
+ if(NOT(-1 EQUAL ${v}))
+ message(SEND_ERROR "FIND REVERSE sub-command should return -1 but returned ${v}.")
+ endif(NOT(-1 EQUAL ${v}))
+
+elseif(testname STREQUAL string_find_with_required_result) # pass
+ string(FIND "CMake is a great application." "g" v)
+ message(STATUS "v='${v}'")
+ if(NOT(11 EQUAL ${v}))
+ message(SEND_ERROR "FIND sub-command should return 11 but returned ${v}.")
+ endif(NOT(11 EQUAL ${v}))
+
+elseif(testname STREQUAL string_find_reverse_with_required_result) # pass
+ string(FIND REVERSE "CMake is a great application." "e" v)
+ message(STATUS "v='${v}'")
+ if(NOT(13 EQUAL ${v}))
+ message(SEND_ERROR "FIND REVERSE sub-command should return 13 but returned ${v}.")
+ endif(NOT(13 EQUAL ${v}))
else() # fail
message(FATAL_ERROR "testname='${testname}' - error: no such test in '${CMAKE_CURRENT_LIST_FILE}'")
--
1.7.4
0001-Added-a-new-STRING-sub-command-called-FIND.patch [^] (4,694 bytes) 2011-02-15 09:15 [Show Content] [Hide Content]From 030ecf0f7719fe5a62e46a8cdab1bcfabea122af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20Hu=CC=88tz?= <tim@huetz.biz>
Date: Tue, 15 Feb 2011 11:20:47 +0100
Subject: [PATCH 1/2] Added a new STRING sub-command called FIND
Added the sub-command FIND to the STRING command. This
provides the developer with a function to find for a
specified character is a string. The command will return
the position of the character in the supplied string.
---
Source/cmStringCommand.cxx | 66 +++++++++++++++++++++++++++++++++++++++++++-
Source/cmStringCommand.h | 6 ++++
2 files changed, 71 insertions(+), 1 deletions(-)
diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx
index 949fcf5..3ae23d0 100644
--- a/Source/cmStringCommand.cxx
+++ b/Source/cmStringCommand.cxx
@@ -72,7 +72,11 @@ bool cmStringCommand
{
return this->HandleRandomCommand(args);
}
-
+ else if(subCommand == "FIND")
+ {
+ return this->HandleFindCommand(args);
+ }
+
std::string e = "does not recognize sub-command "+subCommand;
this->SetError(e.c_str());
return false;
@@ -499,6 +503,66 @@ void cmStringCommand::StoreMatches(cmMakefile* mf,cmsys::RegularExpression& re)
}
//----------------------------------------------------------------------------
+bool cmStringCommand::HandleFindCommand(std::vector<std::string> const&
+ args)
+{
+ // check if all required parameters were passed
+ if(args.size() < 4 || args.size() > 5)
+ {
+ this->SetError("sub-command FIND and FIND REVERSE require 3 parameters.");
+ return false;
+ }
+
+ // check if the reverse flag was set or not
+ bool reverseMode = false;
+ if(args[1] == "REVERSE")
+ {
+ reverseMode = true;
+ }
+
+ //
+ if(reverseMode && args.size() != 5)
+ {
+ this->SetError("sub-command FIND REVERSE requires 3 parameters.");
+ return false;
+ }
+
+ //
+ if(!reverseMode && args.size() > 4)
+ {
+ this->SetError("sub-command FIND requires just 3 further parameters.");
+ return false;
+ }
+
+ // get the coorect parameters (depending on the REVERSE flag)
+ const std::string& sstring = args[reverseMode?2:1];
+ const std::string& schar = args[reverseMode?3:2];
+ const std::string& outvar = args[reverseMode?4:3];
+
+ // try to find the character and return its position
+ size_t pos;
+ if(!reverseMode)
+ {
+ pos = sstring.find( schar );
+ }
+ else
+ {
+ pos = sstring.rfind( schar );
+ }
+ if(std::string::npos != pos)
+ {
+ std::stringstream s;
+ s << pos;
+ this->Makefile->AddDefinition(outvar.c_str(), s.str().c_str());
+ return true;
+ }
+
+ // the character was not found, but this is not really an error
+ this->Makefile->AddDefinition(outvar.c_str(), "-1");
+ return true;
+}
+
+//----------------------------------------------------------------------------
bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const&
args)
{
diff --git a/Source/cmStringCommand.h b/Source/cmStringCommand.h
index 2a916b4..96a0f35 100644
--- a/Source/cmStringCommand.h
+++ b/Source/cmStringCommand.h
@@ -90,6 +90,7 @@ public:
" string(STRIP <string> <output variable>)\n"
" string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n"
" [RANDOM_SEED <seed>] <output variable>)\n"
+ " string(FIND [REVERSE] <string> <substring> <output variable>)\n"
"REGEX MATCH will match the regular expression once and store the "
"match in the output variable.\n"
"REGEX MATCHALL will match the regular expression as many times as "
@@ -117,6 +118,10 @@ public:
"characters and default alphabet is all numbers and upper and "
"lower case letters. If an integer RANDOM_SEED is given, its "
"value will be used to seed the random number generator.\n"
+ "FIND will return the position where the given substring was found "
+ "in the supplied string. If the REVERSE flag was used, the command "
+ "will search for the position of the last occurrence of the "
+ "specified substring.\n"
"The following characters have special meaning in regular expressions:\n"
" ^ Matches at beginning of a line\n"
" $ Matches at end of a line\n"
@@ -152,6 +157,7 @@ protected:
bool HandleSubstringCommand(std::vector<std::string> const& args);
bool HandleStripCommand(std::vector<std::string> const& args);
bool HandleRandomCommand(std::vector<std::string> const& args);
+ bool HandleFindCommand(std::vector<std::string> const& args);
class RegexReplacement
{
--
1.7.4
0003-Updated-the-tests-for-the-new-signature-of-STRING-FI.patch [^] (4,281 bytes) 2011-02-15 11:52 [Show Content] [Hide Content]From 5d9525d94b1f5d59193cfa2930473a0fd41befa6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20H=C3=BCtz?= <tim@huetz.biz>
Date: Tue, 15 Feb 2011 17:38:19 +0100
Subject: [PATCH 3/4] Updated the tests for the new signature of STRING(FIND ...)
Updated the test cases for the new signature of the new
sub-command FIND of the STRING command.
---
Tests/CMakeTests/StringTest.cmake.in | 2 +-
Tests/CMakeTests/StringTestScript.cmake | 31 ++++++++++++++++++++++++-------
2 files changed, 25 insertions(+), 8 deletions(-)
diff --git a/Tests/CMakeTests/StringTest.cmake.in b/Tests/CMakeTests/StringTest.cmake.in
index 2a19ad0..6bb60f4 100644
--- a/Tests/CMakeTests/StringTest.cmake.in
+++ b/Tests/CMakeTests/StringTest.cmake.in
@@ -1,7 +1,7 @@
# Execute each test listed in StringTestScript.cmake:
#
set(scriptname "@CMAKE_CURRENT_SOURCE_DIR@/StringTestScript.cmake")
-set(number_of_tests_expected 66)
+set(number_of_tests_expected 69)
include("@CMAKE_CURRENT_SOURCE_DIR@/ExecuteScriptTests.cmake")
execute_all_script_tests(${scriptname} number_of_tests_executed)
diff --git a/Tests/CMakeTests/StringTestScript.cmake b/Tests/CMakeTests/StringTestScript.cmake
index 785b089..78a2344 100644
--- a/Tests/CMakeTests/StringTestScript.cmake
+++ b/Tests/CMakeTests/StringTestScript.cmake
@@ -214,17 +214,20 @@ elseif(testname STREQUAL string_find_reverse_with_no_parameter) # fail
string(FIND REVERSE)
elseif(testname STREQUAL string_find_reverse_with_one_parameter) # fail
- string(FIND REVERSE "CMake is great.")
+ string(FIND "CMake is great." REVERSE)
elseif(testname STREQUAL string_find_reverse_with_two_parameters) # fail
- string(FIND REVERSE "CMake is great." "a")
+ string(FIND "CMake is great." "a" REVERSE)
elseif(testname STREQUAL string_find_reverse_with_three_parameters) # pass
- string(FIND REVERSE "CMake is great." "a" v)
+ string(FIND "CMake is great." "a" v REVERSE)
message(STATUS "v='${v}'")
-elseif(testname STREQUAL string_find_reverse_with_four_parameters) # fail
- string(FIND REVERSE "CMake is great." "a" v v2)
+elseif(testname STREQUAL string_find_reverse_with_four_parameters_part1) # fail
+ string(FIND "CMake is great." "a" v v2 REVERSE)
+
+elseif(testname STREQUAL string_find_reverse_with_four_parameters_part2) # fail
+ string(FIND "CMake is great." "a" v REVERSE v2)
elseif(testname STREQUAL string_find_with_no_possible_result) # pass
string(FIND "CMake is a great application." "z" v)
@@ -234,7 +237,7 @@ elseif(testname STREQUAL string_find_with_no_possible_result) # pass
endif(NOT(-1 EQUAL ${v}))
elseif(testname STREQUAL string_find_reverse_with_no_possible_result) # pass
- string(FIND REVERSE "CMake is a great application." "z" v)
+ string(FIND "CMake is a great application." "z" v REVERSE)
message(STATUS "v='${v}'")
if(NOT(-1 EQUAL ${v}))
message(SEND_ERROR "FIND REVERSE sub-command should return -1 but returned ${v}.")
@@ -248,11 +251,25 @@ elseif(testname STREQUAL string_find_with_required_result) # pass
endif(NOT(11 EQUAL ${v}))
elseif(testname STREQUAL string_find_reverse_with_required_result) # pass
- string(FIND REVERSE "CMake is a great application." "e" v)
+ string(FIND "CMake is a great application." "e" v REVERSE)
message(STATUS "v='${v}'")
if(NOT(13 EQUAL ${v}))
message(SEND_ERROR "FIND REVERSE sub-command should return 13 but returned ${v}.")
endif(NOT(13 EQUAL ${v}))
+
+elseif(testname STREQUAL string_find_word_reverse_with_required_result) # pass
+ string(FIND "The command should find REVERSE in this string. Or maybe this REVERSE?!" "REVERSE" v)
+ message(STATUS "v='${v}'")
+ if(NOT(24 EQUAL ${v}))
+ message(SEND_ERROR "FIND sub-command should return 24 but returned ${v}.")
+ endif(NOT(24 EQUAL ${v}))
+
+elseif(testname STREQUAL string_find_reverse_word_reverse_with_required_result) # pass
+ string(FIND "The command should find REVERSE in this string. Or maybe this REVERSE?!" "REVERSE" v REVERSE)
+ message(STATUS "v='${v}'")
+ if(NOT(62 EQUAL ${v}))
+ message(SEND_ERROR "FIND sub-command should return 62 but returned ${v}.")
+ endif(NOT(62 EQUAL ${v}))
else() # fail
message(FATAL_ERROR "testname='${testname}' - error: no such test in '${CMAKE_CURRENT_LIST_FILE}'")
--
1.7.3.5
0004-Changed-the-signature-of-the-FIND-sub-command.patch [^] (2,921 bytes) 2011-02-15 11:52 [Show Content] [Hide Content]From bdee5f73bfec1ad266a37550cfa3b23454544f04 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20H=C3=BCtz?= <tim@huetz.biz>
Date: Tue, 15 Feb 2011 17:44:43 +0100
Subject: [PATCH 4/4] Changed the signature of the FIND sub-command
Changed the signature of the FIND sub-command to be able to extend
the command in the future.
---
Source/cmStringCommand.cxx | 26 +++++++++++++-------------
Source/cmStringCommand.h | 2 +-
2 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx
index 3ae23d0..edc1ae3 100644
--- a/Source/cmStringCommand.cxx
+++ b/Source/cmStringCommand.cxx
@@ -515,29 +515,29 @@ bool cmStringCommand::HandleFindCommand(std::vector<std::string> const&
// check if the reverse flag was set or not
bool reverseMode = false;
- if(args[1] == "REVERSE")
+ if(args.size() == 5 && args[4] == "REVERSE")
{
reverseMode = true;
}
- //
- if(reverseMode && args.size() != 5)
+ // if we have 5 arguments, but the last one is not REVERSE, indicate an error
+ if(args.size() == 5 && args[4] != "REVERSE")
{
- this->SetError("sub-command FIND REVERSE requires 3 parameters.");
+ this->SetError("sub-command FIND: unknown last parameter");
return false;
}
-
- //
- if(!reverseMode && args.size() > 4)
+
+ // get the coorect parameters (depending on the REVERSE flag)
+ const std::string& sstring = args[1];
+ const std::string& schar = args[2];
+ const std::string& outvar = args[3];
+
+ // ensure that the user cannot accedently select REVERSE as a variable
+ if(0==outvar.compare("REVERSE"))
{
- this->SetError("sub-command FIND requires just 3 further parameters.");
+ this->SetError("sub-command FIND does not allow to select REVERSE as an output variable. Maybe you missed the actual output variable?");
return false;
}
-
- // get the coorect parameters (depending on the REVERSE flag)
- const std::string& sstring = args[reverseMode?2:1];
- const std::string& schar = args[reverseMode?3:2];
- const std::string& outvar = args[reverseMode?4:3];
// try to find the character and return its position
size_t pos;
diff --git a/Source/cmStringCommand.h b/Source/cmStringCommand.h
index 96a0f35..9586449 100644
--- a/Source/cmStringCommand.h
+++ b/Source/cmStringCommand.h
@@ -90,7 +90,7 @@ public:
" string(STRIP <string> <output variable>)\n"
" string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n"
" [RANDOM_SEED <seed>] <output variable>)\n"
- " string(FIND [REVERSE] <string> <substring> <output variable>)\n"
+ " string(FIND <string> <substring> <output variable> [REVERSE])\n"
"REGEX MATCH will match the regular expression once and store the "
"match in the output variable.\n"
"REGEX MATCHALL will match the regular expression as many times as "
--
1.7.3.5
|