Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- GlobPattern.h - glob pattern matcher implementation -*- C++ -*-----===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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_GLOB_PATTERN_H |
| 15 | #define LLVM_SUPPORT_GLOB_PATTERN_H |
| 16 | |
| 17 | #include "llvm/ADT/BitVector.h" |
| 18 | #include "llvm/ADT/Optional.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/Support/Error.h" |
| 20 | #include <vector> |
| 21 | |
| 22 | // This class represents a glob pattern. Supported metacharacters |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 23 | // are "*", "?", "\", "[<chars>]", "[^<chars>]", and "[!<chars>]". |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 24 | namespace llvm { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 25 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | template <typename T> class ArrayRef; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 27 | class StringRef; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 28 | |
| 29 | class GlobPattern { |
| 30 | public: |
| 31 | static Expected<GlobPattern> create(StringRef Pat); |
| 32 | bool match(StringRef S) const; |
| 33 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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_GLOB_PATTERN_H |