PipeWire 1.2.5
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2021 Red Hat, Inc. */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_UTILS_STRING_H
6#define SPA_UTILS_STRING_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <stdarg.h>
13#include <stdbool.h>
14#include <errno.h>
15#include <stdlib.h>
16#include <locale.h>
17
18#include <spa/utils/defs.h>
19
36static inline bool spa_streq(const char *s1, const char *s2)
37{
38 return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
39}
40
46static inline bool spa_strneq(const char *s1, const char *s2, size_t len)
47{
48 return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
49}
50
57static inline bool spa_strstartswith(const char *s, const char *prefix)
58{
59 if (SPA_UNLIKELY(s == NULL))
60 return false;
61
63
64 return strncmp(s, prefix, strlen(prefix)) == 0;
65}
66
67
73static inline bool spa_strendswith(const char *s, const char *suffix)
74{
75 size_t l1, l2;
76
77 if (SPA_UNLIKELY(s == NULL))
78 return false;
79
80 spa_assert_se(suffix);
81
82 l1 = strlen(s);
83 l2 = strlen(suffix);
84 return l1 >= l2 && spa_streq(s + l1 - l2, suffix);
85}
86
95static inline bool spa_atoi32(const char *str, int32_t *val, int base)
96{
97 char *endptr;
98 long v;
99
100 if (!str || *str =='\0')
101 return false;
102
103 errno = 0;
104 v = strtol(str, &endptr, base);
105 if (errno != 0 || *endptr != '\0')
106 return false;
107
108 if (v != (int32_t)v)
109 return false;
110
111 *val = v;
112 return true;
113}
114
123static inline bool spa_atou32(const char *str, uint32_t *val, int base)
124{
125 char *endptr;
126 unsigned long long v;
127
128 if (!str || *str =='\0')
129 return false;
130
131 errno = 0;
132 v = strtoull(str, &endptr, base);
133 if (errno != 0 || *endptr != '\0')
134 return false;
135
136 if (v != (uint32_t)v)
137 return false;
138
139 *val = v;
140 return true;
141}
142
151static inline bool spa_atoi64(const char *str, int64_t *val, int base)
152{
153 char *endptr;
154 long long v;
155
156 if (!str || *str =='\0')
157 return false;
158
159 errno = 0;
160 v = strtoll(str, &endptr, base);
161 if (errno != 0 || *endptr != '\0')
162 return false;
163
164 *val = v;
165 return true;
166}
167
176static inline bool spa_atou64(const char *str, uint64_t *val, int base)
177{
178 char *endptr;
179 unsigned long long v;
180
181 if (!str || *str =='\0')
182 return false;
183
184 errno = 0;
185 v = strtoull(str, &endptr, base);
186 if (errno != 0 || *endptr != '\0')
187 return false;
188
189 *val = v;
190 return true;
191}
192
199static inline bool spa_atob(const char *str)
200{
201 return spa_streq(str, "true") || spa_streq(str, "1");
202}
203
212SPA_PRINTF_FUNC(3, 0)
213static inline int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
214{
215 int r;
216
217 spa_assert_se((ssize_t)size > 0);
219 r = vsnprintf(buffer, size, format, args);
220 if (SPA_UNLIKELY(r < 0))
221 buffer[0] = '\0';
222 if (SPA_LIKELY(r < (ssize_t)size))
223 return r;
224 return size - 1;
225}
226
235SPA_PRINTF_FUNC(3, 4)
236static inline int spa_scnprintf(char *buffer, size_t size, const char *format, ...)
237{
238 int r;
239 va_list args;
240
241 va_start(args, format);
242 r = spa_vscnprintf(buffer, size, format, args);
243 va_end(args);
244
245 return r;
246}
247
256static inline float spa_strtof(const char *str, char **endptr)
257{
258#ifndef __LOCALE_C_ONLY
259 static locale_t locale = NULL;
260 locale_t prev;
261#endif
262 float v;
263#ifndef __LOCALE_C_ONLY
264 if (SPA_UNLIKELY(locale == NULL))
265 locale = newlocale(LC_ALL_MASK, "C", NULL);
266 prev = uselocale(locale);
267#endif
268 v = strtof(str, endptr);
269#ifndef __LOCALE_C_ONLY
270 uselocale(prev);
271#endif
272 return v;
273}
274
282static inline bool spa_atof(const char *str, float *val)
283{
284 char *endptr;
285 float v;
286
287 if (!str || *str =='\0')
288 return false;
289 errno = 0;
290 v = spa_strtof(str, &endptr);
291 if (errno != 0 || *endptr != '\0')
292 return false;
293
294 *val = v;
295 return true;
296}
297
306static inline double spa_strtod(const char *str, char **endptr)
307{
308#ifndef __LOCALE_C_ONLY
309 static locale_t locale = NULL;
310 locale_t prev;
311#endif
312 double v;
313#ifndef __LOCALE_C_ONLY
314 if (SPA_UNLIKELY(locale == NULL))
315 locale = newlocale(LC_ALL_MASK, "C", NULL);
316 prev = uselocale(locale);
317#endif
318 v = strtod(str, endptr);
319#ifndef __LOCALE_C_ONLY
320 uselocale(prev);
321#endif
322 return v;
323}
324
332static inline bool spa_atod(const char *str, double *val)
333{
334 char *endptr;
335 double v;
336
337 if (!str || *str =='\0')
338 return false;
339
340 errno = 0;
341 v = spa_strtod(str, &endptr);
342 if (errno != 0 || *endptr != '\0')
343 return false;
344
345 *val = v;
346 return true;
347}
348
349static inline char *spa_dtoa(char *str, size_t size, double val)
350{
351 int i, l;
352 l = spa_scnprintf(str, size, "%f", val);
353 for (i = 0; i < l; i++)
354 if (str[i] == ',')
355 str[i] = '.';
356 return str;
357}
358
359struct spa_strbuf {
360 char *buffer;
361 size_t maxsize;
362 size_t pos;
363};
365static inline void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
367 buf->buffer = buffer;
368 buf->maxsize = maxsize;
369 buf->pos = 0;
370 if (maxsize > 0)
371 buf->buffer[0] = '\0';
372}
373
374SPA_PRINTF_FUNC(2, 3)
375static inline int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt, ...)
376{
377 size_t remain = buf->maxsize - buf->pos;
378 ssize_t written;
379 va_list args;
380 va_start(args, fmt);
381 written = vsnprintf(&buf->buffer[buf->pos], remain, fmt, args);
382 va_end(args);
383 if (written > 0)
384 buf->pos += SPA_MIN(remain, (size_t)written);
385 return written;
386}
387
392#ifdef __cplusplus
393} /* extern "C" */
394#endif
395
396#endif /* SPA_UTILS_STRING_H */
spa/utils/defs.h
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:380
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:165
#define spa_assert_se(expr)
Definition defs.h:443
#define SPA_LIKELY(x)
Definition defs.h:367
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition defs.h:295
#define SPA_UNLIKELY(x)
Definition defs.h:369
Definition string.h:364
size_t pos
Definition string.h:367
size_t maxsize
Definition string.h:366
char * buffer
Definition string.h:365