add a str_adopt constructor for sbstring.

This commit is contained in:
Enno Rehling 2018-11-28 17:56:49 +01:00
parent 164baecbd8
commit 5d80f99552
3 changed files with 16 additions and 1 deletions

View File

@ -266,6 +266,16 @@ void sbs_init(struct sbstring *sbs, char *buffer, size_t size)
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;

View File

@ -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);

View File

@ -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)