[util] Rewrite a_strndup()

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-05-29 09:45:10 +02:00
parent 270c705447
commit 9e2ff3f893
2 changed files with 20 additions and 23 deletions

View File

@ -153,27 +153,6 @@ position_to_str(position_t p)
} }
} }
/** \brief safe limited strdup.
*
* Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into a newly
* allocated buffer, always adding a final \c \\0, and returns that buffer.
*
* \param[in] src source string.
* \param[in] l maximum number of chars to copy.
*
* \return a newly allocated buffer containing the first \c l chars of \c src.
*/
char* a_strndup(const char* src, ssize_t l)
{
char* _tmpStr = p_new(char, l + 1);
if (_tmpStr)
{
a_strncpy(_tmpStr, l + 1, src, l);
_tmpStr[l] = 0;
}
return _tmpStr;
}
fuzzy_t fuzzy_t
fuzzy_get_from_str(const char *str) fuzzy_get_from_str(const char *str)
{ {

View File

@ -169,12 +169,31 @@ static inline ssize_t a_strnlen(const char *s, ssize_t n)
* \param[in] s the string to duplicate. * \param[in] s the string to duplicate.
* \return a pointer to the duplicated string. * \return a pointer to the duplicated string.
*/ */
static inline char *a_strdup(const char *s) static inline
char *a_strdup(const char *s)
{ {
ssize_t len = a_strlen(s); ssize_t len = a_strlen(s);
return len ? p_dup(s, len + 1) : NULL; return len ? p_dup(s, len + 1) : NULL;
} }
/** \brief safe limited strdup.
*
* Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into a newly
* allocated buffer, always adding a final \c \\0, and returns that buffer.
*
* \warning when s is \c "" or l is 0, it returns NULL !
*
* \param[in] s source string.
* \param[in] l maximum number of chars to copy.
* \return a newly allocated buffer containing the first \c l chars of \c src.
*/
static inline
char * a_strndup(const char *s, ssize_t l)
{
ssize_t len = MIN(a_strlen(s), l);
return len ? p_dup(s, len + 1) : NULL;
}
/** \brief \c NULL resistant strcmp. /** \brief \c NULL resistant strcmp.
* \param[in] a the first string. * \param[in] a the first string.
* \param[in] b the second string. * \param[in] b the second string.
@ -211,7 +230,6 @@ static inline int a_strncmp(const char *a, const char *b, ssize_t n)
ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1))); ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1)));
ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1))); ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1)));
char* a_strndup(const char *src, ssize_t l) __attribute__((nonnull(1)));
/** \brief safe strcat. /** \brief safe strcat.
* *