diff options
Diffstat (limited to 'Source/cmcldeps.cxx')
-rw-r--r-- | Source/cmcldeps.cxx | 58 |
1 files changed, 26 insertions, 32 deletions
diff --git a/Source/cmcldeps.cxx b/Source/cmcldeps.cxx index caf6453..5c27ac1 100644 --- a/Source/cmcldeps.cxx +++ b/Source/cmcldeps.cxx @@ -25,6 +25,7 @@ #include "cmsys/Encoding.hxx" +#include "cmStringAlgorithms.h" #include "cmSystemTools.h" // We don't want any wildcard expansion. @@ -63,7 +64,7 @@ static void usage(const char* msg) msg); } -static std::string trimLeadingSpace(const std::string& cmdline) +static cm::string_view trimLeadingSpace(cm::string_view cmdline) { int i = 0; for (; cmdline[i] == ' '; ++i) @@ -81,34 +82,30 @@ static void replaceAll(std::string& str, const std::string& search, } } -bool startsWith(const std::string& str, const std::string& what) -{ - return str.compare(0, what.size(), what) == 0; -} - // Strips one argument from the cmdline and returns it. "surrounding quotes" // are removed from the argument if there were any. static std::string getArg(std::string& cmdline) { - std::string ret; bool in_quoted = false; unsigned int i = 0; - cmdline = trimLeadingSpace(cmdline); + cm::string_view cmdview = trimLeadingSpace(cmdline); + size_t spaceCnt = cmdline.size() - cmdview.size(); for (;; ++i) { - if (i >= cmdline.size()) + if (i >= cmdview.size()) usage("Couldn't parse arguments."); - if (!in_quoted && cmdline[i] == ' ') + if (!in_quoted && cmdview[i] == ' ') break; // "a b" "x y" - if (cmdline[i] == '"') + if (cmdview[i] == '"') in_quoted = !in_quoted; } - ret = cmdline.substr(0, i); - if (ret[0] == '"' && ret[i - 1] == '"') - ret = ret.substr(1, ret.size() - 2); - cmdline = cmdline.substr(i); + cmdview = cmdview.substr(0, i); + if (cmdview[0] == '"' && cmdview[i - 1] == '"') + cmdview = cmdview.substr(1, i - 2); + std::string ret(cmdview); + cmdline.erase(0, spaceCnt + i); return ret; } @@ -127,7 +124,7 @@ static void parseCommandLine(LPWSTR wincmdline, std::string& lang, prefix = getArg(cmdline); clpath = getArg(cmdline); binpath = getArg(cmdline); - rest = trimLeadingSpace(cmdline); + rest = std::string(trimLeadingSpace(cmdline)); } // Not all backslashes need to be escaped in a depfile, but it's easier that @@ -169,8 +166,8 @@ static void outputDepFile(const std::string& dfile, const std::string& objfile, // build.ninja file. Therefore we need to canonicalize the path to use // backward slashes and relativize the path to the build directory. replaceAll(tmp, "/", "\\"); - if (startsWith(tmp, cwd)) - tmp = tmp.substr(cwd.size()); + if (cmHasPrefix(tmp, cwd)) + tmp.erase(0, cwd.size()); escapePath(tmp); fprintf(out, "%s \\\n", tmp.c_str()); } @@ -194,7 +191,7 @@ std::string replace(const std::string& str, const std::string& what, return replaced.replace(pos, what.size(), replacement); } -static int process(const std::string& srcfilename, const std::string& dfile, +static int process(cm::string_view srcfilename, const std::string& dfile, const std::string& objfile, const std::string& prefix, const std::string& cmd, const std::string& dir = "", bool quiet = false) @@ -221,13 +218,14 @@ static int process(const std::string& srcfilename, const std::string& dfile, std::vector<std::string> includes; bool isFirstLine = true; // cl prints always first the source filename while (std::getline(ss, line)) { - if (startsWith(line, prefix)) { - std::string inc = trimLeadingSpace(line.substr(prefix.size()).c_str()); + cm::string_view inc(line); + if (cmHasPrefix(inc, prefix)) { + inc = trimLeadingSpace(inc.substr(prefix.size())); if (inc.back() == '\r') // blech, stupid \r\n inc = inc.substr(0, inc.size() - 1); - includes.push_back(inc); + includes.emplace_back(std::string(inc)); } else { - if (!isFirstLine || !startsWith(line, srcfilename)) { + if (!isFirstLine || !cmHasPrefix(inc, srcfilename)) { if (!quiet || exit_code != 0) { fprintf(stdout, "%s\n", line.c_str()); } @@ -258,14 +256,10 @@ int main() cl, binpath, rest); // needed to suppress filename output of msvc tools - std::string srcfilename; - { - std::string::size_type pos = srcfile.rfind('\\'); - if (pos == std::string::npos) { - srcfilename = srcfile; - } else { - srcfilename = srcfile.substr(pos + 1); - } + cm::string_view srcfilename(srcfile); + std::string::size_type pos = srcfile.rfind('\\'); + if (pos != std::string::npos) { + srcfilename = srcfilename.substr(pos + 1); } std::string nol = " /nologo "; @@ -286,7 +280,7 @@ int main() // call cl in object dir so the .i is generated there std::string objdir; { - std::string::size_type pos = objfile.rfind("\\"); + pos = objfile.rfind("\\"); if (pos != std::string::npos) { objdir = objfile.substr(0, pos); } |