util: Added ::quoteIfNeeded

This commit is contained in:
Paul Beckingham 2016-03-20 14:15:45 -04:00
parent f50b3b4fc2
commit ebf9234006
2 changed files with 24 additions and 0 deletions

View file

@ -41,5 +41,6 @@ int dispatchCommand (const std::vector <std::string>&, Database&, Rules&, Extens
// utiŀ.cpp
std::string osName ();
std::string escape (const std::string&, int);
std::string quoteIfNeeded (const std::string&);
#endif

View file

@ -25,6 +25,7 @@
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <timew.h>
#include <string>
////////////////////////////////////////////////////////////////////////////////
@ -79,3 +80,25 @@ std::string escape (const std::string& input, int c)
}
////////////////////////////////////////////////////////////////////////////////
std::string quoteIfNeeded (const std::string& input)
{
auto quote = input.find ('"');
auto space = input.find (' ');
if (quote == std::string::npos &&
space == std::string::npos)
return input;
std::string output;
if (quote != std::string::npos)
output = escape (input, '"');
else
output = input;
if (space != std::string::npos)
output = std::string ("\"") + output + "\"";
return output;
}
////////////////////////////////////////////////////////////////////////////////