]> mj.ucw.cz Git - libucw.git/blob - lib/stkstring.c
simple explain support for fuzzy regions matching
[libucw.git] / lib / stkstring.c
1 #include "lib/lib.h"
2 #include "lib/stkstring.h"
3
4 #include <stdio.h>
5
6 uns
7 stk_array_len(char **s, uns cnt)
8 {
9   uns l = 1;
10   while (cnt--)
11     l += strlen(*s++);
12   return l;
13 }
14
15 void
16 stk_array_join(char *x, char **s, uns cnt, uns sep)
17 {
18   while (cnt--)
19     {
20       uns l = strlen(*s);
21       memcpy(x, *s, l);
22       x += l;
23       s++;
24       if (sep && cnt)
25         *x++ = sep;
26     }
27   *x = 0;
28 }
29
30 char *stk_printf_buf;
31 static int stk_printf_len;
32
33 uns
34 stk_printf_internal(char *fmt, ...)
35 {
36   va_list args, args2;
37   va_start(args, fmt);
38   if (!stk_printf_buf)
39     {
40       stk_printf_buf = xmalloc(256);
41       stk_printf_len = 256;
42     }
43   for (;;)
44     {
45       va_copy(args2, args);
46       int l = vsnprintf(stk_printf_buf, stk_printf_len, fmt, args2);
47       va_end(args2);
48       if (l < 0)
49         stk_printf_len *= 2;
50       else if (l < stk_printf_len)
51         {
52           va_end(args);
53           return l+1;
54         }
55       else
56         stk_printf_len = MAX(stk_printf_len*2, l+1);
57       stk_printf_buf = xrealloc(stk_printf_buf, stk_printf_len);
58     }
59 }
60
61 void
62 stk_hexdump_internal(char *dst, byte *src, uns n)
63 {
64   for (uns i=0; i<n; i++)
65     {
66       if (i)
67         *dst++ = ' ';
68       dst += sprintf(dst, "%02x", *src++);
69     }
70   *dst = 0;
71 }
72
73 #ifdef TEST
74
75 int main(void)
76 {
77   char *a = stk_strndup("are!",3);
78   a = stk_strcat(a, " the ");
79   a = stk_strmulticat(a, stk_strdup("Jabberwock, "), "my", NULL);
80   char *arr[] = { a, " son" };
81   a = stk_strarraycat(arr, 2);
82   a = stk_printf("Bew%s!", a);
83   puts(a);
84   puts(stk_hexdump(a, 3));
85   char *ary[] = { "The", "jaws", "that", "bite" };
86   puts(stk_strjoin(ary, 4, ' '));
87   return 0;
88 }
89
90 #endif