drop the old string API fully.
[apps/madmutt.git] / lib-lib / str.c
index 3b9b5b5..a5333e4 100644 (file)
@@ -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++;
+    }
+}