1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-12-11 21:13:00 +01:00

perf: mutate destination string for string_concatenate

BREAKING CHANGE: Function signature changed
This commit is contained in:
Théo LUDWIG 2023-08-07 00:42:11 +02:00
parent b922fd9cd3
commit e5190818c4
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
5 changed files with 30 additions and 31 deletions

View File

@ -32,13 +32,13 @@ build/test/%.o: test/%.c ${HEADER_FILES} | build/test
.PHONY: run .PHONY: run
run: ${LIB} ./main.c run: ${LIB} ./main.c
mkdir --parents ./bin 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} ./${MAIN_EXECUTABLE} ${ARGS}
.PHONY: set_version .PHONY: set_version
set_version: ${LIB} ./set_version.c set_version: ${LIB} ./set_version.c
mkdir --parents ./bin 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 .PHONY: test
test: ${LIB} $(addprefix build/, ${TEST_OBJECTS}) test: ${LIB} $(addprefix build/, ${TEST_OBJECTS})

View File

@ -212,24 +212,20 @@ string_t string_join(string_t* array, const char separator, size_t array_length)
return string; return string;
} }
string_t string_concatenate(string_t string1, string_t string2) { void string_concatenate(string_t* destination, string_t source) {
size_t string1_length = string_get_length(string1); size_t destination_length = string_get_length(*destination);
size_t string2_length = string_get_length(string2); size_t source_length = string_get_length(source);
size_t result_length = string1_length + string2_length; size_t new_length = destination_length + source_length;
string_t result = malloc(sizeof(char) * (result_length + 1)); *destination = realloc(*destination, sizeof(char) * (new_length + 1));
if (result == NULL) { if (*destination == NULL) {
perror("Error (string_concatenate)"); perror("Error (string_concatenate)");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
size_t index_string1 = 0; size_t index_destination = destination_length;
for (; index_string1 < string1_length; index_string1++) { for (size_t index_source = 0; index_source < source_length; index_source++) {
result[index_string1] = string1[index_string1]; (*destination)[index_destination++] = source[index_source];
} }
for (size_t index_string2 = 0; index_string2 < string2_length; index_string2++) { (*destination)[index_destination] = '\0';
result[index_string1 + index_string2] = string2[index_string2];
}
result[result_length] = '\0';
return result;
} }
bool string_get_has_unique_characters(const string_t string) { 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'; result[formatted_length] = '\0';
string_reverse(result); string_reverse(result);
if (is_negative) { if (is_negative) {
string_t dash = convert_character_to_string('-'); string_t negative_result = convert_character_to_string('-');
string_t negative_result = string_concatenate(dash, result); string_concatenate(&negative_result, result);
free(result); free(result);
free(dash);
return negative_result; return negative_result;
} }
return result; return result;

View File

@ -160,11 +160,13 @@ string_t string_join(string_t* array, const char separator, size_t array_length)
/** /**
* @brief Concatenate two strings. * @brief Concatenate two strings.
* *
* @param string1 * NOTE: Mutates the string `destination`.
* @param string2 *
* @param destination
* @param source
* @since v1.0.0 * @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. * @brief Check if a string contains only unique characters.

View File

@ -3,16 +3,16 @@
#include "libcproject.h" #include "libcproject.h"
int main(int argc, string_t* argv) { int main(int argc, char** argv) {
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s <version>\n", argv[0]); fprintf(stderr, "Usage: %s <version>\n", argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
string_t content = "#ifndef __LIBCPROJECT_VERSION__\n"; string_t content = string_copy("#ifndef __LIBCPROJECT_VERSION__\n");
content = string_concatenate(content, "#define __LIBCPROJECT_VERSION__ \""); string_concatenate(&content, "#define __LIBCPROJECT_VERSION__ \"");
content = string_concatenate(content, argv[1]); string_concatenate(&content, argv[1]);
content = string_concatenate(content, "\"\n\n"); string_concatenate(&content, "\"\n\n");
content = string_concatenate(content, "#endif\n"); string_concatenate(&content, "#endif\n");
int result = filesystem_write("./version.h", (byte_t*)content, string_get_length(content)); int result = filesystem_write("./version.h", (byte_t*)content, string_get_length(content));
if (result == -1) { if (result == -1) {
fprintf(stderr, "Error: Could not write to file.\n"); fprintf(stderr, "Error: Could not write to file.\n");

View File

@ -158,12 +158,14 @@ void string_join_test() {
} }
void string_concatenate_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")); assert(assert_string_equal(result, "abcdef"));
free(result); free(result);
result = string_concatenate("abc", " defghi"); result = string_copy("abcz");
assert(assert_string_equal(result, "abc defghi")); string_concatenate(&result, " defghi");
assert(assert_string_equal(result, "abcz defghi"));
free(result); free(result);
} }