11#include " string_utils.h"
22#include < cctype>
3+ #include < map>
34
45namespace
56{
@@ -15,19 +16,9 @@ namespace
1516
1617 void escape_regex (std::string ®ex)
1718 {
18- replace_substring (regex, " \\ " , " \\\\ " );
19- replace_substring (regex, " ^" , " \\ ^" );
20- replace_substring (regex, " ." , " \\ ." );
21- replace_substring (regex, " $" , " \\ $" );
22- replace_substring (regex, " |" , " \\ |" );
23- replace_substring (regex, " (" , " \\ (" );
24- replace_substring (regex, " )" , " \\ )" );
25- replace_substring (regex, " [" , " \\ [" );
26- replace_substring (regex, " ]" , " \\ ]" );
27- replace_substring (regex, " *" , " \\ *" );
28- replace_substring (regex, " +" , " \\ +" );
29- replace_substring (regex, " ?" , " \\ ?" );
30- replace_substring (regex, " /" , " \\ /" );
19+ std::string escape = " \\ " ;
20+ std::string escapeables[] = {" \\ " , " ^" , " ." , " $" , " |" , " (" , " )" , " [" , " ]" , " *" , " +" , " ?" , " /" };
21+ for (auto &escapeable : escapeables) { replace_substring (regex, escapeable, escape + escapeable); }
3122 }
3223
3324} // namespace
@@ -61,8 +52,9 @@ std::regex helpers::wildcards_regex(std::string wildcard_pattern)
6152 escape_regex (wildcard_pattern);
6253
6354 // Convert chars '*?' back to their regex equivalents
64- replace_substring (wildcard_pattern, " \\ ?" , " ." ) ;
65- replace_substring (wildcard_pattern, " \\ * " , " .* " );
55+ std::map<std::string, std::string> wildcards = {{ " \\ ?" , " ." }, { " \\ * " , " .* " }} ;
56+ for ( auto entry : wildcards) { replace_substring (wildcard_pattern, entry. first , entry. second ); }
6657
58+ // return regular expression created from escaped and wildcarded input
6759 return std::regex (wildcard_pattern);
6860}
0 commit comments