2 * Copyright © 2017 Thomas Helland
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "string_buffer.h"
28 ensure_capacity(struct _mesa_string_buffer
*str
, uint32_t needed_capacity
)
30 if (needed_capacity
<= str
->capacity
)
33 /* Too small, double until we can fit the new string */
34 uint32_t new_capacity
= str
->capacity
* 2;
35 while (needed_capacity
> new_capacity
)
38 str
->buf
= reralloc_array_size(str
, str
->buf
, sizeof(char), new_capacity
);
42 str
->capacity
= new_capacity
;
46 struct _mesa_string_buffer
*
47 _mesa_string_buffer_create(void *mem_ctx
, uint32_t initial_capacity
)
49 struct _mesa_string_buffer
*str
;
50 str
= ralloc(mem_ctx
, struct _mesa_string_buffer
);
55 /* If no initial capacity is set then set it to something */
56 str
->capacity
= initial_capacity
? initial_capacity
: 32;
57 str
->buf
= ralloc_array(str
, char, str
->capacity
);
65 str
->buf
[str
->length
] = '\0';
70 _mesa_string_buffer_append_all(struct _mesa_string_buffer
*str
,
71 uint32_t num_args
, ...)
76 va_start(args
, num_args
);
77 for (i
= 0; i
< num_args
; i
++) {
78 s
= va_arg(args
, char*);
79 if (!_mesa_string_buffer_append_len(str
, s
, strlen(s
))) {
89 _mesa_string_buffer_append_len(struct _mesa_string_buffer
*str
,
90 const char *c
, uint32_t len
)
92 uint32_t needed_length
= str
->length
+ len
+ 1;
94 /* Check if we're overflowing uint32_t */
95 if (needed_length
< str
->length
)
98 if (!ensure_capacity(str
, needed_length
))
101 memcpy(str
->buf
+ str
->length
, c
, len
);
103 str
->buf
[str
->length
] = '\0';
108 _mesa_string_buffer_vprintf(struct _mesa_string_buffer
*str
,
109 const char *format
, va_list args
)
111 /* We're looping two times to avoid duplicating code */
112 for (uint32_t i
= 0; i
< 2; i
++) {
114 va_copy(arg_copy
, args
);
115 uint32_t space_left
= str
->capacity
- str
->length
;
117 int32_t len
= util_vsnprintf(str
->buf
+ str
->length
,
118 space_left
, format
, arg_copy
);
121 /* Error in vsnprintf() or measured len overflows size_t */
122 if (unlikely(len
< 0 || str
->length
+ len
+ 1 < str
->length
))
125 /* There was enough space for the string; we're done */
126 if (len
< space_left
) {
131 /* Not enough space, resize and retry */
132 ensure_capacity(str
, str
->length
+ len
+ 1);
139 _mesa_string_buffer_printf(struct _mesa_string_buffer
*str
,
140 const char *format
, ...)
144 va_start(args
, format
);
145 res
= _mesa_string_buffer_vprintf(str
, format
, args
);