object: add b_string_substr
This commit is contained in:
@@ -365,6 +365,28 @@ const char *b_string_ptr(const struct b_string *str)
|
||||
return str->s_data.d_external;
|
||||
}
|
||||
|
||||
struct b_string *b_string_substr(const struct b_string *str, size_t start, size_t len)
|
||||
{
|
||||
if (start > b_string_get_size(str, B_STRLEN_NORMAL)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (start + len > b_string_get_size(str, B_STRLEN_NORMAL)) {
|
||||
len = b_string_get_size(str, B_STRLEN_NORMAL) - start;
|
||||
}
|
||||
|
||||
struct b_string *newstr = b_string_create();
|
||||
b_string_reserve(newstr, len);
|
||||
|
||||
const char *src = b_string_ptr(str) + start;
|
||||
char *dest = string_ptr(newstr);
|
||||
|
||||
memcpy(dest, src, len);
|
||||
newstr->s_len = len;
|
||||
|
||||
return newstr;
|
||||
}
|
||||
|
||||
static void string_release(struct b_object *obj)
|
||||
{
|
||||
struct b_string *str = B_STRING(obj);
|
||||
|
||||
Reference in New Issue
Block a user