X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=lib-lib%2Fstr.c;h=65377466269722906f436c709c98816f9afba513;hb=ceffe3e66d8c6690ac460046945f09aee43e6d46;hp=a5333e481fe7b1f7c62c8d75928ba1b4076ac310;hpb=93b012884de4ca5e1f2550f767d0b8680b9c0e9f;p=apps%2Fmadmutt.git diff --git a/lib-lib/str.c b/lib-lib/str.c index a5333e4..6537746 100644 --- a/lib-lib/str.c +++ b/lib-lib/str.c @@ -17,8 +17,7 @@ * Copyright © 2006 Pierre Habouzit */ -#include "macros.h" -#include "str.h" +#include "lib-lib.h" #define XX 255 unsigned char const __m_strdigits[128] = { @@ -141,3 +140,68 @@ 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; +} + +ssize_t m_snsubst(char *dst, ssize_t n, const char *fmt, const char *src) +{ + ssize_t pos = 0; + const char *p; + + p = strchr(fmt, '%'); + if (!p) + return snprintf(dst, n, "%s %s", fmt, src); + + for (;;) { + if (p[1] == 's') { + pos += m_strncpy(dst + pos, n - pos, fmt, p - fmt); + pos += m_strcpy(dst + pos, n - pos, src); + fmt = p + 2; + } else { + pos += m_strncpy(dst + pos, n - pos, fmt, p + 1 - fmt); + fmt = p + 1; + if (p[1] == '%') + p++; + } + + p = strchr(fmt, '%'); + if (!p) + return pos + m_strcpy(dst + pos, n - pos, fmt); + } +}