strfuncs.h revision 05c1d362cc69d18840d9c37b563f01ba25fc2634
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#define MAX_INT_STRLEN ((sizeof(uintmax_t) * CHAR_BIT + 2) / 3 + 1)
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainenextern const unsigned char uchar_nul; /* (const unsigned char *)"" */
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainenextern const unsigned char *uchar_empty_ptr; /* non-NULL pointer that shouldn't be dereferenced. */
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen/* Returns -1 if dest wasn't large enough, 0 if not. */
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainenint i_snprintf(char *dest, size_t max_chars, const char *format, ...)
923eb3dde28e4d8841c14fd6b4a69635b7070c3eTimo Sirainenchar *p_strdup(pool_t pool, const char *str) ATTR_MALLOC;
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenbergervoid *p_memdup(pool_t pool, const void *data, size_t size) ATTR_MALLOC;
06ff2a72c39cb34cc6425f17fc82c5e93fef2018Timo Sirainen/* return NULL if str = "" */
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainenchar *p_strdup_empty(pool_t pool, const char *str) ATTR_MALLOC;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* *end isn't included */
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainenchar *p_strdup_until(pool_t pool, const void *start, const void *end)
de76b960297406115cf6bae473f004c08174b16aTimo Sirainenchar *p_strndup(pool_t pool, const void *str, size_t max_chars) ATTR_MALLOC;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainenchar *p_strdup_printf(pool_t pool, const char *format, ...)
3ddbbe03fe74b3ee7b1dff4e08ec706d7880d052Timo Sirainen ATTR_FORMAT(2, 3) ATTR_MALLOC ATTR_RETURNS_NONNULL;
c519de264df14a9d525e2604671c332590ce54e3Timo Sirainenchar *p_strdup_vprintf(pool_t pool, const char *format, va_list args)
61530b48694398df42744204e35535dbe3f745c4Timo Sirainen ATTR_FORMAT(2, 0) ATTR_MALLOC ATTR_RETURNS_NONNULL;
61530b48694398df42744204e35535dbe3f745c4Timo Sirainenchar *p_strconcat(pool_t pool, const char *str1, ...)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen/* same with temporary memory allocations: */
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainenconst char *t_strdup(const char *str) ATTR_MALLOC;
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainenchar *t_strdup_noconst(const char *str) ATTR_MALLOC;
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen/* return NULL if str = "" */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenconst char *t_strdup_empty(const char *str) ATTR_MALLOC;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen/* *end isn't included */
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainenconst char *t_strdup_until(const void *start, const void *end)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenconst char *t_strndup(const void *str, size_t max_chars) ATTR_MALLOC;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenconst char *t_strdup_printf(const char *format, ...)
2b3b0df76184799317584b596af8df5afec3ebddTimo Sirainen ATTR_FORMAT(1, 2) ATTR_MALLOC ATTR_RETURNS_NONNULL;
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainenconst char *t_strdup_vprintf(const char *format, va_list args)
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen ATTR_FORMAT(1, 0) ATTR_MALLOC ATTR_RETURNS_NONNULL;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen/* Like t_strdup(), but stop at cutchar. */
d244c6cadd5f077f5d0f1e00c3652d0108a2d908Timo Sirainenconst char *t_strcut(const char *str, char cutchar);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen/* Replace all from->to chars in the string. */
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainenconst char *t_str_replace(const char *str, char from, char to);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen/* Like strlcpy(), but return -1 if buffer was overflown, 0 if not. */
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainenint i_strocpy(char *dest, const char *src, size_t dstsize);
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainen/* Trim matching chars from either side of the string */
baf1148108b7d9739626b47cc57298c36929586aTimo Sirainenconst char *t_str_trim(const char *str, const char *chars);
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenconst char *p_str_trim(pool_t pool, const char *str, const char *chars);
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenconst char *str_ltrim(const char *str, const char *chars);
baf1148108b7d9739626b47cc57298c36929586aTimo Sirainenconst char *t_str_ltrim(const char *str, const char *chars);
baf1148108b7d9739626b47cc57298c36929586aTimo Sirainenconst char *p_str_ltrim(pool_t pool, const char *str, const char *chars);
e82e363e7a6917f470412d629db6c5b1f5891a35Timo Sirainenconst char *t_str_rtrim(const char *str, const char *chars);
e82e363e7a6917f470412d629db6c5b1f5891a35Timo Sirainenconst char *p_str_rtrim(pool_t pool, const char *str, const char *chars);
e82e363e7a6917f470412d629db6c5b1f5891a35Timo Sirainenint null_strcmp(const char *s1, const char *s2) ATTR_PURE;
e82e363e7a6917f470412d629db6c5b1f5891a35Timo Sirainenint null_strcasecmp(const char *s1, const char *s2) ATTR_PURE;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenint i_memcasecmp(const void *p1, const void *p2, size_t size) ATTR_PURE;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenint i_strcmp_p(const char *const *p1, const char *const *p2) ATTR_PURE;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenint i_strcasecmp_p(const char *const *p1, const char *const *p2) ATTR_PURE;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenstatic inline char *i_strchr_to_next(const char *str, char chr)
e82e363e7a6917f470412d629db6c5b1f5891a35Timo Sirainen/* separators is an array of separator characters, not a separator string.
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen an empty data string results in an array containing only NULL. */
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenchar **p_strsplit(pool_t pool, const char *data, const char *separators)
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenconst char **t_strsplit(const char *data, const char *separators)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen/* like p_strsplit(), but treats multiple adjacent separators as a single
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen separator. */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenchar **p_strsplit_spaces(pool_t pool, const char *data, const char *separators)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenconst char **t_strsplit_spaces(const char *data, const char *separators)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen/* Return length of NULL-terminated list string array */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenunsigned int str_array_length(const char *const *arr) ATTR_PURE;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen/* Return all strings from array joined into one string. */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenconst char *t_strarray_join(const char *const *arr, const char *separator)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen/* Removes a value from NULL-terminated string array. Returns TRUE if found. */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenbool str_array_remove(const char **arr, const char *value);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen/* Returns TRUE if value exists in NULL-terminated string array. */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenbool str_array_find(const char *const *arr, const char *value);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen/* Like str_array_find(), but use strcasecmp(). */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenbool str_array_icase_find(const char *const *arr, const char *value);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen/* Duplicate array of strings. The memory can be freed by freeing the
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen return value. */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenconst char **p_strarray_dup(pool_t pool, const char *const *arr)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen/* Join ARRAY_TYPE(const_string) to a string, similar to t_strarray_join() */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenchar *p_array_const_string_join(pool_t pool, const ARRAY_TYPE(const_string) *arr,
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen#define t_array_const_string_join(arr, separator) \
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen ((const char *)p_array_const_string_join(unsafe_data_stack_pool, arr, separator))
e82e363e7a6917f470412d629db6c5b1f5891a35Timo Sirainen/* INTERNAL */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenchar *t_noalloc_strdup_vprintf(const char *format, va_list args,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen unsigned int *size_r)
e82e363e7a6917f470412d629db6c5b1f5891a35Timo Sirainenchar *vstrconcat(const char *str1, va_list args, size_t *ret_len) ATTR_MALLOC;