* Copyright © 2006 Pierre Habouzit
*/
-#include "macros.h"
-#include "str.h"
+#include "lib-lib.h"
#define XX 255
unsigned char const __m_strdigits[128] = {
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;
+}
+
+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);
+ }
+}