PipeWire: spa/utils/string.h Source File

PipeWire 1.6.8

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
45 SPA_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 */
55 SPA_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 */
66 SPA_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
82 SPA_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
89 spa_assert_se(suffix);
90
91 l1 = strlen(s);
92 l2 = strlen(suffix);
93 return l1 >= l2 && spa_streq(s + l1 - l2, suffix);
94}
95
102
104 SPA_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;
111
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
132 SPA_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;
139
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
160 SPA_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;
167
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
185 SPA_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;
192
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
208 SPA_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 */
221 SPA_PRINTF_FUNC(3, 0)
222 SPA_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
244 SPA_PRINTF_FUNC(3, 4)
245 SPA_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);
252 va_end(args);
253
254 return r;
255}
256
263
265 SPA_API_STRING float spa_strtof(const char *str, char **endptr)
266{
267#ifndef __LOCALE_C_ONLY
268 static locale_t locale = NULL;
269 locale_t prev;
270#endif
271 float v;
272#ifndef __LOCALE_C_ONLY
273 if (SPA_UNLIKELY(locale == NULL))
274 locale = newlocale(LC_ALL_MASK, "C", NULL);
275 prev = uselocale(locale);
276#endif
277 v = strtof(str, endptr);
278#ifndef __LOCALE_C_ONLY
279 uselocale(prev);
280#endif
281 return v;
282}
283
290
291 SPA_API_STRING bool spa_atof(const char *str, float *val)
292{
293 char *endptr;
294 float v;
295
296 if (!str || *str =='\0')
297 return false;
298 errno = 0;
299 v = spa_strtof(str, &endptr);
300 if (errno != 0 || *endptr != '\0')
301 return false;
302
303 *val = v;
304 return true;
305}
306
313
315 SPA_API_STRING double spa_strtod(const char *str, char **endptr)
316{
317#ifndef __LOCALE_C_ONLY
318 static locale_t locale = NULL;
319 locale_t prev;
320#endif
321 double v;
322#ifndef __LOCALE_C_ONLY
323 if (SPA_UNLIKELY(locale == NULL))
324 locale = newlocale(LC_ALL_MASK, "C", NULL);
325 prev = uselocale(locale);
326#endif
327 v = strtod(str, endptr);
328#ifndef __LOCALE_C_ONLY
329 uselocale(prev);
330#endif
331 return v;
332}
333
340
341 SPA_API_STRING bool spa_atod(const char *str, double *val)
342{
343 char *endptr;
344 double v;
345
346 if (!str || *str =='\0')
347 return false;
348
349 errno = 0;
350 v = spa_strtod(str, &endptr);
351 if (errno != 0 || *endptr != '\0')
352 return false;
353
354 *val = v;
355 return true;
356}
357
358 SPA_API_STRING char *spa_dtoa(char *str, size_t size, double val)
359{
360 int i, l;
361 l = spa_scnprintf(str, size, "%f", val);
362 for (i = 0; i < l; i++)
363 if (str[i] == ',')
364 str[i] = '.';
365 return str;
366}
367
368struct spa_strbuf {
369 char *buffer;
370 size_t maxsize;
371 size_t pos;
372};
373
374 SPA_API_STRING void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
375{
376 buf->buffer = buffer;
377 buf->maxsize = maxsize;
378 buf->pos = 0;
379 if (maxsize > 0)
380 buf->buffer[0] = '\0';
381}
382
383 SPA_PRINTF_FUNC(2, 3)
384 SPA_API_STRING int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt, ...)
385{
386 size_t remain = buf->maxsize - buf->pos;
387 ssize_t written;
388 va_list args;
389 va_start(args, fmt);
390 written = vsnprintf(&buf->buffer[buf->pos], remain, fmt, args);
391 va_end(args);
392 if (written > 0)
393 buf->pos += SPA_MIN(remain, (size_t)written);
394 return written;
395}
396
400
401#ifdef __cplusplus
402} /* extern "C" */
403#endif
404
405#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:365
spa_strbuf_append
SPA_API_STRING int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt,...)
Definition string.h:391
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:192
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:298
spa_strbuf_init
SPA_API_STRING void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
Definition string.h:381
spa_strendswith
SPA_API_STRING bool spa_strendswith(const char *s, const char *suffix)
Definition string.h:89
spa_strstartswith
SPA_API_STRING bool spa_strstartswith(const char *s, const char *prefix)
Definition string.h:73
spa_strneq
SPA_API_STRING bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition string.h:62
spa_atob
SPA_API_STRING bool spa_atob(const char *str)
Convert str to a boolean.
Definition string.h:215
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:167
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:111
spa_vscnprintf
SPA_API_STRING int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
Definition string.h:229
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:322
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:272
spa_scnprintf
SPA_API_STRING int spa_scnprintf(char *buffer, size_t size, const char *format,...)
Definition string.h:252
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:348
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:139
spa_streq
SPA_API_STRING bool spa_streq(const char *s1, const char *s2)
Definition string.h:52
SPA_MIN
#define SPA_MIN(a, b)
Definition defs.h:165
spa_assert_se
#define spa_assert_se(expr)
Definition defs.h:472
SPA_LIKELY
#define SPA_LIKELY(x)
Definition defs.h:396
SPA_PRINTF_FUNC
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition defs.h:297
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition defs.h:398
SPA_API_STRING
#define SPA_API_STRING
Definition string.h:32
spa_strbuf
Definition string.h:375
spa_strbuf::pos
size_t pos
Definition string.h:378
spa_strbuf::maxsize
size_t maxsize
Definition string.h:377
spa_strbuf::buffer
char * buffer
Definition string.h:376