drop str_adjust: we don't care about a few octets unused, please do me a
[apps/madmutt.git] / lib / str.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14
15 #include <lib-lib/mem.h>
16 #include <lib-lib/str.h>
17
18 #include "str.h"
19
20 /* convert all characters in the string to lowercase */
21 char *str_tolower (char *s)
22 {
23   char *p = s;
24
25   while (*p) {
26     *p = tolower ((unsigned char) *p);
27     p++;
28   }
29
30   return (s);
31 }
32
33 /* NULL-pointer aware string comparison functions */
34
35 char *str_substrcpy (char *dest, const char *beg, const char *end,
36                       size_t destlen)
37 {
38   size_t len;
39
40   len = end - beg;
41   if (len > destlen - 1)
42     len = destlen - 1;
43   memcpy (dest, beg, len);
44   dest[len] = 0;
45   return dest;
46 }
47
48 char *str_substrdup(const char *begin, const char *end)
49 {
50     return p_dupstr(begin, (end ? end - begin : strlen(begin)));
51 }
52
53 const char *str_isstr (const char *haystack, const char *needle)
54 {
55   const char *p, *q;
56
57   if (!haystack)
58     return NULL;
59   if (!needle)
60     return (haystack);
61
62   while (*(p = haystack)) {
63     for (q = needle;
64          *p && *q &&
65          tolower ((unsigned char) *p) == tolower ((unsigned char) *q);
66          p++, q++);
67     if (!*q)
68       return (haystack);
69     haystack++;
70   }
71   return NULL;
72 }
73
74 int str_eq (const char* s1, const char* s2) {
75   int l = m_strlen(s1);
76
77   if (l != m_strlen(s2))
78     return (0);
79   return (m_strncmp(s1, s2, l) == 0);
80 }
81
82 void str_skip_trailws (char *s) {
83   char *p;
84
85   for (p = s + m_strlen(s) - 1; p >= s && ISSPACE (*p); p--)
86     *p = 0;
87 }