]> mj.ucw.cz Git - libucw.git/blob - ucw/string.c
tableprinter: code cleanup
[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 #ifdef CONFIG_DARWIN
17 size_t
18 strnlen(const char *str, size_t n)
19 {
20   const char *end = str + n;
21   const char *c;
22   for (c = str; *c && c < end; c++);
23   return c - str;
24 }
25 #endif
26
27 char *
28 str_format_flags(char *dest, const char *fmt, uint flags)
29 {
30   char *start = dest;
31   for (uint i=0; fmt[i]; i++)
32     {
33       if (flags & (1 << i))
34         *dest++ = fmt[i];
35       else
36         *dest++ = '-';
37     }
38   *dest = 0;
39   return start;
40 }
41
42 size_t
43 str_count_char(const char *str, uint chr)
44 {
45   const byte *s = str;
46   size_t i = 0;
47   while (*s)
48     if (*s++ == chr)
49       i++;
50   return i;
51 }