diff options
author | 2017-01-09 12:06:45 +0000 | |
---|---|---|
committer | 2017-01-09 13:22:07 +0000 | |
commit | f9f41c739dfc13f028259dbd24628eeec78bb043 (patch) | |
tree | 43424b41fe48b8f28bc6c3fb1322b492375eeb54 /src/main/cpp | |
parent | 2e08b9a8001f87903a04183387d2a67605592138 (diff) |
blaze_util_windows: use ostringstream to build cmd
Use std::ostringstream instead of string concats
when building a command line, for more efficiency.
See https://github.com/bazelbuild/bazel/issues/2107
--
PiperOrigin-RevId: 143947880
MOS_MIGRATED_REVID=143947880
Diffstat (limited to 'src/main/cpp')
-rw-r--r-- | src/main/cpp/blaze_util_windows.cc | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc index 8e02590816..12aa3e3721 100644 --- a/src/main/cpp/blaze_util_windows.cc +++ b/src/main/cpp/blaze_util_windows.cc @@ -31,6 +31,7 @@ #include <cstdio> #include <cstdlib> +#include <sstream> #include <thread> // NOLINT (to slience Google-internal linter) #include <type_traits> // static_assert @@ -371,26 +372,23 @@ struct CmdLine { }; static void CreateCommandLine(CmdLine* result, const string& exe, const vector<string>& args_vector) { - string cmdline; + std::ostringstream cmdline; bool first = true; for (const auto& s : args_vector) { if (first) { first = false; // Skip first argument, instead use quoted executable name with ".exe" // suffix. - cmdline.append("\""); - cmdline.append(exe); - cmdline.append(".exe"); - cmdline.append("\""); + cmdline << '\"' << exe << '\"'; continue; } else { - cmdline.append(" "); + cmdline << ' '; } bool has_space = s.find(" ") != string::npos; if (has_space) { - cmdline.append("\""); + cmdline << '\"'; } // TODO(bazel-team): get rid of the code to append character by character, @@ -404,38 +402,39 @@ static void CreateCommandLine(CmdLine* result, const string& exe, switch (ch) { case '"': // Escape double quotes - cmdline.append("\\\""); + cmdline << "\\\""; break; case '\\': if (it == s.end()) { // Backslashes at the end of the string are quoted if we add quotes - cmdline.append(has_space ? "\\\\" : "\\"); + cmdline << (has_space ? "\\\\" : "\\"); } else { // Backslashes everywhere else are quoted if they are followed by a // quote or a backslash - cmdline.append(*it == '"' || *it == '\\' ? "\\\\" : "\\"); + cmdline << (*it == '"' || *it == '\\' ? "\\\\" : "\\"); } break; default: - cmdline.append(1, ch); + cmdline << ch; } } if (has_space) { - cmdline.append("\""); + cmdline << '\"'; } } - if (cmdline.size() >= MAX_CMDLINE_LENGTH) { - pdie(blaze_exit_code::INTERNAL_ERROR, - "Command line too long: %s", cmdline.c_str()); + string cmdline_str = cmdline.str(); + if (cmdline_str.size() >= MAX_CMDLINE_LENGTH) { + pdie(blaze_exit_code::INTERNAL_ERROR, "Command line too long: %s", + cmdline_str.c_str()); } // Copy command line into a mutable buffer. // CreateProcess is allowed to mutate its command line argument. - strncpy(result->cmdline, cmdline.c_str(), MAX_CMDLINE_LENGTH - 1); + strncpy(result->cmdline, cmdline_str.c_str(), MAX_CMDLINE_LENGTH - 1); result->cmdline[MAX_CMDLINE_LENGTH - 1] = 0; } |