blob: b79de6f41c494a3dba19e2dd08a621c2cc604131 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- GlobPattern.h - glob pattern matcher implementation -*- C++ -*-----===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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 Scull5e1ddfa2018-08-14 10:06:54 +01006//
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 Scull5e1ddfa2018-08-14 10:06:54 +010019#include "llvm/Support/Error.h"
20#include <vector>
21
22// This class represents a glob pattern. Supported metacharacters
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020023// are "*", "?", "\", "[<chars>]", "[^<chars>]", and "[!<chars>]".
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010024namespace llvm {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020025
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026template <typename T> class ArrayRef;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020027class StringRef;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010028
29class GlobPattern {
30public:
31 static Expected<GlobPattern> create(StringRef Pat);
32 bool match(StringRef S) const;
33
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020034 // 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 Scull5e1ddfa2018-08-14 10:06:54 +010044private:
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