diff --git a/strops.c b/strops.c index 7ba6b9f..c1ff2f6 100644 --- a/strops.c +++ b/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)); diff --git a/strops.h b/strops.h index fe33219..61b070f 100644 --- a/strops.h +++ b/strops.h @@ -4,10 +4,8 @@ #include /* - ATTENTION! THIS LIBRARY CURRENTLY LEAKS MEMORY! Functions that return char*, return a new heap-allocated string. The user of this library is responsible for freeing the memory of the result. - No function modifies the input string. The input string is copied at the beginning of a function. Only 7-Bit Ascii is supported. */ @@ -23,6 +21,7 @@ int strops_is_uppercase(const char* string); char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, ull_t pos); char* strops_remove_at_pos_char(const char* string, ull_t pos); +void strops_remove_at_pos_char_inplace(char* string, ull_t pos); char* strops_trim_right_whitespace(const char* string); char* strops_trim_left_whitespace(const char* string);