Skip to content

Commit

Permalink
identify literal glob patterns
Browse files Browse the repository at this point in the history
#feat
  • Loading branch information
alandefreitas committed Jan 21, 2025
1 parent 7f47ff5 commit b569319
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
36 changes: 36 additions & 0 deletions include/mrdocs/Support/Glob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ class GlobPattern {
bool
matchPatternPrefix(std::string_view prefix, char delimiter) const;

/** Checks if the glob pattern is a literal string.
This function determines if the glob pattern does not contain
any special characters. In other words, it matches a single string.
@return true if the glob pattern is a literal string, false otherwise.
*/
bool
isLiteral() const;

/** Returns the glob pattern.
@return The glob pattern as a string view.
Expand Down Expand Up @@ -185,6 +195,19 @@ class PathGlobPattern {
return glob_.matchPatternPrefix(prefix, '/');
}

/** Checks if the glob pattern is a literal string.
This function determines if the glob pattern does not contain
any special characters. In other words, it matches a single string.
@return true if the glob pattern is a literal string, false otherwise.
*/
bool
isLiteral() const
{
return glob_.isLiteral();
}

/** Returns the glob pattern.
@return The glob pattern as a string view.
Expand Down Expand Up @@ -276,6 +299,19 @@ class SymbolGlobPattern {
return glob_.matchPatternPrefix(prefix, ':');
}

/** Checks if the glob pattern is a literal string.
This function determines if the glob pattern does not contain
any special characters. In other words, it matches a single string.
@return true if the glob pattern is a literal string, false otherwise.
*/
bool
isLiteral() const
{
return glob_.isLiteral();
}

/** Returns the glob pattern.
@return The glob pattern as a string view.
Expand Down
19 changes: 15 additions & 4 deletions src/lib/Support/Glob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,9 @@ match(std::string_view const str, char const delimiter) const
}

bool
GlobPattern::
matchPatternPrefix(std::string_view const str, char const delimiter) const
{
GlobPattern::matchPatternPrefix(
std::string_view const str,
char const delimiter) const {
if (!impl_)
{
return str.empty();
Expand All @@ -604,14 +604,25 @@ matchPatternPrefix(std::string_view const str, char const delimiter) const
for (auto& subGlob: impl_->subGlobs)
{
if (auto m = subGlob.match(suffix, delimiter);
m == SubGlobPattern::MatchType::FULL || m == SubGlobPattern::MatchType::PARTIAL)
m == SubGlobPattern::MatchType::FULL
|| m == SubGlobPattern::MatchType::PARTIAL)
{
return true;
}
}
return false;
}

bool
GlobPattern::isLiteral() const
{
if (!impl_)
{
return true;
}
return impl_->subGlobs.empty();
}

std::string_view
GlobPattern::
pattern() const
Expand Down
32 changes: 32 additions & 0 deletions src/test/Support/Glob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,38 @@ struct Glob_test
BOOST_TEST_NOT(glob.matchPatternPrefix("std"));
}
}

// isLiteral
{
// default constructed to empty string
{
PathGlobPattern glob;
BOOST_TEST(glob.isLiteral());
BOOST_TEST(glob.match(""));
BOOST_TEST_NOT(glob.match("a"));
}

// empty string
{
auto globExp = PathGlobPattern::create("");
BOOST_TEST(globExp);
PathGlobPattern const& glob = *globExp;
BOOST_TEST(glob.isLiteral());
BOOST_TEST(glob.match(""));
BOOST_TEST_NOT(glob.match("a"));
}

// literal string
{
auto globExp = PathGlobPattern::create("abc");
BOOST_TEST(globExp);
PathGlobPattern const& glob = *globExp;
BOOST_TEST(glob.isLiteral());
BOOST_TEST(glob.match("abc"));
BOOST_TEST_NOT(glob.match("abcd"));
BOOST_TEST_NOT(glob.match("a/b/c"));
}
}
}
};

Expand Down

0 comments on commit b569319

Please sign in to comment.