From e5190818c43ff7dc97165fd3b0d9f9aa4ee89d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Mon, 7 Aug 2023 00:42:11 +0200 Subject: [PATCH] perf: mutate destination string for `string_concatenate` BREAKING CHANGE: Function signature changed --- Makefile | 4 ++-- lib/string.c | 29 ++++++++++++----------------- lib/string.h | 8 +++++--- set_version.c | 12 ++++++------ test/string_test.c | 8 +++++--- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 9d8d127..35cfd26 100644 --- a/Makefile +++ b/Makefile @@ -32,13 +32,13 @@ build/test/%.o: test/%.c ${HEADER_FILES} | build/test .PHONY: run run: ${LIB} ./main.c mkdir --parents ./bin - ${CC} ${CC_FLAGS} -o ${MAIN_EXECUTABLE} ./main.c ${LIB_CC_FLAGS} + ${CC} ${CC_FLAGS} ${CC_SANITIZER_FLAGS} -o ${MAIN_EXECUTABLE} ./main.c ${LIB_CC_FLAGS} ./${MAIN_EXECUTABLE} ${ARGS} .PHONY: set_version set_version: ${LIB} ./set_version.c mkdir --parents ./bin - ${CC} ${CC_FLAGS} -o ${SET_VERSION_EXECUTABLE} ./set_version.c ${LIB_CC_FLAGS} + ${CC} ${CC_FLAGS} ${CC_SANITIZER_FLAGS} -o ${SET_VERSION_EXECUTABLE} ./set_version.c ${LIB_CC_FLAGS} .PHONY: test test: ${LIB} $(addprefix build/, ${TEST_OBJECTS}) diff --git a/lib/string.c b/lib/string.c index 4d3fdbb..258176d 100644 --- a/lib/string.c +++ b/lib/string.c @@ -212,24 +212,20 @@ string_t string_join(string_t* array, const char separator, size_t array_length) return string; } -string_t string_concatenate(string_t string1, string_t string2) { - size_t string1_length = string_get_length(string1); - size_t string2_length = string_get_length(string2); - size_t result_length = string1_length + string2_length; - string_t result = malloc(sizeof(char) * (result_length + 1)); - if (result == NULL) { +void string_concatenate(string_t* destination, string_t source) { + size_t destination_length = string_get_length(*destination); + size_t source_length = string_get_length(source); + size_t new_length = destination_length + source_length; + *destination = realloc(*destination, sizeof(char) * (new_length + 1)); + if (*destination == NULL) { perror("Error (string_concatenate)"); exit(EXIT_FAILURE); } - size_t index_string1 = 0; - for (; index_string1 < string1_length; index_string1++) { - result[index_string1] = string1[index_string1]; + size_t index_destination = destination_length; + for (size_t index_source = 0; index_source < source_length; index_source++) { + (*destination)[index_destination++] = source[index_source]; } - for (size_t index_string2 = 0; index_string2 < string2_length; index_string2++) { - result[index_string1 + index_string2] = string2[index_string2]; - } - result[result_length] = '\0'; - return result; + (*destination)[index_destination] = '\0'; } bool string_get_has_unique_characters(const string_t string) { @@ -320,10 +316,9 @@ string_t string_get_formatted_number(const long long number, string_t separator) result[formatted_length] = '\0'; string_reverse(result); if (is_negative) { - string_t dash = convert_character_to_string('-'); - string_t negative_result = string_concatenate(dash, result); + string_t negative_result = convert_character_to_string('-'); + string_concatenate(&negative_result, result); free(result); - free(dash); return negative_result; } return result; diff --git a/lib/string.h b/lib/string.h index afc6efa..3b241bd 100644 --- a/lib/string.h +++ b/lib/string.h @@ -160,11 +160,13 @@ string_t string_join(string_t* array, const char separator, size_t array_length) /** * @brief Concatenate two strings. * - * @param string1 - * @param string2 + * NOTE: Mutates the string `destination`. + * + * @param destination + * @param source * @since v1.0.0 */ -string_t string_concatenate(string_t string1, string_t string2); +void string_concatenate(string_t* destination, string_t source); /** * @brief Check if a string contains only unique characters. diff --git a/set_version.c b/set_version.c index 79d831c..e333b17 100644 --- a/set_version.c +++ b/set_version.c @@ -3,16 +3,16 @@ #include "libcproject.h" -int main(int argc, string_t* argv) { +int main(int argc, char** argv) { if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return EXIT_FAILURE; } - string_t content = "#ifndef __LIBCPROJECT_VERSION__\n"; - content = string_concatenate(content, "#define __LIBCPROJECT_VERSION__ \""); - content = string_concatenate(content, argv[1]); - content = string_concatenate(content, "\"\n\n"); - content = string_concatenate(content, "#endif\n"); + string_t content = string_copy("#ifndef __LIBCPROJECT_VERSION__\n"); + string_concatenate(&content, "#define __LIBCPROJECT_VERSION__ \""); + string_concatenate(&content, argv[1]); + string_concatenate(&content, "\"\n\n"); + string_concatenate(&content, "#endif\n"); int result = filesystem_write("./version.h", (byte_t*)content, string_get_length(content)); if (result == -1) { fprintf(stderr, "Error: Could not write to file.\n"); diff --git a/test/string_test.c b/test/string_test.c index da54507..47c8444 100644 --- a/test/string_test.c +++ b/test/string_test.c @@ -158,12 +158,14 @@ void string_join_test() { } void string_concatenate_test() { - char* result = string_concatenate("abc", "def"); + string_t result = string_copy("abc"); + string_concatenate(&result, "def"); assert(assert_string_equal(result, "abcdef")); free(result); - result = string_concatenate("abc", " defghi"); - assert(assert_string_equal(result, "abc defghi")); + result = string_copy("abcz"); + string_concatenate(&result, " defghi"); + assert(assert_string_equal(result, "abcz defghi")); free(result); }