Skip to content
Snippets Groups Projects
Commit 71455b69 authored by Vincent Dupont's avatar Vincent Dupont
Browse files

fmt: add fmt_to_lower()

parent a542e954
No related branches found
No related tags found
No related merge requests found
...@@ -53,6 +53,16 @@ static inline int _is_digit(char c) ...@@ -53,6 +53,16 @@ static inline int _is_digit(char c)
return (c >= '0' && c <= '9'); return (c >= '0' && c <= '9');
} }
static inline int _is_upper(char c)
{
return (c >= 'A' && c <= 'Z');
}
static inline char _to_lower(char c)
{
return 'a' + (c - 'A');
}
size_t fmt_byte_hex(char *out, uint8_t byte) size_t fmt_byte_hex(char *out, uint8_t byte)
{ {
if (out) { if (out) {
...@@ -405,6 +415,27 @@ size_t fmt_lpad(char *out, size_t in_len, size_t pad_len, char pad_char) ...@@ -405,6 +415,27 @@ size_t fmt_lpad(char *out, size_t in_len, size_t pad_len, char pad_char)
return pad_len; return pad_len;
} }
{
size_t fmt_to_lower(char *out, const char *str)
{
size_t len = 0;
while (str && *str) {
if (_is_upper(*str)) {
if (out) {
*out++ = _to_lower(*str);
}
}
else if (out) {
*out++ = *str;
}
str++;
len++;
}
return len;
}
uint32_t scn_u32_dec(const char *str, size_t n) uint32_t scn_u32_dec(const char *str, size_t n)
{ {
uint32_t res = 0; uint32_t res = 0;
......
...@@ -343,6 +343,14 @@ size_t fmt_strnlen(const char *str, size_t maxlen); ...@@ -343,6 +343,14 @@ size_t fmt_strnlen(const char *str, size_t maxlen);
*/ */
size_t fmt_str(char *out, const char *str); size_t fmt_str(char *out, const char *str);
/**
* @brief Copy null-terminated string to a lowercase string (excluding terminating \0)
*
* @param[out] out Pointer to output buffer, or NULL
* @param[in] str Pointer to null-terminated source string
*/
size_t fmt_to_lower(char *out, const char *str);
/** /**
* @brief Convert digits to uint32 * @brief Convert digits to uint32
* *
......
...@@ -749,6 +749,16 @@ static void test_fmt_str(void) ...@@ -749,6 +749,16 @@ static void test_fmt_str(void)
TEST_ASSERT_EQUAL_STRING(string1, &string2[0]); TEST_ASSERT_EQUAL_STRING(string1, &string2[0]);
} }
static void test_fmt_to_lower(void)
{
const char string_up[] = "AbCdeFGHijkLM";
char string[] = "zzzzzzzzzzzzzzz";
TEST_ASSERT_EQUAL_INT(fmt_strlen(string_up), fmt_to_lower(string, string_up));
string[fmt_strlen(string_up)] = '\0';
TEST_ASSERT_EQUAL_STRING("abcdefghijklm", &string[0]);
}
static void test_scn_u32_dec(void) static void test_scn_u32_dec(void)
{ {
const char *string1 = "123456789"; const char *string1 = "123456789";
...@@ -817,6 +827,7 @@ Test *tests_fmt_tests(void) ...@@ -817,6 +827,7 @@ Test *tests_fmt_tests(void)
new_TestFixture(test_fmt_strlen), new_TestFixture(test_fmt_strlen),
new_TestFixture(test_fmt_strnlen), new_TestFixture(test_fmt_strnlen),
new_TestFixture(test_fmt_str), new_TestFixture(test_fmt_str),
new_TestFixture(test_fmt_to_lower),
new_TestFixture(test_scn_u32_dec), new_TestFixture(test_scn_u32_dec),
new_TestFixture(test_fmt_lpad), new_TestFixture(test_fmt_lpad),
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment