ENH: Add STRING STRIP command
This commit is contained in:
parent
6697979aaf
commit
1d4613a63b
|
@ -68,6 +68,10 @@ bool cmStringCommand::InitialPass(std::vector<std::string> const& args)
|
||||||
{
|
{
|
||||||
return this->HandleSubstringCommand(args);
|
return this->HandleSubstringCommand(args);
|
||||||
}
|
}
|
||||||
|
else if(subCommand == "STRIP")
|
||||||
|
{
|
||||||
|
return this->HandleStripCommand(args);
|
||||||
|
}
|
||||||
else if(subCommand == "RANDOM")
|
else if(subCommand == "RANDOM")
|
||||||
{
|
{
|
||||||
return this->HandleRandomCommand(args);
|
return this->HandleRandomCommand(args);
|
||||||
|
@ -613,6 +617,43 @@ bool cmStringCommand
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
bool cmStringCommand::HandleStripCommand(
|
||||||
|
std::vector<std::string> const& args)
|
||||||
|
{
|
||||||
|
if(args.size() != 3)
|
||||||
|
{
|
||||||
|
this->SetError("sub-command LENGTH requires two arguments.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& stringValue = args[1];
|
||||||
|
const std::string& variableName = args[2];
|
||||||
|
size_t inStringLength = stringValue.size();
|
||||||
|
size_t startPos = inStringLength + 1;
|
||||||
|
size_t endPos = 0;
|
||||||
|
const char* ptr = stringValue.c_str();
|
||||||
|
size_t cc;
|
||||||
|
for ( cc = 0; cc < inStringLength; ++ cc )
|
||||||
|
{
|
||||||
|
if ( !isspace(*ptr) )
|
||||||
|
{
|
||||||
|
if ( startPos > inStringLength )
|
||||||
|
{
|
||||||
|
startPos = cc;
|
||||||
|
}
|
||||||
|
endPos = cc;
|
||||||
|
}
|
||||||
|
++ ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t outLength = endPos - startPos + 1;
|
||||||
|
|
||||||
|
this->Makefile->AddDefinition(variableName.c_str(),
|
||||||
|
stringValue.substr(startPos, outLength).c_str());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
bool cmStringCommand
|
bool cmStringCommand
|
||||||
::HandleRandomCommand(std::vector<std::string> const& args)
|
::HandleRandomCommand(std::vector<std::string> const& args)
|
||||||
|
|
|
@ -85,6 +85,7 @@ public:
|
||||||
" STRING(TOLOWER <string1> <output variable>)\n"
|
" STRING(TOLOWER <string1> <output variable>)\n"
|
||||||
" STRING(LENGTH <string> <output variable>)\n"
|
" STRING(LENGTH <string> <output variable>)\n"
|
||||||
" STRING(SUBSTRING <string> <begin> <length> <output variable>)\n"
|
" STRING(SUBSTRING <string> <begin> <length> <output variable>)\n"
|
||||||
|
" STRING(STRIP <string> <output variable>)\n"
|
||||||
" STRING(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n"
|
" STRING(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n"
|
||||||
" <output variable>)\n"
|
" <output variable>)\n"
|
||||||
"REGEX MATCH will match the regular expression once and store the "
|
"REGEX MATCH will match the regular expression once and store the "
|
||||||
|
@ -111,6 +112,8 @@ public:
|
||||||
"TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
|
"TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
|
||||||
"LENGTH will return a given string's length.\n"
|
"LENGTH will return a given string's length.\n"
|
||||||
"SUBSTRING will return a substring of a given string.\n"
|
"SUBSTRING will return a substring of a given string.\n"
|
||||||
|
"STRIP will return a substring of a given string with leading "
|
||||||
|
"and trailing spaces removed.\n"
|
||||||
"RANDOM will return a random string of given length consisting of "
|
"RANDOM will return a random string of given length consisting of "
|
||||||
"characters from the given alphabet. Default length is 5 "
|
"characters from the given alphabet. Default length is 5 "
|
||||||
"characters and default alphabet is all numbers and upper and "
|
"characters and default alphabet is all numbers and upper and "
|
||||||
|
@ -131,6 +134,7 @@ protected:
|
||||||
bool HandleReplaceCommand(std::vector<std::string> const& args);
|
bool HandleReplaceCommand(std::vector<std::string> const& args);
|
||||||
bool HandleLengthCommand(std::vector<std::string> const& args);
|
bool HandleLengthCommand(std::vector<std::string> const& args);
|
||||||
bool HandleSubstringCommand(std::vector<std::string> const& args);
|
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 HandleRandomCommand(std::vector<std::string> const& args);
|
||||||
|
|
||||||
class RegexReplacement
|
class RegexReplacement
|
||||||
|
|
|
@ -34,6 +34,20 @@ STRING(TOUPPER "CMake" tuvar)
|
||||||
STRING(TOLOWER "CMake" tlvar)
|
STRING(TOLOWER "CMake" tlvar)
|
||||||
STRING(REPLACE "Autoconf" "CMake" repvar "People should use Autoconf")
|
STRING(REPLACE "Autoconf" "CMake" repvar "People should use Autoconf")
|
||||||
|
|
||||||
|
STRING(STRIP "
|
||||||
|
ST1
|
||||||
|
" ST1)
|
||||||
|
STRING(STRIP "ST2 " ST2)
|
||||||
|
STRING(STRIP " ST3" ST3)
|
||||||
|
|
||||||
|
FOREACH(var ST1 ST2 ST3)
|
||||||
|
IF("${var}" STREQUAL "${${var}}")
|
||||||
|
MESSAGE("[${var}] == [${${var}}]")
|
||||||
|
ELSE("${var}" STREQUAL "${${var}}")
|
||||||
|
MESSAGE(SEND_ERROR "Problem with the STRIP command for ${var}: [${${var}}]")
|
||||||
|
ENDIF("${var}" STREQUAL "${${var}}")
|
||||||
|
ENDFOREACH(var)
|
||||||
|
|
||||||
STRING(SUBSTRING "People should use Autoconf" 7 10 substringres)
|
STRING(SUBSTRING "People should use Autoconf" 7 10 substringres)
|
||||||
SET(substringres "Everybody ${substringres} CMake")
|
SET(substringres "Everybody ${substringres} CMake")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue