mirror of
https://github.com/theoludwig/libcproject.git
synced 2024-12-11 21:13:00 +01:00
feat: add string_pad_start
This commit is contained in:
parent
7683aa1db7
commit
35b868d0c1
24
lib/string.c
24
lib/string.c
@ -407,3 +407,27 @@ size_t string_last_position_of(const string_t string, const char character) {
|
||||
}
|
||||
return position_found;
|
||||
}
|
||||
|
||||
string_t string_pad_start(const string_t string, const string_t pad_string, size_t target_length) {
|
||||
string_t result = malloc(sizeof(char) * (target_length + 1));
|
||||
size_t initial_length = string_get_length(string);
|
||||
size_t left_length = target_length - initial_length;
|
||||
if (target_length <= initial_length) {
|
||||
left_length = 0;
|
||||
}
|
||||
size_t pad_length = string_get_length(pad_string);
|
||||
size_t count_pad_string = 0;
|
||||
size_t index_initial_string = 0;
|
||||
for (size_t index = 0; index < target_length; index++) {
|
||||
if (index < left_length) {
|
||||
size_t index_pad_string = count_pad_string % pad_length;
|
||||
result[index] = pad_string[index_pad_string];
|
||||
count_pad_string += 1;
|
||||
} else {
|
||||
result[index] = string[index_initial_string];
|
||||
index_initial_string += 1;
|
||||
}
|
||||
}
|
||||
result[target_length] = '\0';
|
||||
return result;
|
||||
}
|
||||
|
12
lib/string.h
12
lib/string.h
@ -286,4 +286,16 @@ size_t string_position_of(const string_t string, const char character);
|
||||
*/
|
||||
size_t string_last_position_of(const string_t string, const char character);
|
||||
|
||||
/**
|
||||
* @brief Pads a `string` with another `pad_string` (multiple times, if needed) until the resulting string reaches the `target_length`. The padding is applied from the start (left) of the string.
|
||||
*
|
||||
* @param string The string to pad.
|
||||
* @param pad_string The string to pad the current string with, to the left.
|
||||
* @param target_length
|
||||
* @return string_t
|
||||
* @example string_pad_start("hello", " ", 10) // " hello"
|
||||
* @since vTODO
|
||||
*/
|
||||
string_t string_pad_start(const string_t string, const string_t pad_string, size_t target_length);
|
||||
|
||||
#endif
|
||||
|
@ -27,6 +27,7 @@ void string_test() {
|
||||
string_ends_with_test();
|
||||
string_position_of_test();
|
||||
string_last_position_of_test();
|
||||
string_pad_start_test();
|
||||
}
|
||||
|
||||
void string_get_length_test() {
|
||||
@ -303,3 +304,21 @@ void string_last_position_of_test() {
|
||||
assert(string_last_position_of("abcdef", 'f') == 6);
|
||||
assert(string_last_position_of("abcdef", 'g') == 0);
|
||||
}
|
||||
|
||||
void string_pad_start_test() {
|
||||
string_t result = string_pad_start("hello", "ab", 10);
|
||||
assert(assert_string_equal(result, "ababahello"));
|
||||
free(result);
|
||||
|
||||
result = string_pad_start("hello", "ab", 4);
|
||||
assert(assert_string_equal(result, "hell"));
|
||||
free(result);
|
||||
|
||||
result = string_pad_start("hello", "ab", 5);
|
||||
assert(assert_string_equal(result, "hello"));
|
||||
free(result);
|
||||
|
||||
result = string_pad_start("hello", "ab", 6);
|
||||
assert(assert_string_equal(result, "ahello"));
|
||||
free(result);
|
||||
}
|
||||
|
@ -62,4 +62,6 @@ void string_position_of_test();
|
||||
|
||||
void string_last_position_of_test();
|
||||
|
||||
void string_pad_start_test();
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user