Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions llvm/include/llvm/Support/GlobPattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,22 @@ class GlobPattern {
// Returns true for glob pattern "*". Can be used to avoid expensive
// preparation/acquisition of the input for match().
bool isTrivialMatchAll() const {
if (!Prefix.empty())
if (PrefixSize)
return false;
if (!Suffix.empty())
if (SuffixSize)
return false;
if (SubGlobs.size() != 1)
return false;
return SubGlobs[0].getPat() == "*";
}

StringRef prefix() const { return Prefix; }
StringRef suffix() const { return Suffix; }
StringRef prefix() const { return Pattern.take_front(PrefixSize); }
StringRef suffix() const { return Pattern.take_back(SuffixSize); }

private:
StringRef Prefix;
StringRef Suffix;
StringRef Pattern;
size_t PrefixSize = 0;
size_t SuffixSize = 0;

struct SubGlobPattern {
/// \param Pat the pattern to match against
Expand Down
19 changes: 11 additions & 8 deletions llvm/lib/Support/GlobPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,24 @@ parseBraceExpansions(StringRef S, std::optional<size_t> MaxSubPatterns) {
Expected<GlobPattern>
GlobPattern::create(StringRef S, std::optional<size_t> MaxSubPatterns) {
GlobPattern Pat;
Pat.Pattern = S;

// Store the prefix that does not contain any metacharacter.
size_t PrefixSize = S.find_first_of("?*[{\\");
Pat.Prefix = S.substr(0, PrefixSize);
if (PrefixSize == std::string::npos)
Pat.PrefixSize = S.find_first_of("?*[{\\");
if (Pat.PrefixSize == std::string::npos) {
Pat.PrefixSize = S.size();
return Pat;
S = S.substr(PrefixSize);
}
S = S.substr(Pat.PrefixSize);

// Just in case we stop on unmatched opening brackets.
size_t SuffixStart = S.find_last_of("?*[]{}\\");
assert(SuffixStart != std::string::npos);
if (S[SuffixStart] == '\\')
++SuffixStart;
++SuffixStart;
Pat.Suffix = S.substr(SuffixStart);
if (SuffixStart < S.size())
++SuffixStart;
Pat.SuffixSize = S.size() - SuffixStart;
S = S.substr(0, SuffixStart);

SmallVector<std::string, 1> SubPats;
Expand Down Expand Up @@ -200,9 +203,9 @@ GlobPattern::SubGlobPattern::create(StringRef S) {
}

bool GlobPattern::match(StringRef S) const {
if (!S.consume_front(Prefix))
if (!S.consume_front(prefix()))
return false;
if (!S.consume_back(Suffix))
if (!S.consume_back(suffix()))
return false;
if (SubGlobs.empty() && S.empty())
return true;
Expand Down