Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/datadog/glob.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "glob.h"

#include <cctype>
#include <cstdint>

namespace datadog {
Expand All @@ -18,8 +19,11 @@ bool glob_match(StringView pattern, StringView subject) {
Index next_p = 0; // next [p]attern index
Index next_s = 0; // next [s]ubject index

while (p < pattern.size() || s < subject.size()) {
if (p < pattern.size()) {
const size_t p_size = pattern.size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I can't comment on the relevant line, but instead of using Index = std::size_t; it would make more sense to use using Index = StringView::size_type;, it should work as expected with both absl::string_view and std::string_view. In practice it doesn't make a difference.

const size_t s_size = subject.size();

while (p < p_size || s < s_size) {
if (p < p_size) {
const char pattern_char = pattern[p];
switch (pattern_char) {
case '*':
Expand All @@ -30,22 +34,22 @@ bool glob_match(StringView pattern, StringView subject) {
++p;
continue;
case '?':
if (s < subject.size()) {
if (s < s_size) {
++p;
++s;
continue;
}
break;
default:
if (s < subject.size() && subject[s] == pattern_char) {
if (s < s_size && tolower(subject[s]) == tolower(pattern_char)) {
++p;
++s;
continue;
}
}
}
// Mismatch. Maybe restart.
if (0 < next_s && next_s <= subject.size()) {
if (0 < next_s && next_s <= s_size) {
p = next_p;
s = next_s;
continue;
Expand Down
10 changes: 8 additions & 2 deletions test/test_glob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using namespace datadog::tracing;

TEST_CASE("glob") {
TEST_CASE("glob", "[glob]") {
struct TestCase {
StringView pattern;
StringView subject;
Expand Down Expand Up @@ -42,7 +42,13 @@ TEST_CASE("glob") {
{"", "", true},
{"", "a", false},
{"*", "", true},
{"?", "", false}
{"?", "", false},

// case sensitivity
{"true", "TRUE", true},
{"true", "True", true},
{"true", "tRue", true},
{"false", "FALSE", true}
}));
// clang-format on

Expand Down