Clarift uc_dstr_ensure
This commit is contained in:
@@ -45,12 +45,16 @@ void uc_dstr_destroy(struct DStr *str);
|
|||||||
*/
|
*/
|
||||||
size_t uc_dstr_available(const struct DStr *str);
|
size_t uc_dstr_available(const struct DStr *str);
|
||||||
|
|
||||||
/** Ensure the DStr is of at least 'len' bytes,
|
/** Ensure the DStr have room for least 'len' bytes,
|
||||||
* allocating room if needed.
|
* allocating room if needed.
|
||||||
*
|
*
|
||||||
|
* This includes the length of the existing string,
|
||||||
|
* to ensure e.g an additional 10 bytes can be appended,
|
||||||
|
* one would do uc_dstr_ensure(str, str->len + 10)
|
||||||
|
*
|
||||||
* @return 0 on success (!= 0 if an allocation fail)
|
* @return 0 on success (!= 0 if an allocation fail)
|
||||||
*/
|
*/
|
||||||
int uc_dstr_ensure(struct DStr *str, size_t len);
|
int uc_dstr_ensure(struct DStr *str, size_t total);
|
||||||
|
|
||||||
/** Reset the DStr, setting its length to 0
|
/** Reset the DStr, setting its length to 0
|
||||||
*/
|
*/
|
||||||
|
|||||||
+3
-3
@@ -22,13 +22,13 @@ size_t uc_dstr_available(const struct DStr *str)
|
|||||||
return str->allocated - str->len;
|
return str->allocated - str->len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int uc_dstr_ensure(struct DStr *str, size_t len)
|
int uc_dstr_ensure(struct DStr *str, size_t total)
|
||||||
{
|
{
|
||||||
if (str->allocated < len) {
|
if (str->allocated < total) {
|
||||||
//allocate a minimum no. of bytes, and one
|
//allocate a minimum no. of bytes, and one
|
||||||
//additional for the common case of nul terminating
|
//additional for the common case of nul terminating
|
||||||
//the string
|
//the string
|
||||||
size_t new_len = UC_MAX(7U, len) + 1;
|
size_t new_len = UC_MAX(7U, total) + 1;
|
||||||
char *new_str = realloc(str->str, new_len);
|
char *new_str = realloc(str->str, new_len);
|
||||||
|
|
||||||
if (new_str == NULL) {
|
if (new_str == NULL) {
|
||||||
|
|||||||
Reference in New Issue
Block a user