PipeWire 1.7.0
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#include <stdarg.h>
9#include <stdbool.h>
10#include <errno.h>
11#include <stdlib.h>
12#include <locale.h>
13#include <sys/types.h>
14
15#include <spa/utils/defs.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#ifndef SPA_API_STRING
22 #ifdef SPA_API_IMPL
23 #define SPA_API_STRING SPA_API_IMPL
24 #else
25 #define SPA_API_STRING static inline
26 #endif
27#endif
28
33
38
45SPA_API_STRING bool spa_streq(const char *s1, const char *s2)
46{
47 return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
48}
49
52 *
53 * If both \a a and \a b are NULL, the two are considered equal.
54 */
55SPA_API_STRING bool spa_strneq(const char *s1, const char *s2, size_t len)
56{
57 return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
58}
59
60
62 * \return true if \a s starts with the \a prefix or false otherwise.
63 * A \a s is NULL, it never starts with the given \a prefix. A \a prefix of
64 * NULL is a bug in the caller.
65 */
66SPA_API_STRING bool spa_strstartswith(const char *s, const char *prefix)
67{
68 if (SPA_UNLIKELY(s == NULL))
69 return false;
70
71 spa_assert_se(prefix);
72
73 return strncmp(s, prefix, strlen(prefix)) == 0;
74}
75
76
82SPA_API_STRING bool spa_strendswith(const char *s, const char *suffix)
83{
84 size_t l1, l2;
85
86 if (SPA_UNLIKELY(s == NULL))
87 return false;
88
90
91 l1 = strlen(s);
92 l2 = strlen(suffix);
93 return l1 >= l2 && spa_streq(s + l1 - l2, suffix);
94}
95
102
104SPA_API_STRING bool spa_atoi32(const char *str, int32_t *val, int base)
105{
106 char *endptr;
107 long v;
108
109 if (!str || *str =='\0')
110 return false;
112 errno = 0;
113 v = strtol(str, &endptr, base);
114 if (errno != 0 || *endptr != '\0')
115 return false;
116
117 if (v != (int32_t)v)
118 return false;
119
120 *val = v;
121 return true;
122}
123
130
132SPA_API_STRING bool spa_atou32(const char *str, uint32_t *val, int base)
133{
134 char *endptr;
135 unsigned long long v;
136
137 if (!str || *str =='\0')
138 return false;
140 errno = 0;
141 v = strtoull(str, &endptr, base);
142 if (errno != 0 || *endptr != '\0')
143 return false;
144
145 if (v != (uint32_t)v)
146 return false;
147
148 *val = v;
149 return true;
150}
151
158
160SPA_API_STRING bool spa_atoi64(const char *str, int64_t *val, int base)
161{
162 char *endptr;
163 long long v;
164
165 if (!str || *str =='\0')
166 return false;
168 errno = 0;
169 v = strtoll(str, &endptr, base);
170 if (errno != 0 || *endptr != '\0')
171 return false;
172
173 *val = v;
174 return true;
175}
176
183
185SPA_API_STRING bool spa_atou64(const char *str, uint64_t *val, int base)
186{
187 char *endptr;
188 unsigned long long v;
189
190 if (!str || *str =='\0')
191 return false;
193 errno = 0;
194 v = strtoull(str, &endptr, base);
195 if (errno != 0 || *endptr != '\0')
196 return false;
197
198 *val = v;
199 return true;
200}
201
208SPA_API_STRING bool spa_atob(const char *str)
209{
210 return spa_streq(str, "true") || spa_streq(str, "1");
211}
212
215 * returned value is clipped to `size - 1` and a negative or zero size
216 * will abort() the program.
217 *
218 * \return The number of bytes printed, capped to `size-1`, or a negative
219 * number on error.
220 */
221SPA_PRINTF_FUNC(3, 0)
222SPA_API_STRING int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
223{
224 int r;
225
226 spa_assert_se((ssize_t)size > 0);
227
228 r = vsnprintf(buffer, size, format, args);
229 if (SPA_UNLIKELY(r < 0))
230 buffer[0] = '\0';
231 if (SPA_LIKELY(r < (ssize_t)size))
232 return r;
233 return size - 1;
234}
235
242
244SPA_PRINTF_FUNC(3, 4)
245SPA_API_STRING int spa_scnprintf(char *buffer, size_t size, const char *format, ...)
246{
247 int r;
248 va_list args;
249
250 va_start(args, format);
251 r = spa_vscnprintf(buffer, size, format, args);
253
254 return r;
255}
256
263
265SPA_API_STRING float spa_strtof(const char *str, char **endptr)
266{
267#ifndef __LOCALE_C_ONLY
268 static locale_t locale = NULL;
269
270 if (SPA_UNLIKELY(locale == NULL))
271 locale = newlocale(LC_ALL_MASK, "C", NULL);
272#ifdef __NetBSD__
273 return strtof_l(str, endptr, locale);
274#else
275 locale_t prev = uselocale(locale);
276 float v = strtof(str, endptr);
277 uselocale(prev);
278 return v;
279#endif
280#else
281 return strtof(str, endptr);
282#endif
283}
284
291
292SPA_API_STRING bool spa_atof(const char *str, float *val)
293{
294 char *endptr;
295 float v;
296
297 if (!str || *str =='\0')
298 return false;
299 errno = 0;
300 v = spa_strtof(str, &endptr);
301 if (errno != 0 || *endptr != '\0')
302 return false;
303
304 *val = v;
305 return true;
306}
307
314
316SPA_API_STRING double spa_strtod(const char *str, char **endptr)
317{
318#ifndef __LOCALE_C_ONLY
319 static locale_t locale = NULL;
320
321 if (SPA_UNLIKELY(locale == NULL))
322 locale = newlocale(LC_ALL_MASK, "C", NULL);
323#ifdef __NetBSD__
324 return strtod_l(str, endptr, locale);
325#else
326 locale_t prev = uselocale(locale);
327 double v = strtod(str, endptr);
328 uselocale(prev);
329 return v;
330#endif
331#else
332 return strtod(str, endptr);
333#endif
334}
335
342
343SPA_API_STRING bool spa_atod(const char *str, double *val)
344{
345 char *endptr;
346 double v;
347
348 if (!str || *str =='\0')
349 return false;
351 errno = 0;
352 v = spa_strtod(str, &endptr);
353 if (errno != 0 || *endptr != '\0')
354 return false;
355
356 *val = v;
357 return true;
358}
359
360SPA_API_STRING char *spa_dtoa(char *str, size_t size, double val)
361{
362 int i, l;
363 l = spa_scnprintf(str, size, "%f", val);
364 for (i = 0; i < l; i++)
365 if (str[i] == ',')
366 str[i] = '.';
367 return str;
368}
369
370struct spa_strbuf {
371 char *buffer;
372 size_t maxsize;
373 size_t pos;
374};
375
376SPA_API_STRING void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
380 buf->pos = 0;
381 if (maxsize > 0)
382 buf->buffer[0] = '\0';
384
385SPA_PRINTF_FUNC(2, 0)
386SPA_API_STRING int spa_strbuf_appendv(struct spa_strbuf *buf, const char *fmt, va_list args)
387{
388 size_t remain = buf->maxsize - buf->pos;
389 int written = vsnprintf(&buf->buffer[buf->pos], remain, fmt, args);
390 if (written > 0)
391 buf->pos += SPA_MIN(remain, (size_t)written);
392 return written;
394
395SPA_PRINTF_FUNC(2, 3)
396SPA_API_STRING int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt, ...)
397{
398 va_list args;
399 int written;
400
401 va_start(args, fmt);
402 written = spa_strbuf_appendv(buf, fmt, args);
404
405 return written;
406}
407
411
412#ifdef __cplusplus
413} /* extern "C" */
414#endif
415
416#endif /* SPA_UTILS_STRING_H */
spa/utils/defs.h
uint32_t int int const char va_list args
Definition core.h:434
va_end(args)
vsnprintf(buffer, sizeof(buffer), message, args)
uint32_t int int const char int r
Definition core.h:447
va_start(args, message)
const char const char * fmt
Definition json-builder.h:308
bool const char const char char * val
Definition json-builder.h:377
SPA_API_STRING char * spa_dtoa(char *str, size_t size, double val)
Definition string.h:367
SPA_API_STRING int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt,...)
Definition string.h:403
SPA_API_STRING 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:192
SPA_API_STRING bool spa_atof(const char *str, float *val)
Convert str to a float and store the result in val.
Definition string.h:299
SPA_API_STRING void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
Definition string.h:383
SPA_API_STRING bool spa_strendswith(const char *s, const char *suffix)
Definition string.h:89
SPA_API_STRING int spa_strbuf_appendv(struct spa_strbuf *buf, const char *fmt, va_list args)
Definition string.h:393
SPA_API_STRING bool spa_strstartswith(const char *s, const char *prefix)
Definition string.h:73
SPA_API_STRING bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition string.h:62
SPA_API_STRING bool spa_atob(const char *str)
Convert str to a boolean.
Definition string.h:215
SPA_API_STRING 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:167
SPA_API_STRING 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:111
SPA_API_STRING int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
Definition string.h:229
SPA_API_STRING double spa_strtod(const char *str, char **endptr)
Convert str to a double in the C locale.
Definition string.h:323
SPA_API_STRING float spa_strtof(const char *str, char **endptr)
Convert str to a float in the C locale.
Definition string.h:272
SPA_API_STRING int spa_scnprintf(char *buffer, size_t size, const char *format,...)
Definition string.h:252
SPA_API_STRING bool spa_atod(const char *str, double *val)
Convert str to a double and store the result in val.
Definition string.h:350
SPA_API_STRING 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:139
SPA_API_STRING bool spa_streq(const char *s1, const char *s2)
Definition string.h:52
#define SPA_MIN(a, b)
Definition defs.h:166
#define spa_assert_se(expr)
Definition defs.h:527
#define SPA_LIKELY(x)
Definition defs.h:441
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition defs.h:298
#define SPA_UNLIKELY(x)
Definition defs.h:443
#define SPA_API_STRING
Definition string.h:32
Definition string.h:377
size_t pos
Definition string.h:380
size_t maxsize
Definition string.h:379
char * buffer
Definition string.h:378