Path: blob/main/contrib/llvm-project/clang/lib/Format/MatchFilePath.cpp
35232 views
//===--- MatchFilePath.cpp - Match file path with pattern -------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// This file implements the functionality of matching a file path name to10/// a pattern, similar to the POSIX fnmatch() function.11///12//===----------------------------------------------------------------------===//1314#include "MatchFilePath.h"1516using namespace llvm;1718namespace clang {19namespace format {2021// Check whether `FilePath` matches `Pattern` based on POSIX 2.13.1, 2.13.2, and22// Rule 1 of 2.13.3.23bool matchFilePath(StringRef Pattern, StringRef FilePath) {24assert(!Pattern.empty());25assert(!FilePath.empty());2627// No match if `Pattern` ends with a non-meta character not equal to the last28// character of `FilePath`.29if (const auto C = Pattern.back(); !strchr("?*]", C) && C != FilePath.back())30return false;3132constexpr auto Separator = '/';33const auto EOP = Pattern.size(); // End of `Pattern`.34const auto End = FilePath.size(); // End of `FilePath`.35unsigned I = 0; // Index to `Pattern`.3637for (unsigned J = 0; J < End; ++J) {38if (I == EOP)39return false;4041switch (const auto F = FilePath[J]; Pattern[I]) {42case '\\':43if (++I == EOP || F != Pattern[I])44return false;45break;46case '?':47if (F == Separator)48return false;49break;50case '*': {51while (++I < EOP && Pattern[I] == '*') { // Skip consecutive stars.52}53const auto K = FilePath.find(Separator, J); // Index of next `Separator`.54const bool NoMoreSeparatorsInFilePath = K == StringRef::npos;55if (I == EOP) // `Pattern` ends with a star.56return NoMoreSeparatorsInFilePath;57// `Pattern` ends with a lone backslash.58if (Pattern[I] == '\\' && ++I == EOP)59return false;60// The star is followed by a (possibly escaped) `Separator`.61if (Pattern[I] == Separator) {62if (NoMoreSeparatorsInFilePath)63return false;64J = K; // Skip to next `Separator` in `FilePath`.65break;66}67// Recurse.68for (auto Pat = Pattern.substr(I); J < End && FilePath[J] != Separator;69++J) {70if (matchFilePath(Pat, FilePath.substr(J)))71return true;72}73return false;74}75case '[':76// Skip e.g. `[!]`.77if (I + 3 < EOP || (I + 3 == EOP && Pattern[I + 1] != '!')) {78// Skip unpaired `[`, brackets containing slashes, and `[]`.79if (const auto K = Pattern.find_first_of("]/", I + 1);80K != StringRef::npos && Pattern[K] == ']' && K > I + 1) {81if (F == Separator)82return false;83++I; // After the `[`.84bool Negated = false;85if (Pattern[I] == '!') {86Negated = true;87++I; // After the `!`.88}89bool Match = false;90do {91if (I + 2 < K && Pattern[I + 1] == '-') {92Match = Pattern[I] <= F && F <= Pattern[I + 2];93I += 3; // After the range, e.g. `A-Z`.94} else {95Match = F == Pattern[I++];96}97} while (!Match && I < K);98if (Negated ? Match : !Match)99return false;100I = K + 1; // After the `]`.101continue;102}103}104[[fallthrough]]; // Match `[` literally.105default:106if (F != Pattern[I])107return false;108}109110++I;111}112113// Match trailing stars with null strings.114while (I < EOP && Pattern[I] == '*')115++I;116117return I == EOP;118}119120} // namespace format121} // namespace clang122123124