]> mj.ucw.cz Git - libucw.git/blob - ucw/string.c
Growing arrays now support auto-zeroing mode
[libucw.git] / ucw / string.c
1 /*
2  *      UCW Library -- String Routines
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *      (c) 2007--2012 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #undef LOCAL_DEBUG
12
13 #include "ucw/lib.h"
14 #include "ucw/string.h"
15
16 #include <string.h>
17
18 #ifdef CONFIG_DARWIN
19 uns
20 strnlen(const char *str, uns n)
21 {
22   const char *end = str + n;
23   const char *c;
24   for (c = str; *c && c < end; c++);
25   return c - str;
26 }
27 #endif
28
29 char *
30 str_format_flags(char *dest, const char *fmt, uns flags)
31 {
32   char *start = dest;
33   for (uns i=0; fmt[i]; i++)
34     {
35       if (flags & (1 << i))
36         *dest++ = fmt[i];
37       else
38         *dest++ = '-';
39     }
40   *dest = 0;
41   return start;
42 }
43
44 uns
45 str_count_char(const char *str, uns chr)
46 {
47   const byte *s = str;
48   uns i = 0;
49   while (*s)
50     if (*s++ == chr)
51       i++;
52   return i;
53 }
54
55 int
56 str_starts_with(const char *haystack, const char *needle)
57 {
58   while (*needle)
59     if (*haystack++ != *needle++)
60       return 0;
61   return 1;
62 }
63
64 int
65 str_ends_with(const char *haystack, const char *needle)
66 {
67   int hlen = strlen(haystack);
68   int nlen = strlen(needle);
69   if (hlen < nlen)
70     return 0;
71   else
72     return !memcmp(haystack + hlen - nlen, needle, nlen);
73 }