Cppcheck
utils.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "utils.h"
20 
21 #include <algorithm>
22 #include <cctype>
23 #include <iterator>
24 #include <stack>
25 #include <utility>
26 
27 
28 int caseInsensitiveStringCompare(const std::string &lhs, const std::string &rhs)
29 {
30  if (lhs.size() != rhs.size())
31  return (lhs.size() < rhs.size()) ? -1 : 1;
32  for (unsigned int i = 0; i < lhs.size(); ++i) {
33  const int c1 = std::toupper(lhs[i]);
34  const int c2 = std::toupper(rhs[i]);
35  if (c1 != c2)
36  return (c1 < c2) ? -1 : 1;
37  }
38  return 0;
39 }
40 
41 bool isValidGlobPattern(const std::string& pattern)
42 {
43  for (std::string::const_iterator i = pattern.cbegin(); i != pattern.cend(); ++i) {
44  if (*i == '*' || *i == '?') {
45  const std::string::const_iterator j = i + 1;
46  if (j != pattern.cend() && (*j == '*' || *j == '?')) {
47  return false;
48  }
49  }
50  }
51  return true;
52 }
53 
54 bool matchglob(const std::string& pattern, const std::string& name)
55 {
56  const char* p = pattern.c_str();
57  const char* n = name.c_str();
58  std::stack<std::pair<const char*, const char*>, std::vector<std::pair<const char*, const char*>>> backtrack;
59 
60  for (;;) {
61  bool matching = true;
62  while (*p != '\0' && matching) {
63  switch (*p) {
64  case '*':
65  // Step forward until we match the next character after *
66  while (*n != '\0' && *n != p[1]) {
67  n++;
68  }
69  if (*n != '\0') {
70  // If this isn't the last possibility, save it for later
71  backtrack.emplace(p, n);
72  }
73  break;
74  case '?':
75  // Any character matches unless we're at the end of the name
76  if (*n != '\0') {
77  n++;
78  } else {
79  matching = false;
80  }
81  break;
82  default:
83  // Non-wildcard characters match literally
84  if (*n == *p) {
85  n++;
86  } else if (*n == '\\' && *p == '/') {
87  n++;
88  } else if (*n == '/' && *p == '\\') {
89  n++;
90  } else {
91  matching = false;
92  }
93  break;
94  }
95  p++;
96  }
97 
98  // If we haven't failed matching and we've reached the end of the name, then success
99  if (matching && *n == '\0') {
100  return true;
101  }
102 
103  // If there are no other paths to try, then fail
104  if (backtrack.empty()) {
105  return false;
106  }
107 
108  // Restore pointers from backtrack stack
109  p = backtrack.top().first;
110  n = backtrack.top().second;
111  backtrack.pop();
112 
113  // Advance name pointer by one because the current position didn't work
114  n++;
115  }
116 }
117 
118 bool matchglobs(const std::vector<std::string> &patterns, const std::string &name) {
119  return std::any_of(begin(patterns), end(patterns), [&name](const std::string &pattern) {
120  return matchglob(pattern, name);
121  });
122 }
123 
124 void strTolower(std::string& str)
125 {
126  // This wrapper exists because Sun's CC does not allow a static_cast
127  // from extern "C" int(*)(int) to int(*)(int).
128  std::transform(str.cbegin(), str.cend(), str.begin(), [](int c) {
129  return std::tolower(c);
130  });
131 }
132 
133 std::string trim(const std::string& s, const std::string& t)
134 {
135  const std::string::size_type beg = s.find_first_not_of(t);
136  if (beg == std::string::npos)
137  return "";
138  const std::string::size_type end = s.find_last_not_of(t);
139  return s.substr(beg, end - beg + 1);
140 }
141 
142 void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)
143 {
144  std::string::size_type index = 0;
145  while ((index = source.find(searchFor, index)) != std::string::npos) {
146  source.replace(index, searchFor.length(), replaceWith);
147  index += replaceWith.length();
148  }
149 }
bool matchglob(const std::string &pattern, const std::string &name)
Definition: utils.cpp:54
void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)
Replace all occurrences of searchFor with replaceWith in the given source.
Definition: utils.cpp:142
int caseInsensitiveStringCompare(const std::string &lhs, const std::string &rhs)
Definition: utils.cpp:28
bool isValidGlobPattern(const std::string &pattern)
Definition: utils.cpp:41
bool matchglobs(const std::vector< std::string > &patterns, const std::string &name)
Definition: utils.cpp:118
void strTolower(std::string &str)
Definition: utils.cpp:124
std::string trim(const std::string &s, const std::string &t)
Remove heading and trailing whitespaces from the input parameter.
Definition: utils.cpp:133