cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
check_glob_pattern.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <cstddef>
23
25
26namespace stat_bench {
27namespace util {
28
29namespace {
30
31[[nodiscard]] auto check_glob_pattern_impl_asterisk(
32 StringView pattern, StringView str) -> bool {
33 const StringView remaining_pattern = pattern.substr(1U);
34 for (std::size_t size = 0; size < str.size(); ++size) {
35 if (check_glob_pattern(remaining_pattern, str.substr(size))) {
36 return true;
37 }
38 }
39 return check_glob_pattern(remaining_pattern, StringView());
40}
41
42} // namespace
43
44auto check_glob_pattern(StringView pattern, StringView str) -> bool {
45 std::size_t pattern_index = 0;
46 std::size_t str_index = 0;
47 while (true) {
48 if (pattern_index >= pattern.size()) {
49 return str_index == str.size();
50 }
51
52 if (pattern.at(pattern_index) == '*') {
53 return check_glob_pattern_impl_asterisk(
54 pattern.substr(pattern_index), str.substr(str_index));
55 }
56
57 if (str_index >= str.size()) {
58 return false;
59 }
60
61 if (pattern.at(pattern_index) != str.at(str_index)) {
62 return false;
63 }
64
65 ++pattern_index;
66 ++str_index;
67 }
68}
69
70} // namespace util
71} // namespace stat_bench
Declaration of check_glob_pattern function.
Class of view of strings.
Definition string_view.h:34
auto substr(std::size_t pos) const -> StringView
Get a part of the string.
Namespace of utility functions and classes.
auto check_glob_pattern(StringView pattern, StringView str) -> bool
Check whether a string matches with a glob pattern.
Namespace of stat_bench source codes.
Declaration of check_glob_pattern function.