1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

fmt: add fmt_to_lower()

This commit is contained in:
Vincent Dupont 2018-08-23 14:00:32 +02:00
parent a542e954cf
commit 71455b692f
3 changed files with 50 additions and 0 deletions

View File

@ -53,6 +53,16 @@ static inline int _is_digit(char c)
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)
{
if (out) {
@ -405,6 +415,27 @@ size_t fmt_lpad(char *out, size_t in_len, size_t pad_len, char pad_char)
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 res = 0;

View File

@ -343,6 +343,14 @@ size_t fmt_strnlen(const char *str, size_t maxlen);
*/
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
*

View File

@ -749,6 +749,16 @@ static void test_fmt_str(void)
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)
{
const char *string1 = "123456789";
@ -817,6 +827,7 @@ Test *tests_fmt_tests(void)
new_TestFixture(test_fmt_strlen),
new_TestFixture(test_fmt_strnlen),
new_TestFixture(test_fmt_str),
new_TestFixture(test_fmt_to_lower),
new_TestFixture(test_scn_u32_dec),
new_TestFixture(test_fmt_lpad),
};