1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-12-11 21:13:00 +01:00

feat: add string_zero_pad

This commit is contained in:
Théo LUDWIG 2024-09-12 12:31:58 +02:00
parent 35b868d0c1
commit c49d5f5421
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
4 changed files with 34 additions and 0 deletions

View File

@ -431,3 +431,12 @@ string_t string_pad_start(const string_t string, const string_t pad_string, size
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;
}

View File

@ -298,4 +298,16 @@ size_t string_last_position_of(const string_t string, const char character);
*/
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

View File

@ -28,6 +28,7 @@ void string_test() {
string_position_of_test();
string_last_position_of_test();
string_pad_start_test();
string_zero_pad_test();
}
void string_get_length_test() {
@ -322,3 +323,13 @@ void string_pad_start_test() {
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);
}

View File

@ -64,4 +64,6 @@ void string_last_position_of_test();
void string_pad_start_test();
void string_zero_pad_test();
#endif