Skip to content

Commit df62b45

Browse files
committed
rebase
Created using spr 1.3.6
2 parents be2bb8b + 260c56f commit df62b45

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

llvm/include/llvm/Support/GlobPattern.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class GlobPattern {
7979
StringRef prefix() const { return Pattern.take_front(PrefixSize); }
8080
// Returns plain suffix of the pattern.
8181
StringRef suffix() const { return Pattern.take_back(SuffixSize); }
82-
// Returns the longest plain substring of the pattern between of prefix and
82+
// Returns the longest plain substring of the pattern between prefix and
8383
// suffix.
8484
StringRef longest_substr() const;
8585

llvm/lib/Support/GlobPattern.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/Support/GlobPattern.h"
1414
#include "llvm/ADT/StringRef.h"
1515
#include "llvm/Support/Errc.h"
16+
#include "llvm/Support/raw_ostream.h"
1617

1718
using namespace llvm;
1819

@@ -133,36 +134,46 @@ parseBraceExpansions(StringRef S, std::optional<size_t> MaxSubPatterns) {
133134
}
134135

135136
static StringRef maxPlainSubstring(StringRef S) {
136-
StringRef R;
137+
StringRef Best;
137138
while (!S.empty()) {
138139
size_t PrefixSize = S.find_first_of("?*[{\\");
139140
if (PrefixSize == std::string::npos)
140141
PrefixSize = S.size();
141142

142-
if (R.size() < PrefixSize)
143-
R = S.take_front(PrefixSize);
143+
if (Best.size() < PrefixSize)
144+
Best = S.take_front(PrefixSize);
145+
144146
S = S.drop_front(PrefixSize);
145147

148+
// It's impossible, as the first and last characters of the input string
149+
// must be Glob special characters, otherwise the would be parts of
150+
// the prefix or the suffix.
151+
assert(!S.empty());
152+
146153
switch (S.front()) {
147154
case '\\':
148155
S = S.drop_front(2);
149156
break;
150157
case '[': {
158+
// Drop '[' and the first character which can be ']'.
159+
S = S.drop_front(2);
151160
size_t EndBracket = S.find_first_of("]");
152-
if (EndBracket == std::string::npos)
153-
return R; // Incorrect, but let SubGlobPattern::create handle it.
161+
// Should not be possible, SubGlobPattern::create should fail on invalid
162+
// pattern before we get here.
163+
assert(EndBracket != std::string::npos);
154164
S = S.drop_front(EndBracket + 1);
155165
break;
156166
}
157167
case '{':
158168
// TODO: implement.
159-
return {};
169+
// Fallback to what ever is best for now.
170+
return Best;
160171
default:
161172
S = S.drop_front(1);
162173
}
163174
}
164175

165-
return R;
176+
return Best;
166177
}
167178

168179
Expected<GlobPattern>

llvm/unittests/Support/GlobPatternTest.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ TEST_F(GlobPatternTest, Substr) {
370370
ASSERT_TRUE((bool)Pat);
371371
EXPECT_EQ("bcd", Pat->longest_substr());
372372

373+
Pat = GlobPattern::create("a*bc[d]efg*h");
374+
ASSERT_TRUE((bool)Pat);
375+
EXPECT_EQ("efg", Pat->longest_substr());
376+
373377
Pat = GlobPattern::create("a*bcde\\fg*h");
374378
ASSERT_TRUE((bool)Pat);
375379
EXPECT_EQ("bcde", Pat->longest_substr());
@@ -384,7 +388,7 @@ TEST_F(GlobPatternTest, Substr) {
384388

385389
Pat = GlobPattern::create("a*bcdef{g}*h");
386390
ASSERT_TRUE((bool)Pat);
387-
EXPECT_EQ("", Pat->longest_substr());
391+
EXPECT_EQ("bcdef", Pat->longest_substr());
388392
}
389393

390394
TEST_F(GlobPatternTest, Pathological) {

0 commit comments

Comments
 (0)