Author: Raphael Isemann Date: 2020-10-26T15:29:13+01:00 New Revision: d0ee1d8efe804b3184b1073cd367a307867f1372
URL: https://github.com/llvm/llvm-project/commit/d0ee1d8efe804b3184b1073cd367a307867f1372 DIFF: https://github.com/llvm/llvm-project/commit/d0ee1d8efe804b3184b1073cd367a307867f1372.diff LOG: [lldb][NFC] Make GetShellSafeArgument simpler and faster Escaping by inserting characters in the middle of a std::string isn't cheap. It's much more verbose than just prepending a backslash in a loop. Added: Modified: lldb/source/Utility/Args.cpp Removed: ################################################################################ diff --git a/lldb/source/Utility/Args.cpp b/lldb/source/Utility/Args.cpp index 152e73db08ea..ed61e1c16303 100644 --- a/lldb/source/Utility/Args.cpp +++ b/lldb/source/Utility/Args.cpp @@ -383,7 +383,7 @@ std::string Args::GetShellSafeArgument(const FileSpec &shell, llvm::StringRef unsafe_arg) { struct ShellDescriptor { ConstString m_basename; - const char *m_escapables; + llvm::StringRef m_escapables; }; static ShellDescriptor g_Shells[] = {{ConstString("bash"), " '\"<>()&"}, @@ -391,7 +391,7 @@ std::string Args::GetShellSafeArgument(const FileSpec &shell, {ConstString("sh"), " '\"<>()&"}}; // safe minimal set - const char *escapables = " '\""; + llvm::StringRef escapables = " '\""; if (auto basename = shell.GetFilename()) { for (const auto &Shell : g_Shells) { @@ -402,16 +402,13 @@ std::string Args::GetShellSafeArgument(const FileSpec &shell, } } - std::string safe_arg(unsafe_arg); - size_t prev_pos = 0; - while (prev_pos < safe_arg.size()) { - // Escape spaces and quotes - size_t pos = safe_arg.find_first_of(escapables, prev_pos); - if (pos != std::string::npos) { - safe_arg.insert(pos, 1, '\\'); - prev_pos = pos + 2; - } else - break; + std::string safe_arg; + safe_arg.reserve(unsafe_arg.size()); + // Add a \ before every character that needs to be escaped. + for (char c : unsafe_arg) { + if (escapables.contains(c)) + safe_arg.push_back('\\'); + safe_arg.push_back(c); } return safe_arg; } _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits