Skip to content

Commit 194ae7c

Browse files
vitalybukaLukacma
authored andcommitted
[NFC][GlobPattern] Change internal structure of GlobPattern (llvm#164513)
Replace two StringRefs with One StringRef + 2 x size_t. Prepare for: * llvm#164512
1 parent 05a7849 commit 194ae7c

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

llvm/include/llvm/Support/GlobPattern.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,22 @@ class GlobPattern {
6363
// Returns true for glob pattern "*". Can be used to avoid expensive
6464
// preparation/acquisition of the input for match().
6565
bool isTrivialMatchAll() const {
66-
if (!Prefix.empty())
66+
if (PrefixSize)
6767
return false;
68-
if (!Suffix.empty())
68+
if (SuffixSize)
6969
return false;
7070
if (SubGlobs.size() != 1)
7171
return false;
7272
return SubGlobs[0].getPat() == "*";
7373
}
7474

75-
StringRef prefix() const { return Prefix; }
76-
StringRef suffix() const { return Suffix; }
75+
StringRef prefix() const { return Pattern.take_front(PrefixSize); }
76+
StringRef suffix() const { return Pattern.take_back(SuffixSize); }
7777

7878
private:
79-
StringRef Prefix;
80-
StringRef Suffix;
79+
StringRef Pattern;
80+
size_t PrefixSize = 0;
81+
size_t SuffixSize = 0;
8182

8283
struct SubGlobPattern {
8384
/// \param Pat the pattern to match against

llvm/lib/Support/GlobPattern.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,24 @@ parseBraceExpansions(StringRef S, std::optional<size_t> MaxSubPatterns) {
135135
Expected<GlobPattern>
136136
GlobPattern::create(StringRef S, std::optional<size_t> MaxSubPatterns) {
137137
GlobPattern Pat;
138+
Pat.Pattern = S;
138139

139140
// Store the prefix that does not contain any metacharacter.
140-
size_t PrefixSize = S.find_first_of("?*[{\\");
141-
Pat.Prefix = S.substr(0, PrefixSize);
142-
if (PrefixSize == std::string::npos)
141+
Pat.PrefixSize = S.find_first_of("?*[{\\");
142+
if (Pat.PrefixSize == std::string::npos) {
143+
Pat.PrefixSize = S.size();
143144
return Pat;
144-
S = S.substr(PrefixSize);
145+
}
146+
S = S.substr(Pat.PrefixSize);
145147

146148
// Just in case we stop on unmatched opening brackets.
147149
size_t SuffixStart = S.find_last_of("?*[]{}\\");
148150
assert(SuffixStart != std::string::npos);
149151
if (S[SuffixStart] == '\\')
150152
++SuffixStart;
151-
++SuffixStart;
152-
Pat.Suffix = S.substr(SuffixStart);
153+
if (SuffixStart < S.size())
154+
++SuffixStart;
155+
Pat.SuffixSize = S.size() - SuffixStart;
153156
S = S.substr(0, SuffixStart);
154157

155158
SmallVector<std::string, 1> SubPats;
@@ -200,9 +203,9 @@ GlobPattern::SubGlobPattern::create(StringRef S) {
200203
}
201204

202205
bool GlobPattern::match(StringRef S) const {
203-
if (!S.consume_front(Prefix))
206+
if (!S.consume_front(prefix()))
204207
return false;
205-
if (!S.consume_back(Suffix))
208+
if (!S.consume_back(suffix()))
206209
return false;
207210
if (SubGlobs.empty() && S.empty())
208211
return true;

0 commit comments

Comments
 (0)