Skip to content

Commit

Permalink
sys/fmt: move _is_digit and _is_upper to public API
Browse files Browse the repository at this point in the history
  • Loading branch information
haukepetersen committed Dec 5, 2019
1 parent 2b934de commit d9229af
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
18 changes: 4 additions & 14 deletions sys/fmt/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ static const uint32_t _tenmap[] = {

#define TENMAP_SIZE ARRAY_SIZE(_tenmap)

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');
Expand Down Expand Up @@ -437,7 +427,7 @@ size_t fmt_to_lower(char *out, const char *str)
size_t len = 0;

while (str && *str) {
if (_is_upper(*str)) {
if (fmt_is_upper(*str)) {
if (out) {
*out++ = _to_lower(*str);
}
Expand All @@ -457,7 +447,7 @@ uint32_t scn_u32_dec(const char *str, size_t n)
uint32_t res = 0;
while(n--) {
char c = *str++;
if (!_is_digit(c)) {
if (!fmt_is_digit(c)) {
break;
}
else {
Expand All @@ -474,8 +464,8 @@ uint32_t scn_u32_hex(const char *str, size_t n)

while (n--) {
char c = *str++;
if (!_is_digit(c)) {
if (_is_upper(c)) {
if (!fmt_is_digit(c)) {
if (fmt_is_upper(c)) {
c = _to_lower(c);
}
if (c == '\0' || c > 'f') {
Expand Down
24 changes: 24 additions & 0 deletions sys/include/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ extern "C" {
#define FMT_USE_MEMMOVE (1) /**< use memmove() or internal implementation */
#endif

/**
* @brief Test if the given character is a numerical digit (regex `[0-9]`)
*
* @param[in] c Character to test
*
* @return true if @p c is a digit, false otherwise
*/
static inline int fmt_is_digit(char c)
{
return (c >= '0' && c <= '9');
}

/**
* @brief Test if the given character is an uppercase letter (regex `[A-Z]`)
*
* @param[in] c Character to test
*
* @return true if @p c is an uppercase letter, false otherwise
*/
static inline int fmt_is_upper(char c)
{
return (c >= 'A' && c <= 'Z');
}

/**
* @brief Format a byte value as hex
*
Expand Down

0 comments on commit d9229af

Please sign in to comment.