1 //===-- GlobPattern.h - glob pattern matcher implementation -*- C++ -*-----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a glob pattern matcher. The glob pattern is the
10 // rule used by the shell.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_GLOBPATTERN_H
15 #define LLVM_SUPPORT_GLOBPATTERN_H
16 
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/Support/Error.h"
20 #include <vector>
21 
22 // This class represents a glob pattern. Supported metacharacters
23 // are "*", "?", "\", "[<chars>]", "[^<chars>]", and "[!<chars>]".
24 namespace llvm {
25 
26 template <typename T> class ArrayRef;
27 class StringRef;
28 
29 class GlobPattern {
30 public:
31   static Expected<GlobPattern> create(StringRef Pat);
32   bool match(StringRef S) const;
33 
34   // Returns true for glob pattern "*". Can be used to avoid expensive
35   // preparation/acquisition of the input for match().
36   bool isTrivialMatchAll() const {
37     if (Prefix && Prefix->empty()) {
38       assert(!Suffix);
39       return true;
40     }
41     return false;
42   }
43 
44 private:
45   bool matchOne(ArrayRef<BitVector> Pat, StringRef S) const;
46 
47   // Parsed glob pattern.
48   std::vector<BitVector> Tokens;
49 
50   // The following members are for optimization.
51   Optional<StringRef> Exact;
52   Optional<StringRef> Prefix;
53   Optional<StringRef> Suffix;
54 };
55 }
56 
57 #endif // LLVM_SUPPORT_GLOBPATTERN_H
58