new strops_remove_at_pos_char_inplace
This commit is contained in:
27
strops.c
27
strops.c
@ -185,10 +185,21 @@ char* strops_remove_at_pos_char(const char* string, ull_t pos) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void strops_remove_at_pos_char_inplace(char* string, ull_t pos) {
|
||||
assert(pos <= strops_length(string) && "pos needs to be inside string");
|
||||
ull_t length = strops_length(string);
|
||||
string[pos] = 0;
|
||||
ull_t i;
|
||||
for (i = pos; i < length; i++) {
|
||||
string[i] = string[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
char* strops_trim_right_whitespace(const char* string) {
|
||||
char* result = strops_copy(string);
|
||||
char* tmp;
|
||||
while (strops_contains_char("\t\n\v\f\r ", result[strops_length(result) - 1])) {
|
||||
result = strops_remove_at_pos_char(result, strops_length(result) - 1);
|
||||
strops_remove_at_pos_char_inplace(result, strops_length(result) - 1);
|
||||
}
|
||||
result = realloc(result, strops_length(result));
|
||||
return result;
|
||||
@ -197,7 +208,7 @@ char* strops_trim_right_whitespace(const char* string) {
|
||||
char* strops_trim_left_whitespace(const char* string) {
|
||||
char* result = strops_copy(string);
|
||||
while (strops_contains_char("\t\n\v\f\r ", result[0])) {
|
||||
result = strops_remove_at_pos_char(result, 0);
|
||||
strops_remove_at_pos_char_inplace(result, 0);
|
||||
}
|
||||
result = realloc(result, strops_length(result));
|
||||
return result;
|
||||
@ -215,7 +226,7 @@ char* strops_trim_both_whitespace(const char* string) {
|
||||
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
||||
char* result = strops_copy(string);
|
||||
while (strops_contains_char(chars_to_remove, result[strops_length(result) - 1])) {
|
||||
result = strops_remove_at_pos_char(result, strops_length(result) - 1);
|
||||
strops_remove_at_pos_char_inplace(result, strops_length(result) - 1);
|
||||
}
|
||||
result = realloc(result, strops_length(result));
|
||||
return result;
|
||||
@ -224,7 +235,7 @@ char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
||||
char* strops_trim_left_chars(const char* string, const char* chars_to_remove) {
|
||||
char* result = strops_copy(string);
|
||||
while (strops_contains_char(chars_to_remove, result[0])) {
|
||||
result = strops_remove_at_pos_char(result, 0);
|
||||
strops_remove_at_pos_char_inplace(result, 0);
|
||||
}
|
||||
result = realloc(result, strops_length(result));
|
||||
return result;
|
||||
@ -248,7 +259,7 @@ char* strops_trim_right_string(const char* string, const char* string_to_remove)
|
||||
while (strcmp(tmp, string_to_remove) == 0) {
|
||||
ull_t i;
|
||||
for (i = 0; i < strops_length(string_to_remove); i++) {
|
||||
result = strops_remove_at_pos_char(result, offset);
|
||||
strops_remove_at_pos_char_inplace(result, offset);
|
||||
}
|
||||
offset = strops_length(result) - strops_length(string_to_remove);
|
||||
tmp = result + offset;
|
||||
@ -265,7 +276,7 @@ char* strops_trim_left_string(const char* string, const char* string_to_remove)
|
||||
while (strcmp(tmp, string_to_remove) == 0) {
|
||||
ull_t i;
|
||||
for (i = 0; i < strops_length(string_to_remove); i++) {
|
||||
result = strops_remove_at_pos_char(result, 0);
|
||||
strops_remove_at_pos_char_inplace(result, 0);
|
||||
}
|
||||
tmp = strops_copy_amount(result, strops_length(string_to_remove));
|
||||
}
|
||||
@ -289,7 +300,7 @@ char* strops_remove_chars(const char* string, const char* chars_to_remove) {
|
||||
ull_t i, j;
|
||||
for (i = 0; i < strops_length(chars_to_remove); i++) {
|
||||
while (strops_contains_char(result, chars_to_remove[i])) {
|
||||
result = strops_remove_at_pos_char(result, strops_first_pos_of_char(result, chars_to_remove[i]));
|
||||
strops_remove_at_pos_char_inplace(result, strops_first_pos_of_char(result, chars_to_remove[i]));
|
||||
}
|
||||
}
|
||||
result = realloc(result, strops_length(result));
|
||||
@ -304,7 +315,7 @@ char* strops_remove_string(const char* string, const char* string_to_remove) {
|
||||
ull_t offset = strops_first_pos_of_string(result, string_to_remove);
|
||||
ull_t i;
|
||||
for (i = 0; i < strops_length(string_to_remove); i++) {
|
||||
result = strops_remove_at_pos_char(result, offset);
|
||||
strops_remove_at_pos_char_inplace(result, offset);
|
||||
}
|
||||
}
|
||||
result = realloc(result, strops_length(result));
|
||||
|
Reference in New Issue
Block a user