X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Fstr.c;h=f421530d3428ba2b2b5c3e114c8b3e08e48ad1f4;hp=a5333e481fe7b1f7c62c8d75928ba1b4076ac310;hb=33d7570d66fa131d801880a7786c8303cd867aaf;hpb=93b012884de4ca5e1f2550f767d0b8680b9c0e9f diff --git a/lib-lib/str.c b/lib-lib/str.c index a5333e4..f421530 100644 --- a/lib-lib/str.c +++ b/lib-lib/str.c @@ -17,9 +17,17 @@ * Copyright © 2006 Pierre Habouzit */ -#include "macros.h" -#include "str.h" +/** \addtogroup mutt_strings */ +/*@{*/ +/** \file str.c + * \brief Madmutt string API module implementation. + * \author Pierre Habouzit + */ + +#include "lib-lib.h" + +#ifndef __doxygen_skip__ #define XX 255 unsigned char const __m_strdigits[128] = { XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, @@ -67,13 +75,25 @@ char const __m_b36chars_upper[36] = { 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; +#endif - +/** \brief safe strcpy. + * + * Copies at most n-1 characters from \c src into \c dst, always + * adding a final \c \\0 in \c dst. + * + * \param[in] dst destination buffer. + * \param[in] n size of the buffer. Negative sizes are allowed. + * \param[in] src source string. + * + * \return \c src \e length. If this value is \>= \c n then the copy was + * truncated. + */ ssize_t m_strcpy(char *dst, ssize_t n, const char *src) { ssize_t len = m_strlen(src); - if (dst && n > 0) { + if (n > 0) { ssize_t dlen = MIN(n - 1, len); memcpy(dst, src, dlen); dst[dlen] = '\0'; @@ -82,11 +102,23 @@ ssize_t m_strcpy(char *dst, ssize_t n, const char *src) return len; } +/** \brief safe limited strcpy. + * + * Copies at most min(n-1, \c l) characters from \c src into \c dst, + * always adding a final \c \\0 in \c dst. + * + * \param[in] dst destination buffer. + * \param[in] n size of the buffer. Negative sizes are allowed. + * \param[in] src source string. + * \param[in] l maximum number of chars to copy. + * + * \return minimum of \c src \e length and \c l. + */ ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) { ssize_t len = MIN(m_strlen(src), l); - if (dst && n > 0) { + if (n > 0) { ssize_t dlen = MIN(n - 1, len); memcpy(dst, src, dlen); dst[dlen] = '\0'; @@ -97,16 +129,12 @@ ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) char *m_strrtrim(char *s) { - if (s) { - char *p = s + m_strlen(s); + ssize_t len = m_strlen(s); - while (p > s && ISSPACE(p[-1])) { - *--p = '\0'; - } - return p; - } + while (len > 1 && ISSPACE(s[len - 1])) + s[--len] = '\0'; - return NULL; + return s + len; } const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen) @@ -141,3 +169,55 @@ const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen) haystack++; } } + +/** \brief \c NULL resistant strcasecmp. + * \param[in] a the first string. + * \param[in] b the second string. + * \return strcasecmp(a, b), and treats \c NULL strings like \c "" + * ones, as if we were in the C locale. + */ +int ascii_strcasecmp(const char *a, const char *b) +{ + if (a == b) + return 0; + if (!a) + return -1; + if (!b) + return 1; + + while (*a || *b) { + int i; + if ((i = ascii_tolower(*a++) - ascii_tolower(*b++))) + return i; + } + + return 0; +} + +/** \brief \c NULL resistant strncasecmp. + * \param[in] a the first string. + * \param[in] b the second string. + * \param[in] n the number of maximum chars to compare. + * \return strncasecmp(a, b), and treats \c NULL strings like \c "" + * ones, as if we were in the C locale. + */ +int ascii_strncasecmp(const char *a, const char *b, ssize_t n) +{ + if (a == b) + return 0; + if (!a) + return -1; + if (!b) + return 1; + + while ((*a || *b) && n > 0) { + int i; + if ((i = ascii_tolower(*a++) - ascii_tolower(*b++))) + return i; + n--; + } + + return 0; +} + +/*@}*/