Skip to content

Commit e3a6bb1

Browse files
hchokshifacebook-github-bot
authored andcommitted
Replace std::regex comment stripping
Summary: Fixes lint error `facebook-hte-StdRegexIsAwful`. https://www.internalfb.com/wiki/RegexEngines/ Replaced regex based approach with looping find and erase. Reviewed By: iahs Differential Revision: D77196476 fbshipit-source-id: 8f0bfec33d19e95c46235e20dea7ae291f911972
1 parent 152dbb0 commit e3a6bb1

File tree

1 file changed

+12
-9
lines changed
  • third-party/thrift/src/thrift/compiler/generate/python

1 file changed

+12
-9
lines changed

third-party/thrift/src/thrift/compiler/generate/python/util.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include <thrift/compiler/generate/python/util.h>
1818

19-
#include <regex>
2019
#include <boost/algorithm/string.hpp>
2120
#include <boost/algorithm/string/replace.hpp>
2221
#include <thrift/compiler/generate/cpp/util.h>
@@ -89,14 +88,18 @@ void strip_cpp_comments_and_newlines(std::string& s) {
8988
s.erase(fr, to - fr + 2);
9089
fr = s.find("/*", fr);
9190
}
92-
// strip cpp-style comments
93-
s.replace(
94-
s.begin(),
95-
s.end(),
96-
std::regex_replace(
97-
s,
98-
std::regex("//.*(?=$|\\n)"), /* simulate multiline regex */
99-
""));
91+
92+
// strip cpp-style comments - '//' followed by 0+ non-newline characters
93+
fr = s.find("//");
94+
while (fr != std::string::npos) {
95+
auto to = s.find('\n', fr + 2);
96+
// Erase from '//' through to the following newline
97+
// If to == npos (last line commented without final line break), we erase to
98+
// the end of s
99+
s.erase(fr, to - fr);
100+
101+
fr = s.find("//", fr);
102+
}
100103

101104
// strip newlines
102105
boost::algorithm::replace_all(s, "\n", " ");

0 commit comments

Comments
 (0)