Skip to content

Commit

Permalink
Replace <regex> with a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Oct 4, 2024
1 parent 4cbb7a0 commit 8775fde
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 27 deletions.
1 change: 1 addition & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 35 additions & 8 deletions src/main/tools/build-runfiles-windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -129,6 +128,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 {
Expand Down Expand Up @@ -172,14 +204,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(' ');
Expand Down
42 changes: 35 additions & 7 deletions src/main/tools/build-runfiles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,39 @@ struct FileInfo {

typedef std::map<std::string, FileInfo> 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)
Expand Down Expand Up @@ -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.
Expand Down
48 changes: 36 additions & 12 deletions tools/cpp/runfiles/runfiles_src.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
#include <fstream>
#include <functional>
#include <map>
#include <regex>
#include <sstream>
#include <vector>
#include <iostream>

#ifdef _WIN32
#include <memory>
Expand All @@ -50,10 +50,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;
Expand Down Expand Up @@ -184,6 +180,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_);
}
Expand Down Expand Up @@ -274,13 +303,8 @@ bool ParseManifest(const string& path, map<string, string>* 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) {
Expand Down

0 comments on commit 8775fde

Please sign in to comment.