add a str_adopt constructor for sbstring.

This commit is contained in:
Enno Rehling 2018-11-28 17:56:49 +01:00
parent 9d0c7b900a
commit 0590fc09ab
3 changed files with 16 additions and 1 deletions

View file

@ -263,13 +263,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;

View file

@ -50,6 +50,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

@ -157,6 +157,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)