Add new function string_set_n to extract a sub-string from a string.

This commit is contained in:
Patrick Pelissier committed 2017-08-14 23:30:20 +02:00
1 parent 9fc684515a
commit 866e1e0b7a
2 files changed
+22

No files matched your search

+17
View File
@@ -242,6 +242,23 @@ string_set (string_t v1, const string_t v2)
STRING_CONTRACT (v1);
}
static inline void
string_set_n(string_t v, const string_t ref, size_t offset, size_t length)
{
STRING_CONTRACT (v);
STRING_CONTRACT (ref);
assert (v != ref); // Not supported yet
if (M_UNLIKELY (ref->size == 0)) {
v->size = 0;
if (v->ptr) {
v->ptr[0] = 0;
}
} else {
assert (ref->ptr != NULL);
string_set_strn(v, ref->ptr + offset, length);
}
}
static inline void
string_init_set(string_t v1, const string_t v2)
{
+5
View File
@@ -50,6 +50,11 @@ int main(void)
assert (strcmp(string_get_cstr(s1), "Hello") == 0);
string_clean(s1);
string_set_str (s1, "Hello");
string_set_str (s2, "Hello, world!");
string_set_n (s1, s2, 7, 5);
assert (strcmp(string_get_cstr(s1), "world") == 0);
string_set_str (s1, "Hello beautiful world!");
assert (string_size(s1) == 22);
assert (strcmp(string_get_cstr(s1), "Hello beautiful world!") == 0);