diff --git a/src/commons/string.c b/src/commons/string.c index fcab1d60..84add702 100644 --- a/src/commons/string.c +++ b/src/commons/string.c @@ -28,7 +28,6 @@ static void _string_upper_element(char* ch); void _string_append_with_format_list(const char* format, char** original, va_list arguments); char** _string_split(char* text, char* separator, bool(*is_last_token)(int)); static void _string_array_push(char*** array, char* text, int size); -static char* _string_array_replace(char** array, int pos, char* text); char *string_repeat(char character, int count) { char *text = calloc(count + 1, 1); @@ -246,11 +245,14 @@ void string_array_push(char*** array, char* text) { } char* string_array_replace(char** array, int pos, char* text) { - return string_array_size(array) > pos && pos >= 0 ? _string_array_replace(array, pos, text) : NULL; + char* old_text = array[pos]; + array[pos] = text; + + return old_text; } char* string_array_pop(char** array) { - return string_array_size(array) > 0 ? _string_array_replace(array, string_array_size(array) - 1, NULL) : NULL; + return string_array_replace(array, string_array_size(array) - 1, NULL); } /** PRIVATE FUNCTIONS **/ @@ -313,11 +315,4 @@ static void _string_array_push(char*** array, char* text, int size) { *array = realloc(*array, sizeof(char*) * (size + 2)); (*array)[size] = text; (*array)[size + 1] = NULL; -} - -static char* _string_array_replace(char** array, int pos, char* text) { - char* old_text = array[pos]; - array[pos] = text; - - return old_text; } \ No newline at end of file diff --git a/tests/unit-tests/test_string.c b/tests/unit-tests/test_string.c index fbaa02c5..aa3d012c 100644 --- a/tests/unit-tests/test_string.c +++ b/tests/unit-tests/test_string.c @@ -567,15 +567,6 @@ context (test_string) { string_iterate_lines(names, _assert_names); } end - it ("not to remove elements in an empty array") { - for(int i = 0; i < 10; i++) { - string_array_pop(names); - } - - should_int(string_array_size(names)) be equal to (0); - should_bool(string_array_is_empty(names)) be truthy; - } end - it("replace an element") { char* name = string_array_replace(names, 2, "Damian"); @@ -590,23 +581,6 @@ context (test_string) { } string_iterate_lines(names, _assert_names); } end - - it("not to replace an element outside the array") { - char* name = string_array_replace(names, 4, "Damian"); - - should_ptr(name) be null; - should_int(string_array_size(names)) be equal to (4); - should_ptr(names[4]) be null; - - char* expected[] = {"Gaston", "Matias", "Sebastian", "Daniela"}; - int i = 0; - void _assert_names(char* name) { - should_ptr(name) not be null; - should_string(name) be equal to (expected[i]); - i++; - } - string_iterate_lines(names, _assert_names); - } end } end } end }