mirror of
https://github.com/theoludwig/libcproject.git
synced 2024-12-11 21:13:00 +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:
parent
316dfa10e7
commit
06b34b115b
16
lib/string.c
16
lib/string.c
@ -52,14 +52,14 @@ string_t string_replace(string_t string_value, char search, char replace) {
|
|||||||
return result;
|
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);
|
size_t string_length = string_get_length(string_value);
|
||||||
string_t result = malloc(sizeof(char) * (string_length + 1));
|
string_t result = malloc(sizeof(char) * (string_length + 1));
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
size_t index_space = 0;
|
size_t index_space = 0;
|
||||||
while (string_value[index_space] == ' ') {
|
while (string_value[index_space] == character) {
|
||||||
index_space++;
|
index_space++;
|
||||||
}
|
}
|
||||||
for (size_t index = index_space; index < string_length; index++) {
|
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;
|
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);
|
size_t string_length = string_get_length(string_value);
|
||||||
string_t result = malloc(sizeof(char) * (string_length + 1));
|
string_t result = malloc(sizeof(char) * (string_length + 1));
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
size_t index_space = string_length - 1;
|
size_t index_space = string_length - 1;
|
||||||
while (string_value[index_space] == ' ') {
|
while (string_value[index_space] == character) {
|
||||||
index_space--;
|
index_space--;
|
||||||
}
|
}
|
||||||
for (size_t index = 0; index < index_space + 1; index++) {
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_trim(string_t string_value) {
|
string_t string_trim(string_t string_value, char character) {
|
||||||
string_t result_start = string_trim_start(string_value);
|
string_t result_start = string_trim_start(string_value, character);
|
||||||
string_t result = string_trim_end(result_start);
|
string_t result = string_trim_end(result_start, character);
|
||||||
free(result_start);
|
free(result_start);
|
||||||
return result;
|
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) {
|
if (index_last_occurrence == SIZE_MAX) {
|
||||||
return string_copy("");
|
return "";
|
||||||
}
|
}
|
||||||
string_t result = malloc(sizeof(char) * (string_length - index_last_occurrence + 1));
|
string_t result = malloc(sizeof(char) * (string_length - index_last_occurrence + 1));
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
|
12
lib/string.h
12
lib/string.h
@ -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);
|
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
|
* @param string_value
|
||||||
* @since v1.0.0
|
* @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
|
* @param string_value
|
||||||
* @since v1.0.0
|
* @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
|
* @param string_value
|
||||||
* @since v1.0.0
|
* @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.
|
* @brief Return the copy of a string.
|
||||||
|
@ -55,21 +55,21 @@ void string_replace_test() {
|
|||||||
|
|
||||||
void string_trim_start_test() {
|
void string_trim_start_test() {
|
||||||
string_t string = " hello world ";
|
string_t string = " hello world ";
|
||||||
string = string_trim_start(string);
|
string = string_trim_start(string, ' ');
|
||||||
assert(assert_string_equal(string, "hello world "));
|
assert(assert_string_equal(string, "hello world "));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_trim_end_test() {
|
void string_trim_end_test() {
|
||||||
string_t string = " hello world ";
|
string_t string = " hello world ";
|
||||||
string = string_trim_end(string);
|
string = string_trim_end(string, ' ');
|
||||||
assert(assert_string_equal(string, " hello world"));
|
assert(assert_string_equal(string, " hello world"));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_trim_test() {
|
void string_trim_test() {
|
||||||
string_t string = " hello world ";
|
string_t string = " hello world ";
|
||||||
string = string_trim(string);
|
string = string_trim(string, ' ');
|
||||||
assert(assert_string_equal(string, "hello world"));
|
assert(assert_string_equal(string, "hello world"));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user