GlobPattern.h 1.8 KB

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