1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-08 22:31:31 +01:00

feat: support giving a custom character for string_trim, string_trim_start, string_trim_end

BREAKING CHANGE: Functions signatures changed.
If you want to preserve the same behavior, you should pass explictly the space character to trim:
Example: `string_trim(" Hello ")` => `string_trim(" Hello ", ' ')`
This commit is contained in:
Théo LUDWIG 2023-08-05 14:19:44 +02:00
parent 316dfa10e7
commit 06b34b115b
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
3 changed files with 17 additions and 17 deletions

View File

@ -52,14 +52,14 @@ string_t string_replace(string_t string_value, char search, char replace) {
return result;
}
string_t string_trim_start(string_t string_value) {
string_t string_trim_start(string_t string_value, char character) {
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(char) * (string_length + 1));
if (result == NULL) {
exit(EXIT_FAILURE);
}
size_t index_space = 0;
while (string_value[index_space] == ' ') {
while (string_value[index_space] == character) {
index_space++;
}
for (size_t index = index_space; index < string_length; index++) {
@ -69,14 +69,14 @@ string_t string_trim_start(string_t string_value) {
return result;
}
string_t string_trim_end(string_t string_value) {
string_t string_trim_end(string_t string_value, char character) {
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(char) * (string_length + 1));
if (result == NULL) {
exit(EXIT_FAILURE);
}
size_t index_space = string_length - 1;
while (string_value[index_space] == ' ') {
while (string_value[index_space] == character) {
index_space--;
}
for (size_t index = 0; index < index_space + 1; index++) {
@ -86,9 +86,9 @@ string_t string_trim_end(string_t string_value) {
return result;
}
string_t string_trim(string_t string_value) {
string_t result_start = string_trim_start(string_value);
string_t result = string_trim_end(result_start);
string_t string_trim(string_t string_value, char character) {
string_t result_start = string_trim_start(string_value, character);
string_t result = string_trim_end(result_start, character);
free(result_start);
return result;
}
@ -377,7 +377,7 @@ string_t string_get_last_occurence_of_character(const string_t string_value, cha
}
}
if (index_last_occurrence == SIZE_MAX) {
return string_copy("");
return "";
}
string_t result = malloc(sizeof(char) * (string_length - index_last_occurrence + 1));
if (result == NULL) {

View File

@ -45,28 +45,28 @@ string_t string_to_lowercase(string_t string_value);
string_t string_replace(string_t string_value, char search, char replace);
/**
* @brief Removes all whitespace from the start of a string.
* @brief Removes all `character` from the start of a string.
*
* @param string_value
* @since v1.0.0
*/
string_t string_trim_start(string_t string_value);
string_t string_trim_start(string_t string_value, char character);
/**
* @brief Removes all whitespace from the end of a string.
* @brief Removes all `character` from the end of a string.
*
* @param string_value
* @since v1.0.0
*/
string_t string_trim_end(string_t string_value);
string_t string_trim_end(string_t string_value, char character);
/**
* @brief Removes all whitespace from the start and end of a string.
* @brief Removes all `character` from the start and end of a string.
*
* @param string_value
* @since v1.0.0
*/
string_t string_trim(string_t string_value);
string_t string_trim(string_t string_value, char character);
/**
* @brief Return the copy of a string.

View File

@ -55,21 +55,21 @@ void string_replace_test() {
void string_trim_start_test() {
string_t string = " hello world ";
string = string_trim_start(string);
string = string_trim_start(string, ' ');
assert(assert_string_equal(string, "hello world "));
free(string);
}
void string_trim_end_test() {
string_t string = " hello world ";
string = string_trim_end(string);
string = string_trim_end(string, ' ');
assert(assert_string_equal(string, " hello world"));
free(string);
}
void string_trim_test() {
string_t string = " hello world ";
string = string_trim(string);
string = string_trim(string, ' ');
assert(assert_string_equal(string, "hello world"));
free(string);
}