32 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
33 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
37 char *old_str, *new_str;
38 unsigned min_size, new_size;
40 if (buf->size == buf->size_max)
44 min_size = buf->len + 1 +
FFMIN(UINT_MAX - buf->len - 1, room);
45 new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
46 if (new_size < min_size)
47 new_size =
FFMIN(buf->size_max, min_size);
53 memcpy(new_str, buf->str, buf->len + 1);
62 extra_len =
FFMIN(extra_len, UINT_MAX - 5 - buf->len);
63 buf->len += extra_len;
65 buf->str[
FFMIN(buf->len, buf->size - 1)] = 0;
70 unsigned size_auto = (
char *)buf +
sizeof(*buf) -
71 buf->reserved_internal_buffer;
75 buf->str = buf->reserved_internal_buffer;
77 buf->size =
FFMIN(size_auto, size_max);
78 buf->size_max = size_max;
80 if (size_init > buf->size)
102 dst = room ? buf->str + buf->len :
NULL;
104 extra_len =
vsnprintf(dst, room, fmt, vl);
108 if (extra_len < room)
118 unsigned room, real_n;
128 real_n =
FFMIN(n, room - 1);
129 memset(buf->str + buf->len, c, real_n);
143 if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
147 room = !room ? strlen(fmt) + 1 :
148 room <= INT_MAX / 2 ? room * 2 : INT_MAX;
157 if ((l = strftime(buf2,
sizeof(buf2), fmt, tm))) {
165 static const char txt[] =
"[truncated strftime output]";
166 memset(buf->str + buf->len,
'!', room);
167 memcpy(buf->str + buf->len, txt,
FFMIN(
sizeof(txt) - 1, room));
177 unsigned char **mem,
unsigned *actual_size)
182 *mem = *actual_size ? buf->str + buf->len :
NULL;
195 unsigned real_size =
FFMIN(buf->len + 1, buf->size);
208 memcpy(str, buf->str, real_size);
217 buf->size = real_size;
221 #define WHITESPACES " \n\t"
226 const char *src0 = src;
235 for (; *src; src++) {
247 for (; *src; src++) {
248 int is_first_last = src == src0 || !*(src+1);
250 int is_strictly_special = special_chars && strchr(special_chars, *src);
252 is_strictly_special || strchr(
"'\\", *src) ||
255 if (is_strictly_special ||
257 (is_special || (is_ws && is_first_last))))
278 for (i = 1; i <=
size; i++) {
280 for (j = i - 1; j > 0; j--)
281 p[j] = p[j] + p[j - 1];
282 for (j = 0; j <= i; j++)
292 struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
295 bprint_pascal(&b, 5);
296 printf(
"Short text in unlimited buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
297 printf(
"%s\n", b.str);
301 bprint_pascal(&b, 25);
302 printf(
"Long text in unlimited buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
306 bprint_pascal(&b, 25);
307 printf(
"Long text in limited buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
311 bprint_pascal(&b, 5);
312 printf(
"Short text in automatic buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
315 bprint_pascal(&b, 25);
316 printf(
"Long text in automatic buffer: %u/%u\n", (
unsigned)strlen(b.str)/8*8, b.len);
320 bprint_pascal(&b, 25);
321 printf(
"Long text count only buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
324 bprint_pascal(&b, 25);
325 printf(
"Long text count only buffer: %u/%u\n", (
unsigned)strlen(buf), b.len);
329 printf(
"strftime full: %u/%u \"%s\"\n", (
unsigned)strlen(buf), b.len, b.str);
334 printf(
"strftime truncated: %u/%u \"%s\"\n", (
unsigned)strlen(buf), b.len, b.str);