33 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
34 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
38 char *old_str, *new_str;
39 unsigned min_size, new_size;
41 if (buf->size == buf->size_max)
45 min_size = buf->len + 1 +
FFMIN(UINT_MAX - buf->len - 1, room);
46 new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
47 if (new_size < min_size)
48 new_size =
FFMIN(buf->size_max, min_size);
54 memcpy(new_str, buf->str, buf->len + 1);
63 extra_len =
FFMIN(extra_len, UINT_MAX - 5 - buf->len);
64 buf->len += extra_len;
66 buf->str[
FFMIN(buf->len, buf->size - 1)] = 0;
71 unsigned size_auto = (
char *)buf +
sizeof(*buf) -
72 buf->reserved_internal_buffer;
76 buf->str = buf->reserved_internal_buffer;
78 buf->size =
FFMIN(size_auto, size_max);
79 buf->size_max = size_max;
81 if (size_init > buf->size)
103 dst = room ? buf->str + buf->len : NULL;
105 extra_len =
vsnprintf(dst, room, fmt, vl);
109 if (extra_len < room)
126 dst = room ? buf->str + buf->len : NULL;
128 extra_len =
vsnprintf(dst, room, fmt, vl);
132 if (extra_len < room)
142 unsigned room, real_n;
152 real_n =
FFMIN(n, room - 1);
153 memset(buf->str + buf->len, c, real_n);
160 unsigned room, real_n;
170 real_n =
FFMIN(size, room - 1);
171 memcpy(buf->str + buf->len, data, real_n);
185 if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
189 room = !room ? strlen(fmt) + 1 :
190 room <= INT_MAX / 2 ? room * 2 : INT_MAX;
199 if ((l = strftime(buf2,
sizeof(buf2), fmt, tm))) {
207 static const char txt[] =
"[truncated strftime output]";
208 memset(buf->str + buf->len,
'!', room);
209 memcpy(buf->str + buf->len, txt,
FFMIN(
sizeof(txt) - 1, room));
219 unsigned char **
mem,
unsigned *actual_size)
224 *mem = *actual_size ? buf->str + buf->len : NULL;
237 unsigned real_size =
FFMIN(buf->len + 1, buf->size);
250 memcpy(str, buf->str, real_size);
259 buf->size = real_size;
263 #define WHITESPACES " \n\t"
268 const char *src0 =
src;
277 for (; *
src; src++) {
289 for (; *
src; src++) {
290 int is_first_last = src == src0 || !*(src+1);
292 int is_strictly_special = special_chars && strchr(special_chars, *src);
294 is_strictly_special || strchr(
"'\\", *src) ||
297 if (is_strictly_special ||
299 (is_special || (is_ws && is_first_last))))
320 for (i = 1; i <=
size; i++) {
322 for (j = i - 1; j > 0; j--)
323 p[j] = p[j] + p[j - 1];
324 for (j = 0; j <= i; j++)
334 struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
337 bprint_pascal(&b, 5);
338 printf(
"Short text in unlimited buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
339 printf(
"%s\n", b.str);
343 bprint_pascal(&b, 25);
344 printf(
"Long text in unlimited buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
348 bprint_pascal(&b, 25);
349 printf(
"Long text in limited buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
353 bprint_pascal(&b, 5);
354 printf(
"Short text in automatic buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
357 bprint_pascal(&b, 25);
358 printf(
"Long text in automatic buffer: %u/%u\n", (
unsigned)strlen(b.str)/8*8, b.len);
362 bprint_pascal(&b, 25);
363 printf(
"Long text count only buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
366 bprint_pascal(&b, 25);
367 printf(
"Long text count only buffer: %u/%u\n", (
unsigned)strlen(buf), b.len);
371 printf(
"strftime full: %u/%u \"%s\"\n", (
unsigned)strlen(buf), b.len, b.str);
376 printf(
"strftime truncated: %u/%u \"%s\"\n", (
unsigned)strlen(buf), b.len, b.str);