2018-05-11 15:25:41 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#ifndef SUBSURFACE_STRING_H
|
|
|
|
#define SUBSURFACE_STRING_H
|
|
|
|
|
2024-05-11 11:21:53 +00:00
|
|
|
#include <algorithm>
|
2018-05-11 15:25:41 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2024-05-04 15:55:50 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
2018-05-11 15:25:41 +00:00
|
|
|
|
2022-08-30 15:47:31 +00:00
|
|
|
// string handling
|
|
|
|
|
2018-05-11 15:25:41 +00:00
|
|
|
static inline bool same_string(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
return !strcmp(a ?: "", b ?: "");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool same_string_caseinsensitive(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
return !strcasecmp(a ?: "", b ?: "");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool empty_string(const char *s)
|
|
|
|
{
|
|
|
|
return !s || !*s;
|
|
|
|
}
|
|
|
|
|
2024-04-23 07:06:28 +00:00
|
|
|
extern double permissive_strtod(const char *str, const char **ptr);
|
|
|
|
extern double ascii_strtod(const char *str, const char **ptr);
|
2018-05-11 15:25:41 +00:00
|
|
|
|
2024-03-16 15:06:39 +00:00
|
|
|
// Sadly, starts_with only with C++20!
|
2024-04-29 05:02:54 +00:00
|
|
|
inline bool starts_with(std::string_view s, const char *s2)
|
2024-03-16 15:06:39 +00:00
|
|
|
{
|
|
|
|
return s.rfind(s2, 0) == 0;
|
|
|
|
}
|
|
|
|
|
2024-03-26 14:11:25 +00:00
|
|
|
// Sadly, std::string::contains only with C++23!
|
2024-04-29 05:02:54 +00:00
|
|
|
inline bool contains(std::string_view s, char c)
|
2024-03-26 14:11:25 +00:00
|
|
|
{
|
|
|
|
return s.find(c) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2024-06-13 20:59:32 +00:00
|
|
|
inline bool contains(std::string_view haystack, const char *needle)
|
|
|
|
{
|
|
|
|
return haystack.find(needle) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool contains(std::string_view haystack, const std::string &needle)
|
|
|
|
{
|
|
|
|
return haystack.find(needle) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2024-03-25 09:58:27 +00:00
|
|
|
std::string join(const std::vector<std::string> &l, const std::string &separator, bool skip_empty = false);
|
|
|
|
|
2018-05-11 15:25:41 +00:00
|
|
|
#endif // SUBSURFACE_STRING_H
|