Loading...
Searching...
No Matches
fmt.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
41#ifndef FMT_H
42#define FMT_H
43
44#include <stdint.h>
45#include <stddef.h>
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51#ifndef FMT_USE_MEMMOVE
52#define FMT_USE_MEMMOVE (1)
53#endif
54
62static inline int fmt_is_digit(char c)
63{
64 return (c >= '0' && c <= '9');
65}
66
74static inline int fmt_is_upper(char c)
75{
76 return (c >= 'A' && c <= 'Z');
77}
78
86int fmt_is_number(const char *str);
87
102size_t fmt_byte_hex(char *out, uint8_t byte);
103
117size_t fmt_bytes_hex(char *out, const uint8_t *ptr, size_t n);
118
132size_t fmt_bytes_hex_reverse(char *out, const uint8_t *ptr, size_t n);
133
143uint8_t fmt_hex_byte(const char *hex);
144
163size_t fmt_hex_bytes(uint8_t *out, const char *hex);
164
177size_t fmt_u16_hex(char *out, uint16_t val);
178
191size_t fmt_u32_hex(char *out, uint32_t val);
192
205size_t fmt_u64_hex(char *out, uint64_t val);
206
218size_t fmt_u16_dec(char *out, uint16_t val);
219
231size_t fmt_u32_dec(char *out, uint32_t val);
232
246size_t fmt_u64_dec(char *out, uint64_t val);
247
261size_t fmt_s64_dec(char *out, int64_t val);
262
276size_t fmt_s32_dec(char *out, int32_t val);
277
291size_t fmt_s16_dec(char *out, int16_t val);
292
304size_t fmt_s16_dfp(char *out, int16_t val, int scale);
305
323size_t fmt_s32_dfp(char *out, int32_t val, int scale);
324
345size_t fmt_float(char *out, float f, unsigned precision);
346
358size_t fmt_char(char *out, char c);
359
367size_t fmt_strlen(const char *str);
368
378size_t fmt_strnlen(const char *str, size_t maxlen);
379
391size_t fmt_str(char *out, const char *str);
392
404size_t fmt_to_lower(char *out, const char *str);
405
416uint32_t scn_u32_dec(const char *str, size_t n);
417
428uint32_t scn_u32_hex(const char *str, size_t n);
429
438void print(const char* s, size_t n);
439
445void print_u32_dec(uint32_t val);
446
452void print_s32_dec(int32_t val);
453
459void print_byte_hex(uint8_t byte);
460
467void print_bytes_hex(const void *bytes, size_t n);
468
474void print_u32_hex(uint32_t val);
475
481void print_u64_hex(uint64_t val);
482
490void print_u64_dec(uint64_t val);
491
499void print_s64_dec(uint64_t val);
500
512void print_float(float f, unsigned precision);
513
519void print_str(const char* str);
520
544size_t fmt_lpad(char *str, size_t in_len, size_t pad_len, char pad_char);
545
546#ifdef __cplusplus
547}
548#endif
549
551#endif /* FMT_H */
size_t fmt_byte_hex(char *out, uint8_t byte)
Format a byte value as hex.
size_t fmt_strlen(const char *str)
Count characters until '\0' (exclusive) in str.
static int fmt_is_digit(char c)
Test if the given character is a numerical digit (regex [0-9])
Definition fmt.h:62
void print_byte_hex(uint8_t byte)
Print byte value as hex to stdout.
size_t fmt_s16_dfp(char *out, int16_t val, int scale)
Convert 16-bit fixed point number to a decimal string.
size_t fmt_char(char *out, char c)
Copy in char to string (without terminating '\0')
size_t fmt_lpad(char *str, size_t in_len, size_t pad_len, char pad_char)
Pad string to the left.
uint8_t fmt_hex_byte(const char *hex)
Converts a sequence of two hex characters to a byte.
size_t fmt_u64_hex(char *out, uint64_t val)
Convert a uint64 value to hex string.
size_t fmt_u32_dec(char *out, uint32_t val)
Convert a uint32 value to decimal string.
size_t fmt_strnlen(const char *str, size_t maxlen)
Count at most maxlen characters until '\0' (exclusive) in str.
void print_s64_dec(uint64_t val)
Print int64 value as decimal to stdout.
size_t fmt_s32_dfp(char *out, int32_t val, int scale)
Convert 32-bit fixed point number to a decimal string.
void print_s32_dec(int32_t val)
Print int32 value to stdout.
size_t fmt_s16_dec(char *out, int16_t val)
Convert a int16 value to decimal string.
size_t fmt_s32_dec(char *out, int32_t val)
Convert a int32 value to decimal string.
size_t fmt_bytes_hex_reverse(char *out, const uint8_t *ptr, size_t n)
Formats a sequence of bytes as hex characters, starting with the last byte.
size_t fmt_to_lower(char *out, const char *str)
Copy null-terminated string to a lowercase string (excluding terminating \0)
void print_u32_dec(uint32_t val)
Print uint32 value to stdout.
size_t fmt_float(char *out, float f, unsigned precision)
Format float to string.
size_t fmt_u32_hex(char *out, uint32_t val)
Convert a uint32 value to hex string.
size_t fmt_u16_dec(char *out, uint16_t val)
Convert a uint16 value to decimal string.
int fmt_is_number(const char *str)
Test if the given string is a number (regex [0-9]+)
void print_u64_hex(uint64_t val)
Print uint64 value as hex to stdout.
void print_u64_dec(uint64_t val)
Print uint64 value as decimal to stdout.
void print_bytes_hex(const void *bytes, size_t n)
Print bytes as hex to stdout.
void print_float(float f, unsigned precision)
Print float value.
size_t fmt_str(char *out, const char *str)
Copy null-terminated string (excluding terminating \0)
size_t fmt_bytes_hex(char *out, const uint8_t *ptr, size_t n)
Formats a sequence of bytes as hex characters.
void print_u32_hex(uint32_t val)
Print uint32 value as hex to stdout.
size_t fmt_s64_dec(char *out, int64_t val)
Convert a int64 value to decimal string.
void print_str(const char *str)
Print null-terminated string to stdout.
size_t fmt_u16_hex(char *out, uint16_t val)
Convert a uint16 value to hex string.
uint32_t scn_u32_dec(const char *str, size_t n)
Convert string of decimal digits to uint32.
static int fmt_is_upper(char c)
Test if the given character is an uppercase letter (regex [A-Z])
Definition fmt.h:74
size_t fmt_u64_dec(char *out, uint64_t val)
Convert a uint64 value to decimal string.
void print(const char *s, size_t n)
Print string to stdout.
uint32_t scn_u32_hex(const char *str, size_t n)
Convert string hexadecimal digits to uin32_t.
size_t fmt_hex_bytes(uint8_t *out, const char *hex)
Converts a sequence of hex characters to an array of bytes.