From 2b7a541069f640989626b595e4c0417611125cbe Mon Sep 17 00:00:00 2001
From: Tim Huetz <tim@huetz.biz>
Date: Fri, 4 Feb 2011 10:17:45 +0100
Subject: [PATCH] Added a new STRING sub-command

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 |   42 ++++++++++++++++++++++++++++++++++++++++++
 Source/cmStringCommand.h   |    4 ++++
 2 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx
index 949fcf5..839b885 100644
--- a/Source/cmStringCommand.cxx
+++ b/Source/cmStringCommand.cxx
@@ -72,6 +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());
@@ -556,6 +561,43 @@ bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const&
 }
 
 //----------------------------------------------------------------------------
+bool cmStringCommand::HandleFindCommand(std::vector<std::string> const&
+                                           args)
+{
+  if(args.size() < 3)
+    {
+    this->SetError("sub-command FIND requires 3 parameters.");
+    return false;
+    }
+  
+  const std::string& sstring = args[1];  
+  const std::string& schar = args[2];
+  const std::string& outvar = args[3];
+  
+  // be sure that the user just specified a character and not a word
+  if(schar.length() != 1)
+    {
+    this->SetError("sub-command FIND can just search for character positions and not for whole strings.");
+    return false;
+    }
+    
+  // try to find the character and return its position
+  size_t pos = sstring.find( 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::HandleReplaceCommand(std::vector<std::string> const&
                                            args)
 {
diff --git a/Source/cmStringCommand.h b/Source/cmStringCommand.h
index 2a916b4..d68dcbd 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 <string> <character> <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,8 @@ 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 character is found "
+      "in the supplied string.\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 +155,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.3.3

