From b621e652317b8a282130a77cc0ecc46740fa6f86 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 4 Oct 2024 10:38:31 +0200 Subject: [PATCH] Replace `` with a helper function --- MODULE.bazel.lock | 1 + src/main/tools/build-runfiles-windows.cc | 47 ++++++++++++++++++------ src/main/tools/build-runfiles.cc | 42 +++++++++++++++++---- tools/cpp/runfiles/runfiles_src.cc | 47 ++++++++++++++++++------ 4 files changed, 106 insertions(+), 31 deletions(-) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 04f24cf4bb8ced..08eea9335390b3 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -138,6 +138,7 @@ "https://bcr.bazel.build/modules/rules_java/6.0.0/MODULE.bazel": "8a43b7df601a7ec1af61d79345c17b31ea1fedc6711fd4abfd013ea612978e39", "https://bcr.bazel.build/modules/rules_java/6.4.0/MODULE.bazel": "e986a9fe25aeaa84ac17ca093ef13a4637f6107375f64667a15999f77db6c8f6", "https://bcr.bazel.build/modules/rules_java/6.5.2/MODULE.bazel": "1d440d262d0e08453fa0c4d8f699ba81609ed0e9a9a0f02cd10b3e7942e61e31", + "https://bcr.bazel.build/modules/rules_java/7.11.1/MODULE.bazel": "b4782e019dd0b0151bd49fd8929136fd4441f527eb208fbd991b77e480b7236e", "https://bcr.bazel.build/modules/rules_java/7.12.1/MODULE.bazel": "0a2ebb53b48a6eb092aef24b36db23294d4d3ebf96bff02b0ccc962bdc70717d", "https://bcr.bazel.build/modules/rules_java/7.12.1/source.json": "2ab5ceabe9d87a773fa44e4cce42c950e34ff6d2f5164e7413088542fa4f1f3e", "https://bcr.bazel.build/modules/rules_java/7.2.0/MODULE.bazel": "06c0334c9be61e6cef2c8c84a7800cef502063269a5af25ceb100b192453d4ab", diff --git a/src/main/tools/build-runfiles-windows.cc b/src/main/tools/build-runfiles-windows.cc index 1654b99967e77b..360b8b3beba826 100644 --- a/src/main/tools/build-runfiles-windows.cc +++ b/src/main/tools/build-runfiles-windows.cc @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -42,10 +41,6 @@ using std::wstring; namespace { -const std::regex kEscapedBackslash(R"(\\b)"); -const std::regex kEscapedNewline(R"(\\n)"); -const std::regex kEscapedSpace(R"(\\s)"); - const wchar_t* manifest_filename; const wchar_t* runfiles_base_dir; @@ -129,6 +124,39 @@ bool ReadSymlink(const wstring& abs_path, wstring* target, wstring* error) { return false; } +// Replaces \s, \n, and \b with their respective characters. +std::string Unescape(const std::string &path) { + std::string result; + result.reserve(path.size()); + for (size_t i = 0; i < path.size(); ++i) { + if (path[i] == '\\' && i + 1 < path.size()) { + switch (path[i + 1]) { + case 's': { + result.push_back(' '); + break; + } + case 'n': { + result.push_back('\n'); + break; + } + case 'b': { + result.push_back('\\'); + break; + } + default: { + result.push_back(path[i]); + result.push_back(path[i + 1]); + break; + } + } + ++i; + } else { + result.push_back(path[i]); + } + } + return result; +} + } // namespace class RunfilesCreator { @@ -172,14 +200,9 @@ class RunfilesCreator { if (idx == string::npos) { die(L"Missing separator in manifest line: %hs", line.c_str()); } - std::string link_path = line.substr(1, idx - 1); - link_path = std::regex_replace(link_path, kEscapedSpace, " "); - link_path = std::regex_replace(link_path, kEscapedNewline, "\n"); - link_path = std::regex_replace(link_path, kEscapedBackslash, "\\"); + std::string link_path = Unescape(line.substr(1, idx - 1)); link = blaze_util::CstringToWstring(link_path); - std::string target_path = line.substr(idx + 1); - target_path = std::regex_replace(target_path, kEscapedNewline, "\n"); - target_path = std::regex_replace(target_path, kEscapedBackslash, " "); + std::string target_path = Unescape(line.substr(idx + 1)); target = blaze_util::CstringToWstring(target_path); } else { string::size_type idx = line.find(' '); diff --git a/src/main/tools/build-runfiles.cc b/src/main/tools/build-runfiles.cc index 345ad6b1890ae3..51f8bc148cec9b 100644 --- a/src/main/tools/build-runfiles.cc +++ b/src/main/tools/build-runfiles.cc @@ -108,6 +108,39 @@ struct FileInfo { typedef std::map FileInfoMap; +// Replaces \s, \n, and \b with their respective characters. +std::string Unescape(const std::string &path) { + std::string result; + result.reserve(path.size()); + for (size_t i = 0; i < path.size(); ++i) { + if (path[i] == '\\' && i + 1 < path.size()) { + switch (path[i + 1]) { + case 's': { + result.push_back(' '); + break; + } + case 'n': { + result.push_back('\n'); + break; + } + case 'b': { + result.push_back('\\'); + break; + } + default: { + result.push_back(path[i]); + result.push_back(path[i + 1]); + break; + } + } + ++i; + } else { + result.push_back(path[i]); + } + } + return result; +} + class RunfilesCreator { public: explicit RunfilesCreator(const std::string &output_base) @@ -170,13 +203,8 @@ class RunfilesCreator { if (!s) { DIE("missing field delimiter at line %d: '%s'\n", lineno, buf); } - link = std::string(buf + 1, s); - link = std::regex_replace(link, kEscapedSpace, " "); - link = std::regex_replace(link, kEscapedNewline, "\n"); - link = std::regex_replace(link, kEscapedBackslash, "\\"); - target = s + 1; - target = std::regex_replace(target, kEscapedNewline, "\n"); - target = std::regex_replace(target, kEscapedBackslash, "\\"); + link = Unescape(std::string(buf + 1, s)); + target = Unescape(s + 1); } else { // The line is of the form "foo /target/path", with only a single space // in the link path. diff --git a/tools/cpp/runfiles/runfiles_src.cc b/tools/cpp/runfiles/runfiles_src.cc index 8795fa20193f72..e585c589d47bf2 100644 --- a/tools/cpp/runfiles/runfiles_src.cc +++ b/tools/cpp/runfiles/runfiles_src.cc @@ -29,7 +29,6 @@ #include #include #include -#include #include #include @@ -50,10 +49,6 @@ using std::vector; namespace { -const std::regex kEscapedBackslash("\\\\b"); -const std::regex kEscapedNewline("\\\\n"); -const std::regex kEscapedSpace("\\\\s"); - bool starts_with(const string& s, const char* prefix) { if (!prefix || !*prefix) { return true; @@ -184,6 +179,39 @@ string GetEnv(const string& key) { #endif } +// Replaces \s, \n, and \b with their respective characters. +string Unescape(const string &path) { + string result; + result.reserve(path.size()); + for (size_t i = 0; i < path.size(); ++i) { + if (path[i] == '\\' && i + 1 < path.size()) { + switch (path[i + 1]) { + case 's': { + result.push_back(' '); + break; + } + case 'n': { + result.push_back('\n'); + break; + } + case 'b': { + result.push_back('\\'); + break; + } + default: { + result.push_back(path[i]); + result.push_back(path[i + 1]); + break; + } + } + ++i; + } else { + result.push_back(path[i]); + } + } + return result; +} + string Runfiles::Rlocation(const string& path) const { return Rlocation(path, source_repository_); } @@ -274,13 +302,8 @@ bool ParseManifest(const string& path, map* result, } return false; } - source = line.substr(1, idx - 1); - source = std::regex_replace(source, kEscapedSpace, " "); - source = std::regex_replace(source, kEscapedNewline, "\n"); - source = std::regex_replace(source, kEscapedBackslash, "\\"); - target = line.substr(idx + 1); - target = std::regex_replace(target, kEscapedNewline, "\n"); - target = std::regex_replace(target, kEscapedBackslash, "\\"); + source = Unescape(line.substr(1, idx - 1)); + target = Unescape(line.substr(idx + 1)); } else { string::size_type idx = line.find(' '); if (idx == string::npos) {