drop the horrible list2_t for good.
[apps/madmutt.git] / lib-lib / str.c
index 8b0c393..7b57305 100644 (file)
  *  Copyright © 2006 Pierre Habouzit
  */
 
-#include "macros.h"
-#include "str.h"
+#include "lib-lib.h"
+
+#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,
+    XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+    XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, XX, XX, XX, XX, XX, XX,
+    XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+    25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
+    XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+    25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
+};
+#undef XX
+
+#define XX -1
+signed char const __m_b64digits[128] = {
+    XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+    XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+    XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 62, XX, XX, XX, 63,
+    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, XX, XX, XX, XX, XX, XX,
+    XX,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
+    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, XX, XX, XX, XX, XX,
+    XX, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, XX, XX, XX, XX, XX
+};
+#undef XX
+
+char const __m_b64chars[64] = {
+    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+    'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
+    'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
+    't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+    '8', '9', '+', '/'
+};
+
+char const __m_b36chars_lower[36] = {
+    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
+    'u', 'v', 'w', 'x', 'y', 'z'
+};
+
+char const __m_b36chars_upper[36] = {
+    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
+    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+    'U', 'V', 'W', 'X', 'Y', 'Z'
+};
+
 
 ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
 {
@@ -45,3 +93,88 @@ 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++;
+    }
+}
+
+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;
+}