dte test coverage


Directory: ./
File: src/util/numtostr.h
Date: 2025-09-07 23:01:39
Exec Total Coverage
Lines: 12 12 100.0%
Functions: 2 2 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #ifndef UTIL_NUMTOSTR_H
2 #define UTIL_NUMTOSTR_H
3
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <sys/types.h>
7 #include "macros.h"
8
9 enum {
10 HRSIZE_MAX = DECIMAL_STR_MAX(uintmax_t) + STRLEN(".99 GiB"),
11 FILESIZE_STR_MAX = HRSIZE_MAX + DECIMAL_STR_MAX(uintmax_t) + STRLEN(" ()"),
12 PRECISE_FILESIZE_STR_MAX = DECIMAL_STR_MAX(uintmax_t) + STRLEN("GiB"),
13 };
14
15 extern const char hextable[32];
16
17 // Encode a uint8_t as a string of 2 hexadecimal digits
18 45 static inline size_t hex_encode_byte(char out[static 2], uint8_t byte)
19 {
20 45 const char *hextab_lower = hextable + 16;
21 45 out[0] = hextab_lower[byte >> 4];
22 45 out[1] = hextab_lower[byte & 0xF];
23 45 return 2;
24 }
25
26 // Encode a uint8_t as a string of 1-3 decimal digits
27 37 static inline size_t buf_u8_to_str(uint8_t x, char *buf)
28 {
29 // Write 3 digits unconditionally, but adjust the indices to produce
30 // the correct string (x=1 writes to `buf[0]` 3 times)
31 37 size_t i = (x >= 100);
32 37 size_t j = (x >= 10) + i;
33 37 buf[0] = '0' + ((x / 100) % 10);
34 37 buf[i] = '0' + ((x / 10) % 10);
35 37 buf[j] = '0' + (x % 10);
36 37 return j + 1;
37 }
38
39 size_t buf_umax_to_str(uintmax_t x, char *buf) NONNULL_ARGS;
40 size_t buf_umax_to_hex_str(uintmax_t x, char *buf, size_t min_digits) NONNULL_ARGS;
41 size_t buf_uint_to_str(unsigned int x, char *buf) NONNULL_ARGS;
42 const char *umax_to_str(uintmax_t x) RETURNS_NONNULL;
43 const char *uint_to_str(unsigned int x) RETURNS_NONNULL;
44 const char *ulong_to_str(unsigned long x) RETURNS_NONNULL;
45 char *file_permissions_to_str(mode_t mode, char buf[static 10]) NONNULL_ARGS_AND_RETURN;
46 char *human_readable_size(uintmax_t bytes, char buf[static HRSIZE_MAX]) NONNULL_ARGS_AND_RETURN;
47 char *filesize_to_str(uintmax_t bytes, char buf[static FILESIZE_STR_MAX]) NONNULL_ARGS_AND_RETURN;
48 char *filesize_to_str_precise(uintmax_t bytes, char buf[static PRECISE_FILESIZE_STR_MAX]) NONNULL_ARGS_AND_RETURN;
49
50 #endif
51