11#include " inspector_profiler.h"
2+ #include " uv.h"
23#include " base_object-inl.h"
34#include " debug_utils-inl.h"
45#include " diagnosticfilename-inl.h"
910#include " node_internals.h"
1011#include " util-inl.h"
1112#include " v8-inspector.h"
12- #include < unistd.h>
13- #include < regex>
1413
1514#include < cinttypes>
1615#include < limits>
@@ -467,11 +466,22 @@ static void EndStartedProfilers(Environment* env) {
467466 }
468467}
469468
470- std::string ReplacePlaceholders (const std::string& pattern) {
469+ static std::string ReplacePlaceholders (const std::string& pattern) {
471470 std::string result = pattern;
472- std::string pid_str = std::to_string (getpid ());
473- result = std::regex_replace (result, std::regex (" \\ $\\ {pid\\ }" ), pid_str);
474- // TODO: Add more placeholders as needed.
471+
472+ static const std::unordered_map<std::string, std::function<std::string ()>> kPlaceholderMap = {
473+ { " ${pid}" , []() { return std::to_string (uv_os_getpid ()); } },
474+ // TODO(haramj): Add more placeholders as needed.
475+ };
476+
477+ for (const auto & [placeholder, getter] : kPlaceholderMap ) {
478+ size_t pos = 0 ;
479+ while ((pos = result.find (placeholder, pos)) != std::string::npos) {
480+ result.replace (pos, placeholder.length (), getter ());
481+ pos += getter ().length ();
482+ }
483+ }
484+
475485 return result;
476486}
477487
@@ -496,7 +506,8 @@ void StartProfilers(Environment* env) {
496506 DiagnosticFilename filename (env, " CPU" , " cpuprofile" );
497507 env->set_cpu_prof_name (*filename);
498508 } else {
499- std::string resolved_name = ReplacePlaceholders (env->options ()->cpu_prof_name );
509+ std::string resolved_name =
510+ ReplacePlaceholders (env->options ()->cpu_prof_name );
500511 env->set_cpu_prof_name (resolved_name);
501512 }
502513 CHECK_NULL (env->cpu_profiler_connection ());
0 commit comments