Loading...
Searching...
No Matches
fmt.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Kaspar Schleiser <kaspar@schleiser.de>
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
39
40#include <stddef.h>
41#include <stdint.h>
42#include <time.h>
43#include <unistd.h>
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49#ifndef FMT_USE_MEMMOVE
50#define FMT_USE_MEMMOVE (1)
51#endif
52
60static inline int fmt_is_digit(char c)
61{
62 return (c >= '0' && c <= '9');
63}
64
72static inline int fmt_is_upper(char c)
73{
74 return (c >= 'A' && c <= 'Z');
75}
76
84int fmt_is_number(const char *str);
85
100size_t fmt_byte_hex(char *out, uint8_t byte);
101
115size_t fmt_bytes_hex(char *out, const uint8_t *ptr, size_t n);
116
130size_t fmt_bytes_hex_reverse(char *out, const uint8_t *ptr, size_t n);
131
141uint8_t fmt_hex_byte(const char *hex);
142
161size_t fmt_hex_bytes(uint8_t *out, const char *hex);
162
175size_t fmt_u16_hex(char *out, uint16_t val);
176
189size_t fmt_u32_hex(char *out, uint32_t val);
190
203size_t fmt_u64_hex(char *out, uint64_t val);
204
216size_t fmt_u16_dec(char *out, uint16_t val);
217
229size_t fmt_u32_dec(char *out, uint32_t val);
230
244size_t fmt_u64_dec(char *out, uint64_t val);
245
259size_t fmt_s64_dec(char *out, int64_t val);
260
274size_t fmt_s32_dec(char *out, int32_t val);
275
289size_t fmt_s16_dec(char *out, int16_t val);
290
302size_t fmt_s16_dfp(char *out, int16_t val, int scale);
303
321size_t fmt_s32_dfp(char *out, int32_t val, int scale);
322
343size_t fmt_float(char *out, float f, unsigned precision);
344
356size_t fmt_char(char *out, char c);
357
365size_t fmt_strlen(const char *str);
366
376size_t fmt_strnlen(const char *str, size_t maxlen);
377
389size_t fmt_str(char *out, const char *str);
390
402size_t fmt_to_lower(char *out, const char *str);
403
421int fmt_time_tm_iso8601(char out[20], const struct tm *tm, char separator);
422
433uint32_t scn_u32_dec(const char *str, size_t n);
434
445uint32_t scn_u32_hex(const char *str, size_t n);
446
472ssize_t scn_buf_hex(void *dest, size_t dest_len, const char *hex, size_t hex_len);
473
493int scn_time_tm_iso8601_date(struct tm *tm, const char *str);
494
514int scn_time_tm_iso8601_time(struct tm *tm, const char *str);
515
538int scn_time_tm_iso8601(struct tm *tm, const char *str, char separator);
539
548void print(const char* s, size_t n);
549
555void print_u32_dec(uint32_t val);
556
562void print_s32_dec(int32_t val);
563
569void print_byte_hex(uint8_t byte);
570
577void print_bytes_hex(const void *bytes, size_t n);
578
584void print_u32_hex(uint32_t val);
585
591void print_u64_hex(uint64_t val);
592
600void print_u64_dec(uint64_t val);
601
609void print_s64_dec(uint64_t val);
610
622void print_float(float f, unsigned precision);
623
629void print_str(const char* str);
630
654size_t fmt_lpad(char *str, size_t in_len, size_t pad_len, char pad_char);
655
656#ifdef __cplusplus
657}
658#endif
659
size_t fmt_byte_hex(char *out, uint8_t byte)
Format a byte value as hex.
int fmt_time_tm_iso8601(char out[20], const struct tm *tm, char separator)
Format a time structure to an ISO 8601 string.
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:60
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)
int scn_time_tm_iso8601(struct tm *tm, const char *str, char separator)
Convert an ISO 8601 string to time structure.
void print_u32_dec(uint32_t val)
Print uint32 value to stdout.
ssize_t scn_buf_hex(void *dest, size_t dest_len, const char *hex, size_t hex_len)
Convert a hex to binary.
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.
int scn_time_tm_iso8601_time(struct tm *tm, const char *str)
Convert an ISO 8601 date string to time structure.
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:72
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.
int scn_time_tm_iso8601_date(struct tm *tm, const char *str)
Convert an ISO 8601 time string to time structure.
size_t fmt_hex_bytes(uint8_t *out, const char *hex)
Converts a sequence of hex characters to an array of bytes.