Skip to content

Commit b569319

Browse files
committed
identify literal glob patterns
#feat
1 parent 7f47ff5 commit b569319

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

include/mrdocs/Support/Glob.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ class GlobPattern {
9797
bool
9898
matchPatternPrefix(std::string_view prefix, char delimiter) const;
9999

100+
/** Checks if the glob pattern is a literal string.
101+
102+
This function determines if the glob pattern does not contain
103+
any special characters. In other words, it matches a single string.
104+
105+
@return true if the glob pattern is a literal string, false otherwise.
106+
*/
107+
bool
108+
isLiteral() const;
109+
100110
/** Returns the glob pattern.
101111
102112
@return The glob pattern as a string view.
@@ -185,6 +195,19 @@ class PathGlobPattern {
185195
return glob_.matchPatternPrefix(prefix, '/');
186196
}
187197

198+
/** Checks if the glob pattern is a literal string.
199+
200+
This function determines if the glob pattern does not contain
201+
any special characters. In other words, it matches a single string.
202+
203+
@return true if the glob pattern is a literal string, false otherwise.
204+
*/
205+
bool
206+
isLiteral() const
207+
{
208+
return glob_.isLiteral();
209+
}
210+
188211
/** Returns the glob pattern.
189212
190213
@return The glob pattern as a string view.
@@ -276,6 +299,19 @@ class SymbolGlobPattern {
276299
return glob_.matchPatternPrefix(prefix, ':');
277300
}
278301

302+
/** Checks if the glob pattern is a literal string.
303+
304+
This function determines if the glob pattern does not contain
305+
any special characters. In other words, it matches a single string.
306+
307+
@return true if the glob pattern is a literal string, false otherwise.
308+
*/
309+
bool
310+
isLiteral() const
311+
{
312+
return glob_.isLiteral();
313+
}
314+
279315
/** Returns the glob pattern.
280316
281317
@return The glob pattern as a string view.

src/lib/Support/Glob.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,9 @@ match(std::string_view const str, char const delimiter) const
581581
}
582582

583583
bool
584-
GlobPattern::
585-
matchPatternPrefix(std::string_view const str, char const delimiter) const
586-
{
584+
GlobPattern::matchPatternPrefix(
585+
std::string_view const str,
586+
char const delimiter) const {
587587
if (!impl_)
588588
{
589589
return str.empty();
@@ -604,14 +604,25 @@ matchPatternPrefix(std::string_view const str, char const delimiter) const
604604
for (auto& subGlob: impl_->subGlobs)
605605
{
606606
if (auto m = subGlob.match(suffix, delimiter);
607-
m == SubGlobPattern::MatchType::FULL || m == SubGlobPattern::MatchType::PARTIAL)
607+
m == SubGlobPattern::MatchType::FULL
608+
|| m == SubGlobPattern::MatchType::PARTIAL)
608609
{
609610
return true;
610611
}
611612
}
612613
return false;
613614
}
614615

616+
bool
617+
GlobPattern::isLiteral() const
618+
{
619+
if (!impl_)
620+
{
621+
return true;
622+
}
623+
return impl_->subGlobs.empty();
624+
}
625+
615626
std::string_view
616627
GlobPattern::
617628
pattern() const

src/test/Support/Glob.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,38 @@ struct Glob_test
551551
BOOST_TEST_NOT(glob.matchPatternPrefix("std"));
552552
}
553553
}
554+
555+
// isLiteral
556+
{
557+
// default constructed to empty string
558+
{
559+
PathGlobPattern glob;
560+
BOOST_TEST(glob.isLiteral());
561+
BOOST_TEST(glob.match(""));
562+
BOOST_TEST_NOT(glob.match("a"));
563+
}
564+
565+
// empty string
566+
{
567+
auto globExp = PathGlobPattern::create("");
568+
BOOST_TEST(globExp);
569+
PathGlobPattern const& glob = *globExp;
570+
BOOST_TEST(glob.isLiteral());
571+
BOOST_TEST(glob.match(""));
572+
BOOST_TEST_NOT(glob.match("a"));
573+
}
574+
575+
// literal string
576+
{
577+
auto globExp = PathGlobPattern::create("abc");
578+
BOOST_TEST(globExp);
579+
PathGlobPattern const& glob = *globExp;
580+
BOOST_TEST(glob.isLiteral());
581+
BOOST_TEST(glob.match("abc"));
582+
BOOST_TEST_NOT(glob.match("abcd"));
583+
BOOST_TEST_NOT(glob.match("a/b/c"));
584+
}
585+
}
554586
}
555587
};
556588

0 commit comments

Comments
 (0)