| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef CLAZY_STL_H |
| 8 |
#define CLAZY_STL_H |
| 9 |
|
| 10 |
#include <clang/AST/Stmt.h> |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <cctype> |
| 14 |
#include <sstream> |
| 15 |
#include <string_view> |
| 16 |
|
| 17 |
namespace clazy |
| 18 |
{ |
| 19 |
// Don't use .begin() or cend(), clang's ranges don't have them |
| 20 |
// Don't use .size(), clang's ranges doesn't have it |
| 21 |
|
| 22 |
template<typename C> |
| 23 |
bool contains(const C &container, const typename C::value_type &value) |
| 24 |
{ |
| 25 |
return std::ranges::find(container, value) != container.end(); |
| 26 |
} |
| 27 |
|
| 28 |
inline bool contains(const std::string &haystack, const std::string &needle) |
| 29 |
{ |
| 30 |
return haystack.find(needle) != std::string::npos; |
| 31 |
} |
| 32 |
|
| 33 |
template<typename Range, typename Pred> |
| 34 |
bool any_of(const Range &r, Pred pred) |
| 35 |
{ |
| 36 |
return std::any_of(r.begin(), r.end(), pred); |
| 37 |
} |
| 38 |
|
| 39 |
inline bool hasChildren(clang::Stmt *s) |
| 40 |
{ |
| 41 |
return s && !s->children().empty(); |
| 42 |
} |
| 43 |
|
| 44 |
inline clang::Stmt *childAt(clang::Stmt *s, int index) |
| 45 |
{ |
| 46 |
int count = s ? std::distance(s->child_begin(), s->child_end()) : 0; |
| 47 |
if (count > index) { |
| 48 |
auto it = s->child_begin(); |
| 49 |
while (index > 0) { |
| 50 |
++it; |
| 51 |
--index; |
| 52 |
} |
| 53 |
return *it; |
| 54 |
} |
| 55 |
|
| 56 |
return nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns true if the string target starts with maybeBeginning |
| 61 |
*/ |
| 62 |
inline bool startsWith(std::string_view target, std::string_view maybeBeginning) |
| 63 |
{ |
| 64 |
return target.compare(0, maybeBeginning.length(), maybeBeginning) == 0; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns true if the string target starts with any of the strings in beginningCandidates |
| 69 |
*/ |
| 70 |
inline bool startsWithAny(std::string_view target, const std::vector<std::string> &beginningCandidates) |
| 71 |
{ |
| 72 |
return std::ranges::any_of(beginningCandidates, [target](const std::string &maybeBeginning) { |
| 73 |
return clazy::startsWith(target, maybeBeginning); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns true if the target equals any of the candidate strings |
| 79 |
*/ |
| 80 |
inline bool equalsAny(const std::string &target, const std::vector<std::string> &candidates) |
| 81 |
{ |
| 82 |
return std::ranges::any_of(candidates, [target](const std::string &candidate) { |
| 83 |
return candidate == target; |
| 84 |
}); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns true if the string target ends with maybeEnding |
| 89 |
*/ |
| 90 |
inline bool endsWith(std::string_view target, std::string_view maybeEnding) |
| 91 |
{ |
| 92 |
return target.size() >= maybeEnding.size() && target.compare(target.size() - maybeEnding.size(), maybeEnding.size(), maybeEnding) == 0; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns true if the string target ends with any of the strings in endingCandidates |
| 97 |
*/ |
| 98 |
inline bool endsWithAny(const std::string &target, const std::vector<std::string> &endingCandidates) |
| 99 |
{ |
| 100 |
return std::ranges::any_of(endingCandidates, [target](const std::string &maybeEnding) { |
| 101 |
return clazy::endsWith(target, maybeEnding); |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
inline std::string toLower(const std::string &s) |
| 106 |
{ |
| 107 |
std::string result(s.size(), 0); |
| 108 |
std::transform(s.begin(), s.end(), result.begin(), ::tolower); |
| 109 |
return result; |
| 110 |
} |
| 111 |
|
| 112 |
inline void rtrim(std::string &s) |
| 113 |
{ |
| 114 |
while (!s.empty() && std::isspace(s.back())) { |
| 115 |
s.pop_back(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
inline std::vector<std::string_view> splitStringBySpaces(std::string_view str) |
| 120 |
{ |
| 121 |
auto nextWord = [str](decltype(str)::const_iterator i) { |
| 122 |
auto isSpace = [](char c) { |
| 123 |
return std::isspace(c); |
| 124 |
}; |
| 125 |
auto first = std::find_if_not(i, str.cend(), isSpace); |
| 126 |
return std::make_pair(first, std::find_if(first, str.cend(), isSpace)); |
| 127 |
}; |
| 128 |
|
| 129 |
std::vector<std::string_view> result; |
| 130 |
for (auto w = nextWord(str.cbegin()); w.first != str.cend(); w = nextWord(w.second)) { |
| 131 |
// TODO[C++20] Use string_view(begin, end) constructor instead |
| 132 |
result.emplace_back(std::addressof(*w.first), std::distance(w.first, w.second)); |
| 133 |
} |
| 134 |
return result; |
| 135 |
} |
| 136 |
|
| 137 |
inline std::vector<std::string> splitString(const std::string &str, char separator) |
| 138 |
{ |
| 139 |
std::string token; |
| 140 |
std::vector<std::string> result; |
| 141 |
std::istringstream istream(str); |
| 142 |
while (std::getline(istream, token, separator)) { |
| 143 |
result.push_back(token); |
| 144 |
} |
| 145 |
|
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
inline std::vector<std::string> splitString(const char *str, char separator) |
| 150 |
{ |
| 151 |
if (!str) { |
| 152 |
return {}; |
| 153 |
} |
| 154 |
|
| 155 |
return clazy::splitString(std::string(str), separator); |
| 156 |
} |
| 157 |
|
| 158 |
template<typename Container, typename LessThan> |
| 159 |
void sort_and_remove_dups(Container &c, LessThan lessThan) |
| 160 |
{ |
| 161 |
std::ranges::sort(c, lessThan); |
| 162 |
c.erase(std::unique(c.begin(), c.end()), c.end()); |
| 163 |
} |
| 164 |
|
| 165 |
inline std::string unquoteString(const std::string &str) |
| 166 |
{ |
| 167 |
// If first and last are ", return what's in between quotes: |
| 168 |
if (str.size() >= 3 && str[0] == '"' && str.at(str.size() - 1) == '"') { |
| 169 |
return str.substr(1, str.size() - 2); |
| 170 |
} |
| 171 |
|
| 172 |
return str; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
#endif |