Skip to content

Commit

Permalink
fix: glob matching case sensitivity (#112)
Browse files Browse the repository at this point in the history
Ensure glob matching is case insensitive and add test covering case
sensitivity.
  • Loading branch information
dmehala committed Apr 16, 2024
1 parent e4cde4e commit e576445
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
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();
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

0 comments on commit e576445

Please sign in to comment.