1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2025-05-21 23:21:15 +02:00

perf: mutate strings instead of copy when possible

BREAKING CHANGE: Most of string functions mutates the string instead of copying now.
This allows better performance when copy is not needed.
It also allows more granual control.
If copy is wanted, simply use `string_copy` before calling the function.

Impacted functions are:
`string_to_uppercase`, `string_to_lowercase`, `string_replace`,
`string_trim_start`, `string_trim_end`, `string_trim`,
`string_capitalize`, `string_reverse`
This commit is contained in:
2023-08-06 23:17:07 +02:00
parent 01e9b71402
commit 72645da4b2
10 changed files with 193 additions and 222 deletions

View File

@ -3,20 +3,20 @@
string_t terminal_input() {
char character;
size_t length = 1;
string_t string_value = malloc(length * sizeof(char));
if (string_value == NULL) {
string_t string = malloc(length * sizeof(char));
if (string == NULL) {
exit(EXIT_FAILURE);
}
*string_value = '\0';
*string = '\0';
while ((character = getchar()) != '\n' && character != EOF) {
length++;
string_value = realloc(string_value, length * sizeof(char));
if (string_value == NULL) {
string = realloc(string, length * sizeof(char));
if (string == NULL) {
exit(EXIT_FAILURE);
}
character_append(string_value, character);
character_append(string, character);
}
return string_value;
return string;
}
void terminal_print_array(void* array, size_t array_size, size_t element_size, void (*print_element)(void*)) {