PipeWire: string.h Source File

PipeWire 0.3.37

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
36#include <spa/utils/defs.h>
37
50/* static */ inline bool spa_streq(const char *s1, const char *s2)
51{
52 return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
53}
54
60/* static */ inline bool spa_strneq(const char *s1, const char *s2, size_t len)
61{
62 return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
63}
64
65
71/* static */ inline bool spa_strstartswith(const char *s, const char *prefix)
72{
73 if (SPA_UNLIKELY(s == NULL))
74 return false;
75
76 spa_assert_se(prefix);
77
78 return strncmp(s, prefix, strlen(prefix)) == 0;
79}
80
81
87/* static */ inline bool spa_strendswith(const char *s, const char *suffix)
88{
89 size_t l1, l2;
90
91 if (SPA_UNLIKELY(s == NULL))
92 return false;
93
94 spa_assert_se(suffix);
95
96 l1 = strlen(s);
97 l2 = strlen(suffix);
98 return l1 >= l2 && spa_streq(s + l1 - l2, suffix);
99}
100
109/* static */ inline bool spa_atoi32(const char *str, int32_t *val, int base)
110{
111 char *endptr;
112 long v;
113
114 if (!str || *str =='\0')
115 return false;
116
117 errno = 0;
118 v = strtol(str, &endptr, base);
119 if (errno != 0 || *endptr != '\0')
120 return false;
121
122 if (v != (int32_t)v)
123 return false;
124
125 *val = v;
126 return true;
127}
128
137/* static */ inline bool spa_atou32(const char *str, uint32_t *val, int base)
138{
139 char *endptr;
140 unsigned long long v;
141
142 if (!str || *str =='\0')
143 return false;
144
145 errno = 0;
146 v = strtoull(str, &endptr, base);
147 if (errno != 0 || *endptr != '\0')
148 return false;
149
150 if (v != (uint32_t)v)
151 return false;
152
153 *val = v;
154 return true;
155}
156
165/* static */ inline bool spa_atoi64(const char *str, int64_t *val, int base)
166{
167 char *endptr;
168 long long v;
169
170 if (!str || *str =='\0')
171 return false;
172
173 errno = 0;
174 v = strtoll(str, &endptr, base);
175 if (errno != 0 || *endptr != '\0')
176 return false;
177
178 *val = v;
179 return true;
180}
181
190/* static */ inline bool spa_atou64(const char *str, uint64_t *val, int base)
191{
192 char *endptr;
193 unsigned long long v;
194
195 if (!str || *str =='\0')
196 return false;
197
198 errno = 0;
199 v = strtoull(str, &endptr, base);
200 if (errno != 0 || *endptr != '\0')
201 return false;
202
203 *val = v;
204 return true;
205}
206
213/* static */ inline bool spa_atob(const char *str)
214{
215 return spa_streq(str, "true") || spa_streq(str, "1");
216}
217
226 SPA_PRINTF_FUNC(3, 0)
227/* static */ inline int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
228{
229 int r;
230
231 spa_assert_se((ssize_t)size > 0);
232
233 r = vsnprintf(buffer, size, format, args);
234 if (SPA_UNLIKELY(r < 0))
235 buffer[0] = '\0';
236 if (SPA_LIKELY(r < (ssize_t)size))
237 return r;
238 return size - 1;
239}
240
249 SPA_PRINTF_FUNC(3, 4)
250/* static */ inline int spa_scnprintf(char *buffer, size_t size, const char *format, ...)
251{
252 int r;
253 va_list args;
254
255 va_start(args, format);
256 r = spa_vscnprintf(buffer, size, format, args);
257 va_end(args);
258
259 return r;
260}
261
269/* static */ inline bool spa_atof(const char *str, float *val)
270{
271 char *endptr;
272 float v;
273
274 if (!str || *str =='\0')
275 return false;
276
277 errno = 0;
278 v = strtof(str, &endptr);
279 if (errno != 0 || *endptr != '\0')
280 return false;
281
282 *val = v;
283 return true;
284}
285
293/* static */ inline bool spa_atod(const char *str, double *val)
294{
295 char *endptr;
296 double v;
297
298 if (!str || *str =='\0')
299 return false;
300
301 errno = 0;
302 v = strtod(str, &endptr);
303 if (errno != 0 || *endptr != '\0')
304 return false;
305
306 *val = v;
307 return true;
308}
309
314#ifdef __cplusplus
315} /* extern "C" */
316#endif
317
318#endif /* SPA_UTILS_STRING_H */
defs.h
spa_atoi32
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:109
spa_scnprintf
int spa_scnprintf(char *buffer, size_t size, const char *format,...)
Definition: string.h:250
spa_strneq
bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition: string.h:60
spa_atod
bool spa_atod(const char *str, double *val)
Convert str to a double and store the result in val.
Definition: string.h:293
spa_streq
bool spa_streq(const char *s1, const char *s2)
Definition: string.h:50
spa_atou32
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:137
spa_assert_se
#define spa_assert_se(expr)
Definition: defs.h:270
spa_vscnprintf
int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
Definition: string.h:227
spa_strstartswith
bool spa_strstartswith(const char *s, const char *prefix)
Definition: string.h:71
spa_atof
bool spa_atof(const char *str, float *val)
Convert str to a float and store the result in val.
Definition: string.h:269
spa_strendswith
bool spa_strendswith(const char *s, const char *suffix)
Definition: string.h:87
SPA_LIKELY
#define SPA_LIKELY(x)
Definition: defs.h:241
SPA_PRINTF_FUNC
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition: defs.h:211
spa_atob
bool spa_atob(const char *str)
Convert str to a boolean.
Definition: string.h:213
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition: defs.h:242
spa_atou64
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:190
spa_atoi64
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:165
buffer
Definition: filter.c:59