31 #include <initializer_list>
35 #include <type_traits>
41 typename Pair::first_type
operator()(
const Pair& p)
const {
48 typename Pair::second_type
operator()(
const Pair& p)
const {
54 std::function<void()>
f;
61 template<
class Range,
class T>
64 return std::find(r.cbegin(), r.cend(), x) != r.cend();
68 bool contains(
const std::initializer_list<T>& r,
const T& x)
70 return std::find(r.begin(), r.end(), x) != r.end();
73 template<
class T,
class U>
74 bool contains(
const std::initializer_list<T>& r,
const U& x)
76 return std::find(r.begin(), r.end(), x) != r.end();
79 template<
class T,
class ... Ts>
80 inline std::array<T,
sizeof...(Ts) + 1>
makeArray(T x, Ts... xs)
82 return {std::move(x), std::move(xs)...};
90 return static_cast<std::size_t
>(t);
94 inline bool startsWith(
const std::string& str,
const char start[], std::size_t startlen)
96 return str.compare(0, startlen, start) == 0;
99 template<std::
size_t N>
100 bool startsWith(
const std::string& str,
const char (&start)[N])
105 inline bool startsWith(
const std::string& str,
const std::string& start)
107 return startsWith(str, start.c_str(), start.length());
110 inline bool endsWith(
const std::string &str,
char c)
112 return !str.empty() && str.back() == c;
115 inline bool endsWith(
const std::string &str,
const char end[], std::size_t endlen)
117 return (str.size() >= endlen) && (str.compare(str.size()-endlen, endlen, end)==0);
120 template<std::
size_t N>
121 bool endsWith(
const std::string& str,
const char (&end)[N])
129 if (str.length() < p.length() + 2)
137 if (str[p.size()] != q)
141 if (str.compare(0, p.size(), p) != 0)
153 static const std::array<std::string, 5> suffixes{
"",
"u8",
"u",
"U",
"L"};
154 return std::any_of(suffixes.cbegin(), suffixes.cend(), [&](
const std::string& p) {
155 return isPrefixStringCharLiteral(str, q, p);
171 const std::size_t quotePos = str.find(q);
172 return str.substr(quotePos + 1U, str.size() - quotePos - 2U);
210 template<typename T, typename std::enable_if<std::is_signed<T>::value,
bool>::type=
true>
211 bool strToInt(
const std::string& str, T &num, std::string* err =
nullptr)
216 tmp = std::stoll(str, &idx);
217 if (idx != str.size()) {
219 *err =
"not an integer";
222 }
catch (
const std::out_of_range&) {
224 *err =
"out of range (stoll)";
226 }
catch (
const std::invalid_argument &) {
228 *err =
"not an integer";
231 if (str.front() ==
'-' && std::numeric_limits<T>::min() == 0) {
233 *err =
"needs to be positive";
236 if (tmp < std::numeric_limits<T>::min() || tmp > std::numeric_limits<T>::max()) {
238 *err =
"out of range (limits)";
241 num =
static_cast<T
>(tmp);
245 template<typename T, typename std::enable_if<std::is_unsigned<T>::value,
bool>::type=
true>
246 bool strToInt(
const std::string& str, T &num, std::string* err =
nullptr)
248 unsigned long long tmp;
251 tmp = std::stoull(str, &idx);
252 if (idx != str.size()) {
254 *err =
"not an integer";
257 }
catch (
const std::out_of_range&) {
259 *err =
"out of range (stoull)";
261 }
catch (
const std::invalid_argument &) {
263 *err =
"not an integer";
266 if (str.front() ==
'-') {
268 *err =
"needs to be positive";
271 if (tmp > std::numeric_limits<T>::max()) {
273 *err =
"out of range (limits)";
286 throw std::runtime_error(
"converting '" + str +
"' to integer failed - " + err);
294 template<
typename T,
int size>
310 static constexpr
int ptr_size =
sizeof(
void*);
313 static constexpr
int buf_size = (ptr_size * 2) + 1;
318 int idx = buf_size - 1;
324 const uintptr_t temp = l % 16;
331 c =
'a' + (temp - 10);
342 return id_string_i(
reinterpret_cast<uintptr_t
>(p));
347 return b ?
"true" :
"false";
356 CPPCHECKLIB std::string
trim(
const std::string& s,
const std::string& t =
" \t");
371 #if defined(__GNUC__)
372 __builtin_unreachable();
373 #elif defined(_MSC_VER)
376 #error "no unreachable implementation"
NORETURN void unreachable()
std::size_t operator()(T t) const
Pair::first_type operator()(const Pair &p) const
Pair::second_type operator()(const Pair &p) const
static std::string id_string_i(std::uintptr_t l)
get id string.
static bool isStringLiteral(const std::string &str)
static std::string getStringCharLiteral(const std::string &str, char q)
static const char * getOrdinalText(int i)
CPPCHECKLIB void strTolower(std::string &str)
std::array< T, sizeof...(Ts)+1 > makeArray(T x, Ts... xs)
static std::string id_string(const void *p)
CPPCHECKLIB int caseInsensitiveStringCompare(const std::string &lhs, const std::string &rhs)
CPPCHECKLIB bool matchglob(const std::string &pattern, const std::string &name)
static bool isPrefixStringCharLiteral(const std::string &str, char q, const std::string &p)
bool startsWith(const std::string &str, const char start[], std::size_t startlen)
static std::string getStringLiteral(const std::string &str)
bool strToInt(const std::string &str, T &num, std::string *err=nullptr)
CPPCHECKLIB bool isValidGlobPattern(const std::string &pattern)
bool endsWith(const std::string &str, char c)
static bool isCharLiteral(const std::string &str)
static T * empty_if_null(T *p)
CPPCHECKLIB void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)
Replace all occurrences of searchFor with replaceWith in the given source.
static const char * bool_to_string(bool b)
std::size_t getArrayLength(const T(&)[size])
Simple helper function:
static std::string getCharLiteral(const std::string &str)
CPPCHECKLIB std::string trim(const std::string &s, const std::string &t=" \t")
Remove heading and trailing whitespaces from the input parameter.
static bool isStringCharLiteral(const std::string &str, char q)
CPPCHECKLIB bool matchglobs(const std::vector< std::string > &patterns, const std::string &name)
bool contains(const Range &r, const T &x)