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>
19 stk_array_len(char **s, uint cnt)
28 stk_array_join(char *x, char **s, uint cnt, uint sep)
43 stk_printf_internal(const char *fmt, ...)
46 char *buf = alloca(len);
52 int l = vsnprintf(buf, len, fmt, args2);
66 stk_vprintf_internal(const char *fmt, va_list args)
69 char *buf = alloca(len);
74 int l = vsnprintf(buf, len, fmt, args2);
88 stk_hexdump_internal(char *dst, const byte *src, uint n)
90 mem_to_hex(dst, src, n, ' ');
94 stk_fsize_internal(char *buf, u64 x)
97 sprintf(buf, "%dB", (int)x);
99 sprintf(buf, "%.1fK", (double)x/(1<<10));
101 sprintf(buf, "%dK", (int)(x/(1<<10)));
103 sprintf(buf, "%.1fM", (double)x/(1<<20));
105 sprintf(buf, "%dM", (int)(x/(1<<20)));
106 else if (x < (u64)10<<30)
107 sprintf(buf, "%.1fG", (double)x/(1<<30));
108 else if (x != ~(u64)0)
109 sprintf(buf, "%dG", (int)(x/(1<<30)));
111 strcpy(buf, "unknown");
118 char *a = stk_strndup("are!",3);
119 a = stk_strcat(a, " the ");
120 a = stk_strmulticat(a, stk_strdup("Jabberwock, "), "my", NULL);
121 char *arr[] = { a, " son" };
122 a = stk_strarraycat(arr, 2);
123 a = stk_printf("Bew%s!", a);
125 puts(stk_hexdump(a, 3));
126 char *ary[] = { "The", "jaws", "that", "bite" };
127 puts(stk_strjoin(ary, 4, ' '));
128 puts(stk_fsize(1234567));