forked from github/server
add a str_adopt constructor for sbstring.
This commit is contained in:
parent
9d0c7b900a
commit
0590fc09ab
3 changed files with 16 additions and 1 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue