diff --git a/src/string.c b/src/string.c index 754ca78..1fae4bc 100644 --- a/src/string.c +++ b/src/string.c @@ -180,19 +180,37 @@ bool cfw_string_set(CFWString *str, const char *cstr) { char *copy; + size_t len; - if ((copy = cfw_strdup(cstr)) == NULL) - return false; + if (str != NULL) { + if ((copy = cfw_strdup(cstr)) == NULL) + return false; + + len = strlen(copy); + } else { + copy = NULL; + len = 0; + } if (str->data != NULL) free(str->data); str->data = copy; - str->len = strlen(copy); + str->len = len; return true; } +void +cfw_string_set_nocopy(CFWString *str, char *cstr, size_t len) +{ + if (str->data != NULL) + free(str->data); + + str->data = cstr; + str->len = len; +} + bool cfw_string_append(CFWString *str, CFWString *append) { diff --git a/src/string.h b/src/string.h index 87737c8..73d33c0 100644 --- a/src/string.h +++ b/src/string.h @@ -37,6 +37,7 @@ extern char* cfw_strndup(const char*, size_t); extern const char* cfw_string_c(CFWString*); extern size_t cfw_string_length(CFWString*); extern bool cfw_string_set(CFWString*, const char*); +extern void cfw_string_set_nocopy(CFWString*, char*, size_t); extern bool cfw_string_append(CFWString*, CFWString*); extern size_t cfw_string_find(CFWString*, CFWString*, cfw_range_t);