PipeWire 1.4.10

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