diff --git a/sys/fmt/fmt.c b/sys/fmt/fmt.c index adffd46868..0d012ed1cc 100644 --- a/sys/fmt/fmt.c +++ b/sys/fmt/fmt.c @@ -53,6 +53,20 @@ static inline char _to_lower(char c) return 'a' + (c - 'A'); } +int fmt_is_number(const char *str) +{ + if (!str || !*str) { + return 0; + } + for (; *str; str++) { + if (!fmt_is_digit(*str)) { + return 0; + } + } + + return 1; +} + size_t fmt_byte_hex(char *out, uint8_t byte) { if (out) { diff --git a/sys/include/fmt.h b/sys/include/fmt.h index c201645593..8e89906ef8 100644 --- a/sys/include/fmt.h +++ b/sys/include/fmt.h @@ -69,6 +69,15 @@ static inline int fmt_is_upper(char c) return (c >= 'A' && c <= 'Z'); } +/** + * @brief Test if the given string is a number (regex `[0-9]+`) + * + * @param[in] str String to test, **must be `\0` terminated** + * + * @return true if @p str solely contains digits, false otherwise + */ +int fmt_is_number(const char *str); + /** * @brief Format a byte value as hex *