Coverage Report

Created: 2024-09-23 06:29

/src/abseil-cpp/absl/log/internal/fnmatch.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2023 The Abseil 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
//     https://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/log/internal/fnmatch.h"
16
17
#include <cstddef>
18
19
#include "absl/base/config.h"
20
#include "absl/strings/string_view.h"
21
22
namespace absl {
23
ABSL_NAMESPACE_BEGIN
24
namespace log_internal {
25
0
bool FNMatch(absl::string_view pattern, absl::string_view str) {
26
0
  bool in_wildcard_match = false;
27
0
  while (true) {
28
0
    if (pattern.empty()) {
29
      // `pattern` is exhausted; succeed if all of `str` was consumed matching
30
      // it.
31
0
      return in_wildcard_match || str.empty();
32
0
    }
33
0
    if (str.empty()) {
34
      // `str` is exhausted; succeed if `pattern` is empty or all '*'s.
35
0
      return pattern.find_first_not_of('*') == pattern.npos;
36
0
    }
37
0
    switch (pattern.front()) {
38
0
      case '*':
39
0
        pattern.remove_prefix(1);
40
0
        in_wildcard_match = true;
41
0
        break;
42
0
      case '?':
43
0
        pattern.remove_prefix(1);
44
0
        str.remove_prefix(1);
45
0
        break;
46
0
      default:
47
0
        if (in_wildcard_match) {
48
0
          absl::string_view fixed_portion = pattern;
49
0
          const size_t end = fixed_portion.find_first_of("*?");
50
0
          if (end != fixed_portion.npos) {
51
0
            fixed_portion = fixed_portion.substr(0, end);
52
0
          }
53
0
          const size_t match = str.find(fixed_portion);
54
0
          if (match == str.npos) {
55
0
            return false;
56
0
          }
57
0
          pattern.remove_prefix(fixed_portion.size());
58
0
          str.remove_prefix(match + fixed_portion.size());
59
0
          in_wildcard_match = false;
60
0
        } else {
61
0
          if (pattern.front() != str.front()) {
62
0
            return false;
63
0
          }
64
0
          pattern.remove_prefix(1);
65
0
          str.remove_prefix(1);
66
0
        }
67
0
        break;
68
0
    }
69
0
  }
70
0
}
71
}  // namespace log_internal
72
ABSL_NAMESPACE_END
73
}  // namespace absl