1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/* Simple Plugin API
*
* Copyright © 2021 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef SPA_UTILS_STRING_H
#define SPA_UTILS_STRING_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <stdbool.h>
#include <errno.h>
#include <stdlib.h>
#include <locale.h>
#include <spa/utils/defs.h>
/**
* \defgroup spa_string String handling
* String handling utilities
*/
/**
* \addtogroup spa_string
* \{
*/
/**
* \return true if the two strings are equal, false otherwise
*
* If both \a a and \a b are NULL, the two are considered equal.
*
*/
static inline bool spa_streq(const char *s1, const char *s2)
{
return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
}
/**
* \return true if the two strings are equal, false otherwise
*
* If both \a a and \a b are NULL, the two are considered equal.
*/
static inline bool spa_strneq(const char *s1, const char *s2, size_t len)
{
return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
}
/**
* \return true if \a s starts with the \a prefix or false otherwise.
* A \a s is NULL, it never starts with the given \a prefix. A \a prefix of
* NULL is a bug in the caller.
*/
static inline bool spa_strstartswith(const char *s, const char *prefix)
{
if (SPA_UNLIKELY(s == NULL))
return false;
spa_assert_se(prefix);
return strncmp(s, prefix, strlen(prefix)) == 0;
}
/**
* \return true if \a s ends with the \a suffix or false otherwise.
* A \a s is NULL, it never ends with the given \a suffix. A \a suffix of
* NULL is a bug in the caller.
*/
static inline bool spa_strendswith(const char *s, const char *suffix)
{
size_t l1, l2;
if (SPA_UNLIKELY(s == NULL))
return false;
spa_assert_se(suffix);
l1 = strlen(s);
l2 = strlen(suffix);
return l1 >= l2 && spa_streq(s + l1 - l2, suffix);
}
/**
* Convert \a str to an int32_t with the given \a base and store the
* result in \a val.
*
* On failure, the value of \a val is unmodified.
*
* \return true on success, false otherwise
*/
static inline bool spa_atoi32(const char *str, int32_t *val, int base)
{
char *endptr;
long v;
if (!str || *str =='\0')
return false;
errno = 0;
v = strtol(str, &endptr, base);
if (errno != 0 || *endptr != '\0')
return false;
if (v != (int32_t)v)
return false;
*val = v;
return true;
}
/**
* Convert \a str to an uint32_t with the given \a base and store the
* result in \a val.
*
* On failure, the value of \a val is unmodified.
*
* \return true on success, false otherwise
*/
static inline bool spa_atou32(const char *str, uint32_t *val, int base)
{
char *endptr;
unsigned long long v;
if (!str || *str =='\0')
return false;
errno = 0;
v = strtoull(str, &endptr, base);
if (errno != 0 || *endptr != '\0')
return false;
if (v != (uint32_t)v)
return false;
*val = v;
return true;
}
/**
* Convert \a str to an int64_t with the given \a base and store the
* result in \a val.
*
* On failure, the value of \a val is unmodified.
*
* \return true on success, false otherwise
*/
static inline bool spa_atoi64(const char *str, int64_t *val, int base)
{
char *endptr;
long long v;
if (!str || *str =='\0')
return false;
errno = 0;
v = strtoll(str, &endptr, base);
if (errno != 0 || *endptr != '\0')
return false;
*val = v;
return true;
}
/**
* Convert \a str to an uint64_t with the given \a base and store the
* result in \a val.
*
* On failure, the value of \a val is unmodified.
*
* \return true on success, false otherwise
*/
static inline bool spa_atou64(const char *str, uint64_t *val, int base)
{
char *endptr;
unsigned long long v;
if (!str || *str =='\0')
return false;
errno = 0;
v = strtoull(str, &endptr, base);
if (errno != 0 || *endptr != '\0')
return false;
*val = v;
return true;
}
/**
* Convert \a str to a boolean. Allowed boolean values are "true" and a
* literal "1", anything else is false.
*
* \return true on success, false otherwise
*/
static inline bool spa_atob(const char *str)
{
return spa_streq(str, "true") || spa_streq(str, "1");
}
/**
* "Safe" version of vsnprintf. Exactly the same as vsnprintf but the
* returned value is clipped to `size - 1` and a negative or zero size
* will abort() the program.
*
* \return The number of bytes printed, capped to `size-1`, or a negative
* number on error.
*/
SPA_PRINTF_FUNC(3, 0)
static inline int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
{
int r;
spa_assert_se((ssize_t)size > 0);
r = vsnprintf(buffer, size, format, args);
if (SPA_UNLIKELY(r < 0))
buffer[0] = '\0';
if (SPA_LIKELY(r < (ssize_t)size))
return r;
return size - 1;
}
/**
* "Safe" version of snprintf. Exactly the same as snprintf but the
* returned value is clipped to `size - 1` and a negative or zero size
* will abort() the program.
*
* \return The number of bytes printed, capped to `size-1`, or a negative
* number on error.
*/
SPA_PRINTF_FUNC(3, 4)
static inline int spa_scnprintf(char *buffer, size_t size, const char *format, ...)
{
int r;
va_list args;
va_start(args, format);
r = spa_vscnprintf(buffer, size, format, args);
va_end(args);
return r;
}
/**
* Convert \a str to a float in the C locale.
*
* If \a endptr is not NULL, a pointer to the character after the last character
* used in the conversion is stored in the location referenced by endptr.
*
* \return the result float.
*/
static inline float spa_strtof(const char *str, char **endptr)
{
#ifndef __LOCALE_C_ONLY
static locale_t locale = NULL;
locale_t prev;
#endif
float v;
#ifndef __LOCALE_C_ONLY
if (SPA_UNLIKELY(locale == NULL))
locale = newlocale(LC_ALL_MASK, "C", NULL);
prev = uselocale(locale);
#endif
v = strtof(str, endptr);
#ifndef __LOCALE_C_ONLY
uselocale(prev);
#endif
return v;
}
/**
* Convert \a str to a float and store the result in \a val.
*
* On failure, the value of \a val is unmodified.
*
* \return true on success, false otherwise
*/
static inline bool spa_atof(const char *str, float *val)
{
char *endptr;
float v;
if (!str || *str =='\0')
return false;
errno = 0;
v = spa_strtof(str, &endptr);
if (errno != 0 || *endptr != '\0')
return false;
*val = v;
return true;
}
/**
* Convert \a str to a double in the C locale.
*
* If \a endptr is not NULL, a pointer to the character after the last character
* used in the conversion is stored in the location referenced by endptr.
*
* \return the result float.
*/
static inline double spa_strtod(const char *str, char **endptr)
{
#ifndef __LOCALE_C_ONLY
static locale_t locale = NULL;
locale_t prev;
#endif
double v;
#ifndef __LOCALE_C_ONLY
if (SPA_UNLIKELY(locale == NULL))
locale = newlocale(LC_ALL_MASK, "C", NULL);
prev = uselocale(locale);
#endif
v = strtod(str, endptr);
#ifndef __LOCALE_C_ONLY
uselocale(prev);
#endif
return v;
}
/**
* Convert \a str to a double and store the result in \a val.
*
* On failure, the value of \a val is unmodified.
*
* \return true on success, false otherwise
*/
static inline bool spa_atod(const char *str, double *val)
{
char *endptr;
double v;
if (!str || *str =='\0')
return false;
errno = 0;
v = spa_strtod(str, &endptr);
if (errno != 0 || *endptr != '\0')
return false;
*val = v;
return true;
}
static inline char *spa_dtoa(char *str, size_t size, double val)
{
int i, l;
l = spa_scnprintf(str, size, "%f", val);
for (i = 0; i < l; i++)
if (str[i] == ',')
str[i] = '.';
return str;
}
/**
* \}
*/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_UTILS_STRING_H */
|