Skip to content

Commit 818e9af

Browse files
committed
Replace <regex> with a helper function
1 parent 4cbb7a0 commit 818e9af

File tree

4 files changed

+106
-27
lines changed

4 files changed

+106
-27
lines changed

MODULE.bazel.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/tools/build-runfiles-windows.cc

+35-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include <fstream>
2222
#include <iostream>
23-
#include <regex>
2423
#include <sstream>
2524
#include <string>
2625
#include <unordered_map>
@@ -129,6 +128,39 @@ bool ReadSymlink(const wstring& abs_path, wstring* target, wstring* error) {
129128
return false;
130129
}
131130

131+
// Replaces \s, \n, and \b with their respective characters.
132+
std::string Unescape(const std::string &path) {
133+
std::string result;
134+
result.reserve(path.size());
135+
for (size_t i = 0; i < path.size(); ++i) {
136+
if (path[i] == '\\' && i + 1 < path.size()) {
137+
switch (path[i + 1]) {
138+
case 's': {
139+
result.push_back(' ');
140+
break;
141+
}
142+
case 'n': {
143+
result.push_back('\n');
144+
break;
145+
}
146+
case 'b': {
147+
result.push_back('\\');
148+
break;
149+
}
150+
default: {
151+
result.push_back(path[i]);
152+
result.push_back(path[i + 1]);
153+
break;
154+
}
155+
}
156+
++i;
157+
} else {
158+
result.push_back(path[i]);
159+
}
160+
}
161+
return result;
162+
}
163+
132164
} // namespace
133165

134166
class RunfilesCreator {
@@ -172,14 +204,9 @@ class RunfilesCreator {
172204
if (idx == string::npos) {
173205
die(L"Missing separator in manifest line: %hs", line.c_str());
174206
}
175-
std::string link_path = line.substr(1, idx - 1);
176-
link_path = std::regex_replace(link_path, kEscapedSpace, " ");
177-
link_path = std::regex_replace(link_path, kEscapedNewline, "\n");
178-
link_path = std::regex_replace(link_path, kEscapedBackslash, "\\");
207+
std::string link_path = Unescape(line.substr(1, idx - 1));
179208
link = blaze_util::CstringToWstring(link_path);
180-
std::string target_path = line.substr(idx + 1);
181-
target_path = std::regex_replace(target_path, kEscapedNewline, "\n");
182-
target_path = std::regex_replace(target_path, kEscapedBackslash, " ");
209+
std::string target_path = Unescape(line.substr(idx + 1));
183210
target = blaze_util::CstringToWstring(target_path);
184211
} else {
185212
string::size_type idx = line.find(' ');

src/main/tools/build-runfiles.cc

+35-7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,39 @@ struct FileInfo {
108108

109109
typedef std::map<std::string, FileInfo> FileInfoMap;
110110

111+
// Replaces \s, \n, and \b with their respective characters.
112+
std::string Unescape(const std::string &path) {
113+
std::string result;
114+
result.reserve(path.size());
115+
for (size_t i = 0; i < path.size(); ++i) {
116+
if (path[i] == '\\' && i + 1 < path.size()) {
117+
switch (path[i + 1]) {
118+
case 's': {
119+
result.push_back(' ');
120+
break;
121+
}
122+
case 'n': {
123+
result.push_back('\n');
124+
break;
125+
}
126+
case 'b': {
127+
result.push_back('\\');
128+
break;
129+
}
130+
default: {
131+
result.push_back(path[i]);
132+
result.push_back(path[i + 1]);
133+
break;
134+
}
135+
}
136+
++i;
137+
} else {
138+
result.push_back(path[i]);
139+
}
140+
}
141+
return result;
142+
}
143+
111144
class RunfilesCreator {
112145
public:
113146
explicit RunfilesCreator(const std::string &output_base)
@@ -170,13 +203,8 @@ class RunfilesCreator {
170203
if (!s) {
171204
DIE("missing field delimiter at line %d: '%s'\n", lineno, buf);
172205
}
173-
link = std::string(buf + 1, s);
174-
link = std::regex_replace(link, kEscapedSpace, " ");
175-
link = std::regex_replace(link, kEscapedNewline, "\n");
176-
link = std::regex_replace(link, kEscapedBackslash, "\\");
177-
target = s + 1;
178-
target = std::regex_replace(target, kEscapedNewline, "\n");
179-
target = std::regex_replace(target, kEscapedBackslash, "\\");
206+
link = Unescape(std::string(buf + 1, s));
207+
target = Unescape(s + 1);
180208
} else {
181209
// The line is of the form "foo /target/path", with only a single space
182210
// in the link path.

tools/cpp/runfiles/runfiles_src.cc

+35-12
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <fstream>
3030
#include <functional>
3131
#include <map>
32-
#include <regex>
3332
#include <sstream>
3433
#include <vector>
3534

@@ -50,10 +49,6 @@ using std::vector;
5049

5150
namespace {
5251

53-
const std::regex kEscapedBackslash("\\\\b");
54-
const std::regex kEscapedNewline("\\\\n");
55-
const std::regex kEscapedSpace("\\\\s");
56-
5752
bool starts_with(const string& s, const char* prefix) {
5853
if (!prefix || !*prefix) {
5954
return true;
@@ -184,6 +179,39 @@ string GetEnv(const string& key) {
184179
#endif
185180
}
186181

182+
// Replaces \s, \n, and \b with their respective characters.
183+
string Unescape(const string &path) {
184+
string result;
185+
result.reserve(path.size());
186+
for (size_t i = 0; i < path.size(); ++i) {
187+
if (path[i] == '\\' && i + 1 < path.size()) {
188+
switch (path[i + 1]) {
189+
case 's': {
190+
result.push_back(' ');
191+
break;
192+
}
193+
case 'n': {
194+
result.push_back('\n');
195+
break;
196+
}
197+
case 'b': {
198+
result.push_back('\\');
199+
break;
200+
}
201+
default: {
202+
result.push_back(path[i]);
203+
result.push_back(path[i + 1]);
204+
break;
205+
}
206+
}
207+
++i;
208+
} else {
209+
result.push_back(path[i]);
210+
}
211+
}
212+
return result;
213+
}
214+
187215
string Runfiles::Rlocation(const string& path) const {
188216
return Rlocation(path, source_repository_);
189217
}
@@ -274,13 +302,8 @@ bool ParseManifest(const string& path, map<string, string>* result,
274302
}
275303
return false;
276304
}
277-
source = line.substr(1, idx - 1);
278-
source = std::regex_replace(source, kEscapedSpace, " ");
279-
source = std::regex_replace(source, kEscapedNewline, "\n");
280-
source = std::regex_replace(source, kEscapedBackslash, "\\");
281-
target = line.substr(idx + 1);
282-
target = std::regex_replace(target, kEscapedNewline, "\n");
283-
target = std::regex_replace(target, kEscapedBackslash, "\\");
305+
source = Unescape(line.substr(1, idx - 1));
306+
target = Unescape(line.substr(idx + 1));
284307
} else {
285308
string::size_type idx = line.find(' ');
286309
if (idx == string::npos) {

0 commit comments

Comments
 (0)