• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/string_view.h"
16 
17 namespace grpc_core {
18 
19 namespace {
IsGlob(absl::string_view trace_glob)20 bool IsGlob(absl::string_view trace_glob) {
21   return std::any_of(trace_glob.begin(), trace_glob.end(),
22                      [](const char c) { return c == '?' || c == '*'; });
23 }
24 }  // namespace
25 
GlobMatch(absl::string_view name,absl::string_view pattern)26 bool GlobMatch(absl::string_view name, absl::string_view pattern) {
27   if (!IsGlob(pattern)) return name == pattern;
28   size_t name_idx = 0;
29   size_t trace_idx = 0;
30   // pointers for iterative wildcard * matching.
31   size_t name_next_idx = name_idx;
32   size_t trace_next_idx = trace_idx;
33 
34   while (trace_idx < pattern.length() || name_idx < name.length()) {
35     if (trace_idx < pattern.length()) {
36       switch (pattern.at(trace_idx)) {
37         case '?':
38           if (name_idx < name.length()) {
39             ++trace_idx;
40             ++name_idx;
41             continue;
42           }
43           break;
44         case '*':
45           trace_next_idx = trace_idx;
46           name_next_idx = name_idx + 1;
47           ++trace_idx;
48           continue;
49         default:
50           if (name_idx < name.length() &&
51               name.at(name_idx) == pattern.at(trace_idx)) {
52             ++trace_idx;
53             ++name_idx;
54             continue;
55           }
56           break;
57       }
58     }
59     // Failed to match a character. Restart if possible.
60     if (name_next_idx > 0 && name_next_idx <= name.length()) {
61       trace_idx = trace_next_idx;
62       name_idx = name_next_idx;
63       continue;
64     }
65     return false;
66   }
67   return true;
68 }
69 
70 }  // namespace grpc_core
71