X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=lib-lib%2Fstr.c;h=e4616e60f15ec8bdec1fc3c9f0b4e89f222aaaa0;hb=688ac22f746f785c27ac99ac86aa85a3035a3638;hp=a5333e481fe7b1f7c62c8d75928ba1b4076ac310;hpb=93b012884de4ca5e1f2550f767d0b8680b9c0e9f;p=apps%2Fmadmutt.git diff --git a/lib-lib/str.c b/lib-lib/str.c index a5333e4..e4616e6 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_ME #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'; @@ -86,7 +106,7 @@ 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'; @@ -141,3 +161,43 @@ const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen) haystack++; } } + +int ascii_strcasecmp(const char *a, const char *b) +{ + int i; + + if (a == b) + return 0; + if (a == NULL && b) + return -1; + if (b == NULL && a) + return 1; + + for (; *a || *b; a++, b++) { + if ((i = ascii_tolower(*a) - ascii_tolower(*b))) + return i; + } + + return 0; +} + +int ascii_strncasecmp (const char *a, const char *b, ssize_t n) +{ + int i, j; + + if (a == b) + return 0; + if (a == NULL && b) + return -1; + if (b == NULL && a) + return 1; + + for (j = 0; (*a || *b) && j < n; a++, b++, j++) { + if ((i = ascii_tolower(*a) - ascii_tolower(*b))) + return i; + } + + return 0; +} + +/*@}*/