aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/launcher
diff options
context:
space:
mode:
authorGravatar Loo Rong Jie <loorongjie@gmail.com>2018-02-25 17:57:09 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-02-25 17:58:35 -0800
commitc91bd9270d1a199d8daeb7fbc41762e9547b0d7c (patch)
treee0c6e1833bd1a674fa304f9cd176fae3511b7266 /src/tools/launcher
parent3d7f0bb5a13fc446fb7923ec0637b3b99920600b (diff)
Optimize GetEscapedArgument
If `argument` does not contain `' '` and `'"'` and `escape_backslash` is false (i.e: no escaping needed), `escaped_arg` will allocate memory exactly once only. Closes #4491. PiperOrigin-RevId: 186962716
Diffstat (limited to 'src/tools/launcher')
-rw-r--r--src/tools/launcher/util/launcher_util.cc20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/tools/launcher/util/launcher_util.cc b/src/tools/launcher/util/launcher_util.cc
index 453b9f0f6a..dd01a39676 100644
--- a/src/tools/launcher/util/launcher_util.cc
+++ b/src/tools/launcher/util/launcher_util.cc
@@ -110,36 +110,36 @@ string GetBinaryPathWithExtension(const string& binary) {
}
string GetEscapedArgument(const string& argument, bool escape_backslash) {
- ostringstream escaped_arg;
+ string escaped_arg;
+ // escaped_arg will be at least this long
+ escaped_arg.reserve(argument.size());
bool has_space = argument.find_first_of(' ') != string::npos;
if (has_space) {
- escaped_arg << '\"';
+ escaped_arg += '\"';
}
- string::const_iterator it = argument.begin();
- while (it != argument.end()) {
- char ch = *it++;
+ for (const char ch : argument) {
switch (ch) {
case '"':
// Escape double quotes
- escaped_arg << "\\\"";
+ escaped_arg += "\\\"";
break;
case '\\':
// Escape back slashes if escape_backslash is true
- escaped_arg << (escape_backslash ? "\\\\" : "\\");
+ escaped_arg += (escape_backslash ? "\\\\" : "\\");
break;
default:
- escaped_arg << ch;
+ escaped_arg += ch;
}
}
if (has_space) {
- escaped_arg << '\"';
+ escaped_arg += '\"';
}
- return escaped_arg.str();
+ return escaped_arg;
}
// An environment variable has a maximum size limit of 32,767 characters