+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 405 lines · 8.2 KB · SPDX: MIT
main
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
18 extern "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  
29 /**
30 * \defgroup spa_string String handling
31 * String handling utilities
32 */
33  
34 /**
35 * \addtogroup spa_string
36 * \{
37 */
38  
39 /**
40 * \return true if the two strings are equal, false otherwise
41 *
42 * If both \a a and \a b are NULL, the two are considered equal.
43 *
44 */
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  
50 /**
51 * \return true if the two strings are equal, false otherwise
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  
61 /**
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  
77 /**
78 * \return true if \a s ends with the \a suffix or false otherwise.
79 * A \a s is NULL, it never ends with the given \a suffix. A \a suffix of
80 * NULL is a bug in the caller.
81 */
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  
96 /**
97 * Convert \a str to an int32_t with the given \a base and store the
98 * result in \a val.
99 *
100 * On failure, the value of \a val is unmodified.
101 *
102 * \return true on success, false otherwise
103 */
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  
124 /**
125 * Convert \a str to an uint32_t with the given \a base and store the
126 * result in \a val.
127 *
128 * On failure, the value of \a val is unmodified.
129 *
130 * \return true on success, false otherwise
131 */
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  
152 /**
153 * Convert \a str to an int64_t with the given \a base and store the
154 * result in \a val.
155 *
156 * On failure, the value of \a val is unmodified.
157 *
158 * \return true on success, false otherwise
159 */
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  
177 /**
178 * Convert \a str to an uint64_t with the given \a base and store the
179 * result in \a val.
180 *
181 * On failure, the value of \a val is unmodified.
182 *
183 * \return true on success, false otherwise
184 */
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  
202 /**
203 * Convert \a str to a boolean. Allowed boolean values are "true" and a
204 * literal "1", anything else is false.
205 *
206 * \return true on success, false otherwise
207 */
208 SPA_API_STRING bool spa_atob(const char *str)
209 {
210 return spa_streq(str, "true") || spa_streq(str, "1");
211 }
212  
213 /**
214 * "Safe" version of vsnprintf. Exactly the same as vsnprintf but the
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  
236 /**
237 * "Safe" version of snprintf. Exactly the same as snprintf but the
238 * returned value is clipped to `size - 1` and a negative or zero size
239 * will abort() the program.
240 *
241 * \return The number of bytes printed, capped to `size-1`, or a negative
242 * number on error.
243 */
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  
257 /**
258 * Convert \a str to a float in the C locale.
259 *
260 * If \a endptr is not NULL, a pointer to the character after the last character
261 * used in the conversion is stored in the location referenced by endptr.
262 *
263 * \return the result float.
264 */
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  
284 /**
285 * Convert \a str to a float and store the result in \a val.
286 *
287 * On failure, the value of \a val is unmodified.
288 *
289 * \return true on success, false otherwise
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  
307 /**
308 * Convert \a str to a double in the C locale.
309 *
310 * If \a endptr is not NULL, a pointer to the character after the last character
311 * used in the conversion is stored in the location referenced by endptr.
312 *
313 * \return the result float.
314 */
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  
334 /**
335 * Convert \a str to a double and store the result in \a val.
336 *
337 * On failure, the value of \a val is unmodified.
338 *
339 * \return true on success, false otherwise
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  
368 struct 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  
397 /**
398 * \}
399 */
400  
401 #ifdef __cplusplus
402 } /* extern "C" */
403 #endif
404  
405 #endif /* SPA_UTILS_STRING_H */