5 #ifndef SPA_UTILS_STRING_H
6 #define SPA_UTILS_STRING_H
36 static inline bool spa_streq(
const char *s1,
const char *s2)
38 return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
46 static inline bool spa_strneq(
const char *s1,
const char *s2,
size_t len)
48 return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
64 return strncmp(s, prefix, strlen(prefix)) == 0;
84 return l1 >= l2 &&
spa_streq(s + l1 - l2, suffix);
95 static inline bool spa_atoi32(
const char *str, int32_t *val,
int base)
100 if (!str || *str ==
'\0')
104 v = strtol(str, &endptr, base);
105 if (errno != 0 || *endptr !=
'\0')
123 static inline bool spa_atou32(
const char *str, uint32_t *val,
int base)
126 unsigned long long v;
128 if (!str || *str ==
'\0')
132 v = strtoull(str, &endptr, base);
133 if (errno != 0 || *endptr !=
'\0')
136 if (v != (uint32_t)v)
151 static inline bool spa_atoi64(
const char *str, int64_t *val,
int base)
156 if (!str || *str ==
'\0')
160 v = strtoll(str, &endptr, base);
161 if (errno != 0 || *endptr !=
'\0')
176 static inline bool spa_atou64(
const char *str, uint64_t *val,
int base)
179 unsigned long long v;
181 if (!str || *str ==
'\0')
185 v = strtoull(str, &endptr, base);
186 if (errno != 0 || *endptr !=
'\0')
199 static inline bool spa_atob(
const char *str)
213 static inline
int spa_vscnprintf(
char *buffer,
size_t size, const
char *format, va_list args)
219 r = vsnprintf(buffer, size, format, args);
236 static inline
int spa_scnprintf(
char *buffer,
size_t size, const
char *format, ...)
241 va_start(args, format);
256 static inline float spa_strtof(
const char *str,
char **endptr)
258 #ifndef __LOCALE_C_ONLY
259 static locale_t locale = NULL;
263 #ifndef __LOCALE_C_ONLY
265 locale = newlocale(LC_ALL_MASK,
"C", NULL);
266 prev = uselocale(locale);
268 v = strtof(str, endptr);
269 #ifndef __LOCALE_C_ONLY
282 static inline bool spa_atof(
const char *str,
float *val)
287 if (!str || *str ==
'\0')
291 if (errno != 0 || *endptr !=
'\0')
306 static inline double spa_strtod(
const char *str,
char **endptr)
308 #ifndef __LOCALE_C_ONLY
309 static locale_t locale = NULL;
313 #ifndef __LOCALE_C_ONLY
315 locale = newlocale(LC_ALL_MASK,
"C", NULL);
316 prev = uselocale(locale);
318 v = strtod(str, endptr);
319 #ifndef __LOCALE_C_ONLY
332 static inline bool spa_atod(
const char *str,
double *val)
337 if (!str || *str ==
'\0')
342 if (errno != 0 || *endptr !=
'\0')
349 static inline char *
spa_dtoa(
char *str,
size_t size,
double val)
353 for (i = 0; i < l; i++)
375 size_t remain = buf->maxsize - buf->pos;
379 written = vsnprintf(&buf->buffer[buf->pos], remain, fmt, args);
382 buf->pos +=
SPA_MIN(remain, (
size_t)written);
static bool spa_atod(const char *str, double *val)
Convert str to a double and store the result in val.
Definition: string.h:337
static bool spa_atou64(const char *str, uint64_t *val, int base)
Convert str to an uint64_t with the given base and store the result in val.
Definition: string.h:181
static bool spa_strstartswith(const char *s, const char *prefix)
Definition: string.h:62
static double spa_strtod(const char *str, char **endptr)
Convert str to a double in the C locale.
Definition: string.h:311
static bool spa_atob(const char *str)
Convert str to a boolean.
Definition: string.h:204
static bool spa_atoi64(const char *str, int64_t *val, int base)
Convert str to an int64_t with the given base and store the result in val.
Definition: string.h:156
static bool spa_atou32(const char *str, uint32_t *val, int base)
Convert str to an uint32_t with the given base and store the result in val.
Definition: string.h:128
static bool spa_atoi32(const char *str, int32_t *val, int base)
Convert str to an int32_t with the given base and store the result in val.
Definition: string.h:100
static bool spa_strendswith(const char *s, const char *suffix)
Definition: string.h:78
static bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition: string.h:51
static int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt,...)
Definition: string.h:378
static float spa_strtof(const char *str, char **endptr)
Convert str to a float in the C locale.
Definition: string.h:261
static void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
Definition: string.h:370
static int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
Definition: string.h:218
static bool spa_streq(const char *s1, const char *s2)
Definition: string.h:41
static bool spa_atof(const char *str, float *val)
Convert str to a float and store the result in val.
Definition: string.h:287
static int spa_scnprintf(char *buffer, size_t size, const char *format,...)
Definition: string.h:241
static char * spa_dtoa(char *str, size_t size, double val)
Definition: string.h:354
#define SPA_MIN(a, b)
Definition: defs.h:147
#define spa_assert_se(expr)
Definition: defs.h:373
#define SPA_LIKELY(x)
Definition: defs.h:341
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition: defs.h:269
#define SPA_UNLIKELY(x)
Definition: defs.h:343
size_t pos
Definition: string.h:367
size_t maxsize
Definition: string.h:366
char * buffer
Definition: string.h:365