diff --git a/src/util/strings.c b/src/util/strings.c index eee8836f8..e469c2a18 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -259,13 +259,23 @@ char *str_strdup(const char *s) { void sbs_init(struct sbstring *sbs, char *buffer, size_t size) { assert(sbs); - assert(size>0); + assert(size > 0); sbs->begin = buffer; sbs->size = size; sbs->end = buffer; buffer[0] = '\0'; } +void sbs_adopt(struct sbstring *sbs, char *buffer, size_t size) +{ + size_t len = strlen(buffer); + assert(sbs); + assert(size > len); + sbs->begin = buffer; + sbs->size = size; + sbs->end = buffer + len; +} + void sbs_strncat(struct sbstring *sbs, const char *str, size_t size) { size_t len; diff --git a/src/util/strings.h b/src/util/strings.h index afcb477e4..29ca11ef7 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -49,6 +49,7 @@ extern "C" { } sbstring; void sbs_init(struct sbstring *sbs, char *buffer, size_t size); + void sbs_adopt(struct sbstring *sbs, char *buffer, size_t size); void sbs_strcat(struct sbstring *sbs, const char *str); void sbs_strncat(struct sbstring *sbs, const char *str, size_t size); void sbs_strcpy(struct sbstring *sbs, const char *str); diff --git a/src/util/strings.test.c b/src/util/strings.test.c index 80ad531e9..b9ab8c729 100644 --- a/src/util/strings.test.c +++ b/src/util/strings.test.c @@ -165,6 +165,10 @@ static void test_sbstring(CuTest * tc) CuAssertStrEquals(tc, "12345678901234", sbs.begin); sbs_strncat(&sbs, "567890", 6); CuAssertStrEquals(tc, "123456789012345", sbs.begin); + + sbs_adopt(&sbs, buffer + 2, sizeof(buffer) - 2); + CuAssertStrEquals(tc, "3456789012345", sbs.begin); + CuAssertIntEquals(tc, 13, sbs_length(&sbs)); } static void test_sbs_strcat(CuTest * tc)