replace SKIPWS with a proper inline func with the right API.
[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 void str_replace (char **p, const char *s)
21 {
22   p_delete(p);
23   *p = m_strdup(s);
24 }
25
26 void str_adjust (char **p)
27 {
28   if (!p || !*p)
29     return;
30   p_realloc(p, m_strlen(*p) + 1);
31 }
32
33 /* convert all characters in the string to lowercase */
34 char *str_tolower (char *s)
35 {
36   char *p = s;
37
38   while (*p) {
39     *p = tolower ((unsigned char) *p);
40     p++;
41   }
42
43   return (s);
44 }
45
46 /* NULL-pointer aware string comparison functions */
47
48 char *str_substrcpy (char *dest, const char *beg, const char *end,
49                       size_t destlen)
50 {
51   size_t len;
52
53   len = end - beg;
54   if (len > destlen - 1)
55     len = destlen - 1;
56   memcpy (dest, beg, len);
57   dest[len] = 0;
58   return dest;
59 }
60
61 char *str_substrdup(const char *begin, const char *end)
62 {
63     return p_dupstr(begin, (end ? end - begin : strlen(begin)));
64 }
65
66 const char *str_isstr (const char *haystack, const char *needle)
67 {
68   const char *p, *q;
69
70   if (!haystack)
71     return NULL;
72   if (!needle)
73     return (haystack);
74
75   while (*(p = haystack)) {
76     for (q = needle;
77          *p && *q &&
78          tolower ((unsigned char) *p) == tolower ((unsigned char) *q);
79          p++, q++);
80     if (!*q)
81       return (haystack);
82     haystack++;
83   }
84   return NULL;
85 }
86
87 int str_eq (const char* s1, const char* s2) {
88   int l = m_strlen(s1);
89
90   if (l != m_strlen(s2))
91     return (0);
92   return (m_strncmp(s1, s2, l) == 0);
93 }
94
95 void str_skip_trailws (char *s) {
96   char *p;
97
98   for (p = s + m_strlen(s) - 1; p >= s && ISSPACE (*p); p--)
99     *p = 0;
100 }