25#ifndef SPA_UTILS_STRING_H
26#define SPA_UTILS_STRING_H
56static inline bool spa_streq(
const char *s1,
const char *s2)
58 return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
66static inline bool spa_strneq(
const char *s1,
const char *s2,
size_t len)
68 return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
84 return strncmp(s, prefix, strlen(prefix)) == 0;
104 return l1 >= l2 &&
spa_streq(s + l1 - l2, suffix);
115static inline bool spa_atoi32(
const char *str, int32_t *val,
int base)
120 if (!str || *str ==
'\0')
124 v = strtol(str, &endptr, base);
125 if (errno != 0 || *endptr !=
'\0')
143static inline bool spa_atou32(
const char *str, uint32_t *val,
int base)
146 unsigned long long v;
148 if (!str || *str ==
'\0')
152 v = strtoull(str, &endptr, base);
153 if (errno != 0 || *endptr !=
'\0')
156 if (v != (uint32_t)v)
171static inline bool spa_atoi64(
const char *str, int64_t *val,
int base)
176 if (!str || *str ==
'\0')
180 v = strtoll(str, &endptr, base);
181 if (errno != 0 || *endptr !=
'\0')
196static inline bool spa_atou64(
const char *str, uint64_t *val,
int base)
199 unsigned long long v;
201 if (!str || *str ==
'\0')
205 v = strtoull(str, &endptr, base);
206 if (errno != 0 || *endptr !=
'\0')
219static inline bool spa_atob(
const char *str)
233static inline
int spa_vscnprintf(
char *buffer,
size_t size, const
char *format, va_list args)
239 r = vsnprintf(buffer, size, format, args);
256static inline
int spa_scnprintf(
char *buffer,
size_t size, const
char *format, ...)
261 va_start(args, format);
276static inline float spa_strtof(
const char *str,
char **endptr)
278#ifndef __LOCALE_C_ONLY
279 static locale_t locale = NULL;
283#ifndef __LOCALE_C_ONLY
285 locale = newlocale(LC_ALL_MASK,
"C", NULL);
286 prev = uselocale(locale);
288 v = strtof(str, endptr);
289#ifndef __LOCALE_C_ONLY
302static inline bool spa_atof(
const char *str,
float *val)
307 if (!str || *str ==
'\0')
311 if (errno != 0 || *endptr !=
'\0')
326static inline double spa_strtod(
const char *str,
char **endptr)
328#ifndef __LOCALE_C_ONLY
329 static locale_t locale = NULL;
333#ifndef __LOCALE_C_ONLY
335 locale = newlocale(LC_ALL_MASK,
"C", NULL);
336 prev = uselocale(locale);
338 v = strtod(str, endptr);
339#ifndef __LOCALE_C_ONLY
352static inline bool spa_atod(
const char *str,
double *val)
357 if (!str || *str ==
'\0')
362 if (errno != 0 || *endptr !=
'\0')
369static inline char *
spa_dtoa(
char *str,
size_t size,
double val)
373 for (i = 0; i < l; i++)
395 size_t remain = buf->maxsize - buf->pos;
399 written = vsnprintf(&buf->buffer[buf->pos], remain, fmt, args);
402 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:357
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:201
static bool spa_strstartswith(const char *s, const char *prefix)
Definition: string.h:82
static double spa_strtod(const char *str, char **endptr)
Convert str to a double in the C locale.
Definition: string.h:331
static bool spa_atob(const char *str)
Convert str to a boolean.
Definition: string.h:224
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:176
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:148
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:120
static bool spa_strendswith(const char *s, const char *suffix)
Definition: string.h:98
static bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition: string.h:71
static int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt,...)
Definition: string.h:398
static float spa_strtof(const char *str, char **endptr)
Convert str to a float in the C locale.
Definition: string.h:281
static void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
Definition: string.h:390
static int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
Definition: string.h:238
static bool spa_streq(const char *s1, const char *s2)
Definition: string.h:61
static bool spa_atof(const char *str, float *val)
Convert str to a float and store the result in val.
Definition: string.h:307
static int spa_scnprintf(char *buffer, size_t size, const char *format,...)
Definition: string.h:261
static char * spa_dtoa(char *str, size_t size, double val)
Definition: string.h:374
#define SPA_MIN(a, b)
Definition: defs.h:167
#define spa_assert_se(expr)
Definition: defs.h:393
#define SPA_LIKELY(x)
Definition: defs.h:361
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition: defs.h:289
#define SPA_UNLIKELY(x)
Definition: defs.h:363
size_t pos
Definition: string.h:387
size_t maxsize
Definition: string.h:386
char * buffer
Definition: string.h:385