X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Fstr.c;h=a5333e481fe7b1f7c62c8d75928ba1b4076ac310;hp=3b9b5b5ec267cd6c82d6ab4bf632e6881bcc5576;hb=93b012884de4ca5e1f2550f767d0b8680b9c0e9f;hpb=3c3c535e5ed1d651c6024b5acf670e217af473f7 diff --git a/lib-lib/str.c b/lib-lib/str.c index 3b9b5b5..a5333e4 100644 --- a/lib-lib/str.c +++ b/lib-lib/str.c @@ -94,3 +94,50 @@ ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) return len; } + +char *m_strrtrim(char *s) +{ + if (s) { + char *p = s + m_strlen(s); + + while (p > s && ISSPACE(p[-1])) { + *--p = '\0'; + } + return p; + } + + return NULL; +} + +const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen) +{ + int nc; + + if (!nlen) + return haystack; + + nc = tolower(*needle); + for (;;) { + int c = tolower(*haystack); + + if (c != nc) { + if (c == '\0') + return NULL; + } else { + ssize_t i; + + /* compare the rest of needle */ + for (i = 1;; i++) { + if (i == nlen) + return haystack; + if (c == '\0') + return NULL; + c = tolower(haystack[i]); + if (c != tolower(needle[i])) + break; + } + } + + haystack++; + } +}