[PATCH] D87468: [Support] Add GlobPattern::isTrivialMatchAll()

Andrew Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 10 10:15:17 PDT 2020


andrewng created this revision.
andrewng added reviewers: MaskRay, rupprecht.
Herald added a project: LLVM.
andrewng requested review of this revision.

GlobPattern::isTrivialMatchAll() returns true for the GlobPattern "*"
which will match all inputs.

This can be used to avoid performing preparation work for the input when
the result of the match will always be true.


https://reviews.llvm.org/D87468

Files:
  llvm/include/llvm/Support/GlobPattern.h
  llvm/unittests/Support/GlobPatternTest.cpp


Index: llvm/unittests/Support/GlobPatternTest.cpp
===================================================================
--- llvm/unittests/Support/GlobPatternTest.cpp
+++ llvm/unittests/Support/GlobPatternTest.cpp
@@ -133,4 +133,28 @@
   EXPECT_TRUE((bool)Pat2);
   EXPECT_TRUE(Pat2->match("\xFF"));
 }
+
+TEST_F(GlobPatternTest, IsTrivialMatchAll) {
+  Expected<GlobPattern> Pat1 = GlobPattern::create("*");
+  EXPECT_TRUE((bool)Pat1);
+  EXPECT_TRUE(Pat1->isTrivialMatchAll());
+  Expected<GlobPattern> Pat2 = GlobPattern::create("a*");
+  EXPECT_TRUE((bool)Pat2);
+  EXPECT_FALSE(Pat2->isTrivialMatchAll());
+  Expected<GlobPattern> Pat3 = GlobPattern::create("*a");
+  EXPECT_TRUE((bool)Pat3);
+  EXPECT_FALSE(Pat3->isTrivialMatchAll());
+  Expected<GlobPattern> Pat4 = GlobPattern::create("**");
+  EXPECT_TRUE((bool)Pat4);
+  EXPECT_FALSE(Pat4->isTrivialMatchAll());
+  Expected<GlobPattern> Pat5 = GlobPattern::create("?*");
+  EXPECT_TRUE((bool)Pat5);
+  EXPECT_FALSE(Pat5->isTrivialMatchAll());
+  Expected<GlobPattern> Pat6 = GlobPattern::create("*?");
+  EXPECT_TRUE((bool)Pat6);
+  EXPECT_FALSE(Pat6->isTrivialMatchAll());
+  Expected<GlobPattern> Pat7 = GlobPattern::create("\\*");
+  EXPECT_TRUE((bool)Pat7);
+  EXPECT_FALSE(Pat7->isTrivialMatchAll());
+}
 }
Index: llvm/include/llvm/Support/GlobPattern.h
===================================================================
--- llvm/include/llvm/Support/GlobPattern.h
+++ llvm/include/llvm/Support/GlobPattern.h
@@ -31,6 +31,9 @@
   static Expected<GlobPattern> create(StringRef Pat);
   bool match(StringRef S) const;
 
+  // Returns true for glob pattern "*".
+  bool isTrivialMatchAll() const { return Prefix && Prefix->empty(); }
+
 private:
   bool matchOne(ArrayRef<BitVector> Pat, StringRef S) const;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87468.291015.patch
Type: text/x-patch
Size: 1772 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200910/35b58294/attachment.bin>


More information about the llvm-commits mailing list