2 * UCW Library -- Strings Allocated on the Stack
4 * (c) 2005--2007 Martin Mares <mj@ucw.cz>
5 * (c) 2005 Tomas Valla <tom@ucw.cz>
6 * (c) 2008 Pavel Charvat <pchar@ucw.cz>
8 * This software may be freely distributed and used according to the terms
9 * of the GNU Lesser General Public License.
13 #include "ucw/stkstring.h"
14 #include "ucw/string.h"
20 strnlen(const char *str, uns n)
22 const char *end = str + n;
24 for (c = str; *c && c < end; c++);
30 stk_array_len(char **s, uns cnt)
39 stk_array_join(char *x, char **s, uns cnt, uns sep)
54 stk_printf_internal(const char *fmt, ...)
57 char *buf = alloca(len);
63 int l = vsnprintf(buf, len, fmt, args2);
77 stk_vprintf_internal(const char *fmt, va_list args)
80 char *buf = alloca(len);
85 int l = vsnprintf(buf, len, fmt, args2);
99 stk_hexdump_internal(char *dst, const byte *src, uns n)
101 mem_to_hex(dst, src, n, ' ');
105 stk_fsize_internal(char *buf, u64 x)
108 sprintf(buf, "%dB", (int)x);
110 sprintf(buf, "%.1fK", (double)x/(1<<10));
112 sprintf(buf, "%dK", (int)(x/(1<<10)));
114 sprintf(buf, "%.1fM", (double)x/(1<<20));
116 sprintf(buf, "%dM", (int)(x/(1<<20)));
117 else if (x < (u64)10<<30)
118 sprintf(buf, "%.1fG", (double)x/(1<<30));
119 else if (x != ~(u64)0)
120 sprintf(buf, "%dG", (int)(x/(1<<30)));
122 strcpy(buf, "unknown");
129 char *a = stk_strndup("are!",3);
130 a = stk_strcat(a, " the ");
131 a = stk_strmulticat(a, stk_strdup("Jabberwock, "), "my", NULL);
132 char *arr[] = { a, " son" };
133 a = stk_strarraycat(arr, 2);
134 a = stk_printf("Bew%s!", a);
136 puts(stk_hexdump(a, 3));
137 char *ary[] = { "The", "jaws", "that", "bite" };
138 puts(stk_strjoin(ary, 4, ' '));
139 puts(stk_fsize(1234567));