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 b621e65
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 31 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.

47 changes: 35 additions & 12 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 All @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(' ');
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
47 changes: 35 additions & 12 deletions tools/cpp/runfiles/runfiles_src.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <fstream>
#include <functional>
#include <map>
#include <regex>
#include <sstream>
#include <vector>

Expand All @@ -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;
Expand Down Expand Up @@ -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_);
}
Expand Down Expand Up @@ -274,13 +302,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 b621e65

Please sign in to comment.