/** * \return true if the two strings are equal, false otherwise * * If both \a a and \a b are NULL, the two are considered equal. *
*/ staticinlinebool spa_streq(constchar *s1, constchar *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.
*/ staticinlinebool spa_strneq(constchar *s1, constchar *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.
*/ staticinlinebool spa_strstartswith(constchar *s, constchar *prefix)
{ if (SPA_UNLIKELY(s == NULL)) returnfalse;
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.
*/ staticinlinebool spa_strendswith(constchar *s, constchar *suffix)
{
size_t l1, l2;
/** * 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
*/ staticinlinebool spa_atoi32(constchar *str, int32_t *val, int base)
{ char *endptr; long v;
if (!str || *str =='\0') returnfalse;
errno = 0;
v = strtol(str, &endptr, base); if (errno != 0 || *endptr != '\0') returnfalse;
if (v != (int32_t)v) returnfalse;
*val = v; returntrue;
}
/** * 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
*/ staticinlinebool spa_atou32(constchar *str, uint32_t *val, int base)
{ char *endptr; unsignedlonglong v;
if (!str || *str =='\0') returnfalse;
errno = 0;
v = strtoull(str, &endptr, base); if (errno != 0 || *endptr != '\0') returnfalse;
if (v != (uint32_t)v) returnfalse;
*val = v; returntrue;
}
/** * 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
*/ staticinlinebool spa_atoi64(constchar *str, int64_t *val, int base)
{ char *endptr; longlong v;
if (!str || *str =='\0') returnfalse;
errno = 0;
v = strtoll(str, &endptr, base); if (errno != 0 || *endptr != '\0') returnfalse;
*val = v; returntrue;
}
/** * 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
*/ staticinlinebool spa_atou64(constchar *str, uint64_t *val, int base)
{ char *endptr; unsignedlonglong v;
if (!str || *str =='\0') returnfalse;
errno = 0;
v = strtoull(str, &endptr, base); if (errno != 0 || *endptr != '\0') returnfalse;
*val = v; returntrue;
}
/** * 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
*/ staticinlinebool spa_atob(constchar *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) staticinlineint spa_vscnprintf(char *buffer, size_t size, constchar *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) staticinlineint spa_scnprintf(char *buffer, size_t size, constchar *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.
*/ staticinlinefloat spa_strtof(constchar *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
*/ staticinlinebool spa_atof(constchar *str, float *val)
{ char *endptr; float v;
if (!str || *str =='\0') returnfalse;
errno = 0;
v = spa_strtof(str, &endptr); if (errno != 0 || *endptr != '\0') returnfalse;
*val = v; returntrue;
}
/** * 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.
*/ staticinlinedouble spa_strtod(constchar *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
*/ staticinlinebool spa_atod(constchar *str, double *val)
{ char *endptr; double v;
if (!str || *str =='\0') returnfalse;
errno = 0;
v = spa_strtod(str, &endptr); if (errno != 0 || *endptr != '\0') returnfalse;
*val = v; returntrue;
}
staticinlinechar *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 */
Messung V0.5
¤ Dauer der Verarbeitung: 0.42 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.