From 35b868d0c12bc5f04c3c95c5b2a2e6e3b0d3b448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Thu, 12 Sep 2024 12:22:53 +0200 Subject: [PATCH] feat: add `string_pad_start` --- lib/string.c | 24 ++++++++++++++++++++++++ lib/string.h | 12 ++++++++++++ test/string_test.c | 19 +++++++++++++++++++ test/string_test.h | 2 ++ 4 files changed, 57 insertions(+) diff --git a/lib/string.c b/lib/string.c index 653602b..c4da49d 100644 --- a/lib/string.c +++ b/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; +} diff --git a/lib/string.h b/lib/string.h index e4e2b00..895ad09 100644 --- a/lib/string.h +++ b/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 diff --git a/test/string_test.c b/test/string_test.c index 3998a5d..0590b97 100644 --- a/test/string_test.c +++ b/test/string_test.c @@ -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); +} diff --git a/test/string_test.h b/test/string_test.h index c5109fc..f3e44b9 100644 --- a/test/string_test.h +++ b/test/string_test.h @@ -62,4 +62,6 @@ void string_position_of_test(); void string_last_position_of_test(); +void string_pad_start_test(); + #endif