mirror of
https://github.com/theoludwig/libcproject.git
synced 2024-11-08 22:31:31 +01:00
perf: mutate destination string for string_concatenate
BREAKING CHANGE: Function signature changed
This commit is contained in:
parent
b922fd9cd3
commit
e5190818c4
4
Makefile
4
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})
|
||||
|
29
lib/string.c
29
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;
|
||||
|
@ -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.
|
||||
|
@ -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 <version>\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");
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user