2323#include < initializer_list>
2424#include < ios>
2525#include < memory>
26- #include < regex> // NOLINT
2726#include < string>
2827#include < utility>
2928#include < vector>
3635#include " absl/strings/str_replace.h"
3736#include " absl/strings/string_view.h"
3837#include " absl/types/optional.h"
38+ #include " third_party/re2/re2.h"
3939#include " yggdrasil_decision_forests/utils/filesystem_interface.h"
4040#include " yggdrasil_decision_forests/utils/logging.h"
4141#include " yggdrasil_decision_forests/utils/status_macros.h"
@@ -164,18 +164,12 @@ std::string JoinPathList(std::initializer_list<absl::string_view> paths) {
164164
165165bool GenerateShardedFilenames (absl::string_view spec,
166166 std::vector<std::string>* names) {
167- std::regex num_shard_pattern ( R"( (.*)\@(\*|[0-9]+)(?:(\..+))?)" ) ;
168- std::smatch match ;
167+ static const LazyRE2 num_shard_pattern = { R"( (.*)\@(\*|[0-9]+)(?:(\..+))?)" } ;
168+ std::string prefix, count, suffix ;
169169 std::string str_spec (spec);
170- if (!std::regex_match (str_spec, match, num_shard_pattern )) {
170+ if (!RE2::FullMatch (str_spec, *num_shard_pattern, &prefix, &count, &suffix )) {
171171 return false ;
172172 }
173- if (match.size () != 4 ) {
174- return false ;
175- }
176- const auto prefix = match[1 ].str ();
177- const auto count = match[2 ].str ();
178- const auto suffix = match[3 ].str ();
179173
180174 int int_count;
181175 if (count == " *" ) {
@@ -212,7 +206,8 @@ absl::Status Match(absl::string_view pattern, std::vector<std::string>* results,
212206 const auto filename = fs::path (SV_ABSL_TO_STD (pattern)).filename ().string ();
213207 std::string regexp_filename =
214208 absl::StrReplaceAll (filename, {{" ." , " \\ ." }, {" *" , " .*" }, {" ?" , " ." }});
215- std::regex regexp_pattern (regexp_filename);
209+ RE2 regexp_pattern (regexp_filename);
210+ STATUS_CHECK (regexp_pattern.ok ());
216211 std::error_code error;
217212
218213 const fs::directory_iterator path_end;
@@ -221,7 +216,7 @@ absl::Status Match(absl::string_view pattern, std::vector<std::string>* results,
221216 if (!fs::is_regular_file (path->path ())) {
222217 continue ;
223218 }
224- if (std::regex_match (path->path ().filename ().string (), regexp_pattern)) {
219+ if (RE2::FullMatch (path->path ().filename ().string (), regexp_pattern)) {
225220 results->push_back (path->path ().string ());
226221 }
227222 }
0 commit comments