Mercurial > libedl
diff src/str.c @ 8:0c98b46eaf73 v2.0
*: update API to 2.0, big changes
all APIs now use pointers to an EDL object. it is up to the
user to make sure that the pointer is valid.
additionally, many things have been separated into new files
to make it easier to digest
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 03 Mar 2024 17:56:58 -0500 |
parents | a6ab6d9c0dac |
children | 0cc2555db371 |
line wrap: on
line diff
--- a/src/str.c Mon Jan 15 06:42:30 2024 -0500 +++ b/src/str.c Sun Mar 03 17:56:58 2024 -0500 @@ -1,6 +1,11 @@ +#include "str.h" + #include <stdlib.h> #include <string.h> -#include "str.h" + +/* rudimentary string functions so the code in edl.c isn't + * as painful to read +*/ int EDL_internal_string_init(EDL_internal_string* str) { if (!str) @@ -14,14 +19,14 @@ int EDL_internal_string_allocate(EDL_internal_string* str, size_t new_capacity) { if (new_capacity == str->capacity) - return 1; // nothing to do + return 1; /* nothing to do */ str->data = realloc(str->data, new_capacity * sizeof(char)); if (!str->data) return 0; - if (new_capacity > str->capacity) - memset(&str->data[str->capacity], 0, (new_capacity - str->capacity) * sizeof(char)); + //if (new_capacity > str->capacity) + // memset(&str->data[str->capacity], 0, (new_capacity - str->capacity) * sizeof(char)); str->capacity = new_capacity; @@ -29,19 +34,23 @@ } int EDL_internal_string_append(EDL_internal_string* str, const char* data, const size_t length) { - { + if (str->capacity == 0) + if (!EDL_internal_string_allocate(str, 1)) + return 0; + + if (str->size + length + 1 >= str->capacity) { size_t capacity = 1; while (capacity < (str->size + length + 1)) capacity *= 2; - if (capacity < str->capacity || !EDL_internal_string_allocate(str, capacity)) + if (!EDL_internal_string_allocate(str, capacity)) return 0; } strncat(str->data, data, length); str->size += length; - return 1; + return length; } void EDL_internal_string_free(EDL_internal_string* str) {