PipeWire 0.3.49
string.h
Go to the documentation of this file.
1/* Simple Plugin API
2 *
3 * Copyright © 2021 Red Hat, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef SPA_UTILS_STRING_H
26#define SPA_UTILS_STRING_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#include <stdarg.h>
33#include <stdbool.h>
34#include <errno.h>
35#include <stdlib.h>
36#include <locale.h>
37
38#include <spa/utils/defs.h>
39
56static inline bool spa_streq(const char *s1, const char *s2)
57{
58 return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
59}
60
66static inline bool spa_strneq(const char *s1, const char *s2, size_t len)
67{
68 return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
69}
70
77static inline bool spa_strstartswith(const char *s, const char *prefix)
78{
79 if (SPA_UNLIKELY(s == NULL))
80 return false;
81
83
84 return strncmp(s, prefix, strlen(prefix)) == 0;
85}
86
87
93static inline bool spa_strendswith(const char *s, const char *suffix)
94{
95 size_t l1, l2;
96
97 if (SPA_UNLIKELY(s == NULL))
98 return false;
99
100 spa_assert_se(suffix);
101
102 l1 = strlen(s);
103 l2 = strlen(suffix);
104 return l1 >= l2 && spa_streq(s + l1 - l2, suffix);
105}
106
115static inline bool spa_atoi32(const char *str, int32_t *val, int base)
116{
117 char *endptr;
118 long v;
119
120 if (!str || *str =='\0')
121 return false;
122
123 errno = 0;
124 v = strtol(str, &endptr, base);
125 if (errno != 0 || *endptr != '\0')
126 return false;
127
128 if (v != (int32_t)v)
129 return false;
130
131 *val = v;
132 return true;
133}
134
143static inline bool spa_atou32(const char *str, uint32_t *val, int base)
144{
145 char *endptr;
146 unsigned long long v;
147
148 if (!str || *str =='\0')
149 return false;
150
151 errno = 0;
152 v = strtoull(str, &endptr, base);
153 if (errno != 0 || *endptr != '\0')
154 return false;
155
156 if (v != (uint32_t)v)
157 return false;
158
159 *val = v;
160 return true;
161}
162
171static inline bool spa_atoi64(const char *str, int64_t *val, int base)
172{
173 char *endptr;
174 long long v;
175
176 if (!str || *str =='\0')
177 return false;
178
179 errno = 0;
180 v = strtoll(str, &endptr, base);
181 if (errno != 0 || *endptr != '\0')
182 return false;
183
184 *val = v;
185 return true;
186}
187
196static inline bool spa_atou64(const char *str, uint64_t *val, int base)
197{
198 char *endptr;
199 unsigned long long v;
200
201 if (!str || *str =='\0')
202 return false;
203
204 errno = 0;
205 v = strtoull(str, &endptr, base);
206 if (errno != 0 || *endptr != '\0')
207 return false;
208
209 *val = v;
210 return true;
211}
212
219static inline bool spa_atob(const char *str)
220{
221 return spa_streq(str, "true") || spa_streq(str, "1");
222}
223
232SPA_PRINTF_FUNC(3, 0)
233static inline int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
234{
235 int r;
236
237 spa_assert_se((ssize_t)size > 0);
239 r = vsnprintf(buffer, size, format, args);
240 if (SPA_UNLIKELY(r < 0))
241 buffer[0] = '\0';
242 if (SPA_LIKELY(r < (ssize_t)size))
243 return r;
244 return size - 1;
245}
246
255SPA_PRINTF_FUNC(3, 4)
256static inline int spa_scnprintf(char *buffer, size_t size, const char *format, ...)
257{
258 int r;
259 va_list args;
260
261 va_start(args, format);
262 r = spa_vscnprintf(buffer, size, format, args);
263 va_end(args);
264
265 return r;
266}
267
276static inline float spa_strtof(const char *str, char **endptr)
277{
278 static locale_t locale = NULL;
279 float v;
280 if (SPA_UNLIKELY(locale == NULL))
281 locale = newlocale(LC_ALL_MASK, "C", NULL);
282 if (locale != NULL)
283 v = strtof_l(str, endptr, locale);
284 else
285 v = strtof(str, endptr);
286 return v;
287}
288
296static inline bool spa_atof(const char *str, float *val)
297{
298 char *endptr;
299 float v;
300
301 if (!str || *str =='\0')
302 return false;
303 errno = 0;
304 v = spa_strtof(str, &endptr);
305 if (errno != 0 || *endptr != '\0')
306 return false;
307
308 *val = v;
309 return true;
310}
311
320static inline double spa_strtod(const char *str, char **endptr)
321{
322 static locale_t locale = NULL;
323 double v;
324 if (SPA_UNLIKELY(locale == NULL))
325 locale = newlocale(LC_ALL_MASK, "C", NULL);
326 if (locale != NULL)
327 v = strtod_l(str, endptr, locale);
328 else
329 v = strtod(str, endptr);
330 return v;
331}
332
340static inline 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
357static inline 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
371#ifdef __cplusplus
372} /* extern "C" */
373#endif
374
375#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:345
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:325
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 float spa_strtof(const char *str, char **endptr)
Convert str to a float in the C locale.
Definition: string.h:281
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:301
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:362
#define spa_assert_se(expr)
Definition: defs.h:345
#define SPA_LIKELY(x)
Definition: defs.h:313
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition: defs.h:260
#define SPA_UNLIKELY(x)
Definition: defs.h:315