Skip to content

File syn_fmt.h

File List > src > syntropic > util > syn_fmt.h

Go to the documentation of this file

#ifndef SYN_FMT_H
#define SYN_FMT_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

size_t syn_fmt_int(char *buf, size_t size, int32_t val);

size_t syn_fmt_uint(char *buf, size_t size, uint32_t val);

size_t syn_fmt_hex(char *buf, size_t size, uint32_t val, uint8_t min_digits);

size_t syn_fmt_hex_parse(const char *hex_str, uint8_t *out_bin, size_t max_bytes);

size_t syn_fmt_q16(char *buf, size_t size, int32_t q16_val, uint8_t frac_digits);

size_t syn_fmt_hexdump(char *buf, size_t size, const uint8_t *data, size_t len);

size_t syn_fmt_fixed(char *buf, size_t size, int32_t val, uint8_t places);

size_t syn_fmt_concat(char *buf, size_t size, const char *const *parts, size_t n);

static inline bool syn_str_prefix_icase(const char *str, const char *prefix)
{
    while (*prefix) {
        char a = *str++;
        char b = *prefix++;
        if (a >= 'A' && a <= 'Z')
            a = (char)(a + 32);
        if (b >= 'A' && b <= 'Z')
            b = (char)(b + 32);
        if (a != b)
            return false;
    }
    return true;
}

static inline uint32_t syn_fmt_parse_uint(const char *s)
{
    uint32_t val = 0;
    while (*s >= '0' && *s <= '9') {
        val = val * 10 + (uint32_t)(*s - '0');
        s++;
    }
    return val;
}

#ifdef __cplusplus
}
#endif

#endif /* SYN_FMT_H */