mirror of
https://github.com/theoludwig/libcproject.git
synced 2024-12-11 21:13:00 +01:00
Compare commits
2 Commits
7683aa1db7
...
c49d5f5421
Author | SHA1 | Date | |
---|---|---|---|
c49d5f5421 | |||
35b868d0c1 |
33
lib/string.c
33
lib/string.c
@ -407,3 +407,36 @@ size_t string_last_position_of(const string_t string, const char character) {
|
|||||||
}
|
}
|
||||||
return position_found;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
string_t string_zero_pad(uint64_t number, size_t places) {
|
||||||
|
string_t number_string = convert_number_to_string((long long)number);
|
||||||
|
string_t pad_string = string_copy("0");
|
||||||
|
string_t result = string_pad_start(number_string, pad_string, places);
|
||||||
|
free(pad_string);
|
||||||
|
free(number_string);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
24
lib/string.h
24
lib/string.h
@ -286,4 +286,28 @@ 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);
|
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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Pad a number with zeros.
|
||||||
|
*
|
||||||
|
* @param number
|
||||||
|
* @param places
|
||||||
|
* @return string_t
|
||||||
|
* @example zero_pad(1, 2) // "01"
|
||||||
|
* @example zero_pad(10, 2) // "10"
|
||||||
|
* @since vTODO
|
||||||
|
*/
|
||||||
|
string_t string_zero_pad(uint64_t number, size_t places);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -27,6 +27,8 @@ void string_test() {
|
|||||||
string_ends_with_test();
|
string_ends_with_test();
|
||||||
string_position_of_test();
|
string_position_of_test();
|
||||||
string_last_position_of_test();
|
string_last_position_of_test();
|
||||||
|
string_pad_start_test();
|
||||||
|
string_zero_pad_test();
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_get_length_test() {
|
void string_get_length_test() {
|
||||||
@ -303,3 +305,31 @@ void string_last_position_of_test() {
|
|||||||
assert(string_last_position_of("abcdef", 'f') == 6);
|
assert(string_last_position_of("abcdef", 'f') == 6);
|
||||||
assert(string_last_position_of("abcdef", 'g') == 0);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void string_zero_pad_test() {
|
||||||
|
string_t result = string_zero_pad(1, 2);
|
||||||
|
assert(assert_string_equal(result, "01"));
|
||||||
|
free(result);
|
||||||
|
|
||||||
|
result = string_zero_pad(10, 2);
|
||||||
|
assert(assert_string_equal(result, "10"));
|
||||||
|
free(result);
|
||||||
|
}
|
||||||
|
@ -62,4 +62,8 @@ void string_position_of_test();
|
|||||||
|
|
||||||
void string_last_position_of_test();
|
void string_last_position_of_test();
|
||||||
|
|
||||||
|
void string_pad_start_test();
|
||||||
|
|
||||||
|
void string_zero_pad_test();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user