2018-05-11 15:25:41 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#ifndef SUBSURFACE_STRING_H
|
|
|
|
#define SUBSURFACE_STRING_H
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
// shared generic definitions and macros
|
|
|
|
// mostly about strings, but a couple of math macros are here as well
|
|
|
|
|
|
|
|
/* Windows has no MIN/MAX macros - so let's just roll our own */
|
2024-03-16 21:09:58 +00:00
|
|
|
#ifndef MIN
|
2018-05-11 15:25:41 +00:00
|
|
|
#define MIN(x, y) ({ \
|
|
|
|
__typeof__(x) _min1 = (x); \
|
|
|
|
__typeof__(y) _min2 = (y); \
|
|
|
|
(void) (&_min1 == &_min2); \
|
|
|
|
_min1 < _min2 ? _min1 : _min2; })
|
2024-03-16 21:09:58 +00:00
|
|
|
#endif
|
2018-05-11 15:25:41 +00:00
|
|
|
|
2024-03-16 21:09:58 +00:00
|
|
|
#ifndef MAX
|
2018-05-11 15:25:41 +00:00
|
|
|
#define MAX(x, y) ({ \
|
|
|
|
__typeof__(x) _max1 = (x); \
|
|
|
|
__typeof__(y) _max2 = (y); \
|
|
|
|
(void) (&_max1 == &_max2); \
|
|
|
|
_max1 > _max2 ? _max1 : _max2; })
|
2024-03-16 21:09:58 +00:00
|
|
|
#endif
|
2018-05-11 15:25:41 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *copy_string(const char *s)
|
|
|
|
{
|
|
|
|
return (s && *s) ? strdup(s) : NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2024-03-01 22:27:55 +00:00
|
|
|
|
|
|
|
#include <string>
|
2024-03-25 09:58:27 +00:00
|
|
|
#include <vector>
|
2024-03-01 22:27:55 +00:00
|
|
|
|
2024-03-16 15:06:39 +00:00
|
|
|
// Sadly, starts_with only with C++20!
|
|
|
|
inline bool starts_with(const std::string &s, const char *s2)
|
|
|
|
{
|
|
|
|
return s.rfind(s2, 0) == 0;
|
|
|
|
}
|
|
|
|
|
2024-03-26 14:11:25 +00:00
|
|
|
// Sadly, std::string::contains only with C++23!
|
|
|
|
inline bool contains(const std::string &s, char c)
|
|
|
|
{
|
|
|
|
return s.find(c) != 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
|
2024-03-13 08:41:11 +00:00
|
|
|
|
2018-05-11 15:25:41 +00:00
|
|
|
#endif // SUBSURFACE_STRING_H
|