object: add a range of string transformation functions
This commit is contained in:
@@ -199,6 +199,45 @@ b_status b_string_reserve(struct b_string *str, size_t capacity)
|
||||
return err == 0 ? B_SUCCESS : B_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
b_status b_string_replace(
|
||||
struct b_string *str, size_t start, size_t length, const char *new_data)
|
||||
{
|
||||
b_status status = B_SUCCESS;
|
||||
size_t new_data_len = strlen(new_data);
|
||||
|
||||
if (start >= str->s_len) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (start + length >= str->s_len) {
|
||||
length = str->s_len - start;
|
||||
}
|
||||
|
||||
size_t new_str_len = str->s_len - length + new_data_len;
|
||||
if (new_str_len > str->s_max) {
|
||||
status = b_string_reserve(str, new_str_len);
|
||||
}
|
||||
|
||||
if (!B_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
char *s = string_ptr(str);
|
||||
|
||||
char *substitution_start = s + start;
|
||||
char *excess_src = s + start + length;
|
||||
size_t excess_length = str->s_len - start - length;
|
||||
char *excess_dest = substitution_start + new_data_len;
|
||||
|
||||
memmove(excess_dest, excess_src, excess_length);
|
||||
memmove(substitution_start, new_data, new_data_len);
|
||||
s[new_str_len] = '\0';
|
||||
|
||||
str->s_len = new_str_len;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_string_replace_all(b_string *str, const char *new_data)
|
||||
{
|
||||
size_t new_len = strlen(new_data);
|
||||
@@ -211,6 +250,48 @@ b_status b_string_replace_all(b_string *str, const char *new_data)
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_string_remove(b_string *str, size_t start, size_t length)
|
||||
{
|
||||
b_status status = B_SUCCESS;
|
||||
|
||||
if (start >= str->s_len) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (start + length >= str->s_len) {
|
||||
length = str->s_len - start;
|
||||
}
|
||||
|
||||
size_t new_str_len = str->s_len - length;
|
||||
|
||||
char *s = string_ptr(str);
|
||||
|
||||
char *removal_start = s + start;
|
||||
char *excess_src = s + start + length;
|
||||
size_t excess_length = str->s_len - start - length;
|
||||
|
||||
memmove(removal_start, excess_src, excess_length);
|
||||
s[new_str_len] = '\0';
|
||||
|
||||
str->s_len = new_str_len;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_string_transform(b_string *str, int (*transformer)(int))
|
||||
{
|
||||
char *s = string_ptr(str);
|
||||
for (size_t i = 0; i < str->s_len; i++) {
|
||||
int c = transformer(s[i]);
|
||||
|
||||
if (c != 0) {
|
||||
s[i] = c;
|
||||
}
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static enum b_status stream_close(struct b_stream *stream)
|
||||
{
|
||||
struct b_string *str = stream->s_ptr;
|
||||
|
||||
Reference in New Issue
Block a user