Skip to content

Commit

Permalink
Merge pull request #50 from zao/fix/case-insensitive-file-search
Browse files Browse the repository at this point in the history
fix: make file searches case-insensitive
  • Loading branch information
Wires77 authored Mar 27, 2024
2 parents 5602a6d + cb3b8cc commit 2d4782f
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions engine/system/win/sys_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,43 @@ bool GlobMatch(const std::filesystem::path& glob, const std::filesystem::path& f
return true;
}

// If no wildcards are present, test file path as-is.
fmt::memory_buffer buf;

// If no wildcards are present, test file path verbatim.
// We use a regex rather than string comparisons to make it case-insensitive.
if (globStr.find_first_of("?*") == std::string::npos) {
return glob == file;
buf.resize(globStr.size());
for (char ch : globStr) {
fmt::format_to(fmt::appender(buf), "[{}]", ch);
}
}

// Otherwise build a regular expression from the glob and use that to match files.
fmt::memory_buffer buf;
auto it = fmt::appender(buf);
for (char ch : glob.generic_string()) {
if (ch == '*') {
it = fmt::format_to(it, ".*");
} else if (ch == '?') {
*it++ = '.';
} else if ("+[]{}+()|"sv.find(ch) != std::string_view::npos) {
// Escape metacharacters
it = fmt::format_to(it, "\\{}", ch);
} else if (std::isalnum((unsigned char)ch)) {
*it++ = ch;
} else {
it = fmt::format_to(it, "[{}]", ch);
else {
// Otherwise build a regular expression from the glob and use that to match files.
auto it = fmt::appender(buf);
for (char ch : glob.generic_string()) {
if (ch == '*') {
it = fmt::format_to(it, ".*");
}
else if (ch == '?') {
*it++ = '.';
}
else if ("+[]{}+()|"sv.find(ch) != std::string_view::npos) {
// Escape metacharacters
it = fmt::format_to(it, "\\{}", ch);
}
else if (std::isalnum((unsigned char)ch)) {
*it++ = ch;
}
else {
it = fmt::format_to(it, "[{}]", ch);
}
}
}
RE2 reGlob{to_string(buf)};

// Assume case-insensitive comparisons are desired.
RE2::Options reOpts;
reOpts.set_case_sensitive(false);
RE2 reGlob{to_string(buf), reOpts};

return RE2::FullMatch(file.generic_string(), reGlob);
}
Expand Down

0 comments on commit 2d4782f

Please sign in to comment.