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 
34 #ifndef FMT_H
35 #define FMT_H
36 
37 #include <stdint.h>
38 #include <stddef.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 #ifndef FMT_USE_MEMMOVE
45 #define FMT_USE_MEMMOVE (1)
46 #endif
47 
55 static inline int fmt_is_digit(char c)
56 {
57  return (c >= '0' && c <= '9');
58 }
59 
67 static inline int fmt_is_upper(char c)
68 {
69  return (c >= 'A' && c <= 'Z');
70 }
71 
79 int fmt_is_number(const char *str);
80 
95 size_t fmt_byte_hex(char *out, uint8_t byte);
96 
110 size_t fmt_bytes_hex(char *out, const uint8_t *ptr, size_t n);
111 
125 size_t fmt_bytes_hex_reverse(char *out, const uint8_t *ptr, size_t n);
126 
136 uint8_t fmt_hex_byte(const char *hex);
137 
153 size_t fmt_hex_bytes(uint8_t *out, const char *hex);
154 
167 size_t fmt_u16_hex(char *out, uint16_t val);
168 
181 size_t fmt_u32_hex(char *out, uint32_t val);
182 
195 size_t fmt_u64_hex(char *out, uint64_t val);
196 
208 size_t fmt_u32_dec(char *out, uint32_t val);
209 
223 size_t fmt_u64_dec(char *out, uint64_t val);
224 
236 size_t fmt_u16_dec(char *out, uint16_t val);
237 
251 size_t fmt_s64_dec(char *out, int64_t val);
252 
266 size_t fmt_s32_dec(char *out, int32_t val);
267 
281 size_t fmt_s16_dec(char *out, int16_t val);
282 
295 size_t fmt_s16_dfp(char *out, int16_t val, int fp_digits);
296 
322 size_t fmt_s32_dfp(char *out, int32_t val, int fp_digits);
323 
344 size_t fmt_float(char *out, float f, unsigned precision);
345 
357 size_t fmt_char(char *out, char c);
358 
366 size_t fmt_strlen(const char *str);
367 
377 size_t fmt_strnlen(const char *str, size_t maxlen);
378 
390 size_t fmt_str(char *out, const char *str);
391 
398 size_t fmt_to_lower(char *out, const char *str);
399 
410 uint32_t scn_u32_dec(const char *str, size_t n);
411 
422 uint32_t scn_u32_hex(const char *str, size_t n);
423 
432 void print(const char* s, size_t n);
433 
439 void print_u32_dec(uint32_t val);
440 
446 void print_s32_dec(int32_t val);
447 
453 void print_byte_hex(uint8_t byte);
454 
460 void print_u32_hex(uint32_t val);
461 
467 void print_u64_hex(uint64_t val);
468 
476 void print_u64_dec(uint64_t val);
477 
489 void print_float(float f, unsigned precision);
490 
496 void print_str(const char* str);
497 
521 size_t fmt_lpad(char *str, size_t in_len, size_t pad_len, char pad_char);
522 
523 #ifdef __cplusplus
524 }
525 #endif
526 
528 #endif /* FMT_H */
fmt_s32_dec
size_t fmt_s32_dec(char *out, int32_t val)
Convert a int32 value to decimal string.
fmt_hex_byte
uint8_t fmt_hex_byte(const char *hex)
Converts a sequence of two hex characters to a byte.
fmt_float
size_t fmt_float(char *out, float f, unsigned precision)
Format float to string.
fmt_lpad
size_t fmt_lpad(char *str, size_t in_len, size_t pad_len, char pad_char)
Pad string to the left.
fmt_s64_dec
size_t fmt_s64_dec(char *out, int64_t val)
Convert a int64 value to decimal string.
fmt_strnlen
size_t fmt_strnlen(const char *str, size_t maxlen)
Count at most maxlen characters until '\0' (exclusive) in str.
fmt_s16_dec
size_t fmt_s16_dec(char *out, int16_t val)
Convert a int16 value to decimal string.
fmt_bytes_hex
size_t fmt_bytes_hex(char *out, const uint8_t *ptr, size_t n)
Formats a sequence of bytes as hex bytes.
print_byte_hex
void print_byte_hex(uint8_t byte)
Print byte value as hex to stdout.
fmt_to_lower
size_t fmt_to_lower(char *out, const char *str)
Copy null-terminated string to a lowercase string (excluding terminating \0)
print_str
void print_str(const char *str)
Print null-terminated string to stdout.
fmt_char
size_t fmt_char(char *out, char c)
Copy in char to string (without terminating '\0')
fmt_strlen
size_t fmt_strlen(const char *str)
Count characters until '\0' (exclusive) in str.
print_float
void print_float(float f, unsigned precision)
Print float value.
fmt_is_digit
static int fmt_is_digit(char c)
Test if the given character is a numerical digit (regex [0-9])
Definition: fmt.h:55
fmt_s16_dfp
size_t fmt_s16_dfp(char *out, int16_t val, int fp_digits)
Convert 16-bit fixed point number to a decimal string.
print_u64_hex
void print_u64_hex(uint64_t val)
Print uint64 value as hex to stdout.
fmt_u32_dec
size_t fmt_u32_dec(char *out, uint32_t val)
Convert a uint32 value to decimal string.
print_u32_dec
void print_u32_dec(uint32_t val)
Print uint32 value to stdout.
fmt_bytes_hex_reverse
size_t fmt_bytes_hex_reverse(char *out, const uint8_t *ptr, size_t n)
Formats a sequence of bytes as hex bytes, starting with the last byte.
fmt_u16_dec
size_t fmt_u16_dec(char *out, uint16_t val)
Convert a uint16 value to decimal string.
fmt_hex_bytes
size_t fmt_hex_bytes(uint8_t *out, const char *hex)
Converts a sequence of hex bytes to an array of bytes.
fmt_u64_hex
size_t fmt_u64_hex(char *out, uint64_t val)
Convert a uint64 value to hex string.
print_u64_dec
void print_u64_dec(uint64_t val)
Print uint64 value as decimal to stdout.
fmt_is_upper
static int fmt_is_upper(char c)
Test if the given character is an uppercase letter (regex [A-Z])
Definition: fmt.h:67
fmt_u32_hex
size_t fmt_u32_hex(char *out, uint32_t val)
Convert a uint32 value to hex string.
print_u32_hex
void print_u32_hex(uint32_t val)
Print uint32 value as hex to stdout.
fmt_byte_hex
size_t fmt_byte_hex(char *out, uint8_t byte)
Format a byte value as hex.
fmt_u16_hex
size_t fmt_u16_hex(char *out, uint16_t val)
Convert a uint16 value to hex string.
fmt_u64_dec
size_t fmt_u64_dec(char *out, uint64_t val)
Convert a uint64 value to decimal string.
fmt_str
size_t fmt_str(char *out, const char *str)
Copy null-terminated string (excluding terminating \0)
scn_u32_hex
uint32_t scn_u32_hex(const char *str, size_t n)
Convert hexadecimal characters to uin32_t.
scn_u32_dec
uint32_t scn_u32_dec(const char *str, size_t n)
Convert digits to uint32.
fmt_s32_dfp
size_t fmt_s32_dfp(char *out, int32_t val, int fp_digits)
Convert 32-bit fixed point number to a decimal string.
fmt_is_number
int fmt_is_number(const char *str)
Test if the given string is a number (regex [0-9]+)
print_s32_dec
void print_s32_dec(int32_t val)
Print int32 value to stdout.
print
void print(const char *s, size_t n)
Print string to stdout.