dte test coverage


Directory: ./
File: src/util/strtonum.h
Date: 2025-02-14 16:55:22
Exec Total Coverage
Lines: 19 19 100.0%
Functions: 4 4 100.0%
Branches: 10 10 100.0%

Line Branch Exec Source
1 #ifndef UTIL_STRTONUM_H
2 #define UTIL_STRTONUM_H
3
4 #include <limits.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include "macros.h"
9
10 extern const uint8_t hex_decode_table[256];
11
12 enum {
13 HEX_INVALID = 0xF0,
14 };
15
16 // Decodes a single, hexadecimal digit and returns a numerical value
17 // between 0-15, or HEX_INVALID for invalid digits
18 756 static inline unsigned int hex_decode(unsigned char c)
19 {
20 756 return hex_decode_table[c];
21 }
22
23 419 static inline bool ascii_isxdigit(unsigned char c)
24 {
25 419 return hex_decode(c) <= 0xF;
26 }
27
28 4 static inline size_t ascii_hex_prefix_length(const char *str, size_t len)
29 {
30 4 size_t i = 0;
31
4/4
✓ Branch 0 (4→5) taken 162 times.
✓ Branch 1 (4→6) taken 1 times.
✓ Branch 2 (5→3) taken 159 times.
✓ Branch 3 (5→6) taken 3 times.
163 while (i < len && ascii_isxdigit(str[i])) {
32 159 i++;
33 }
34 4 return i;
35 }
36
37 WARN_UNUSED_RESULT
38 59 static inline size_t buf_parse_hex_uint(const char *str, size_t len, unsigned int *valp)
39 {
40 59 unsigned int val = 0;
41 59 size_t i;
42
2/2
✓ Branch 0 (6→3) taken 184 times.
✓ Branch 1 (6→7) taken 50 times.
234 for (i = 0; i < len; i++) {
43 184 unsigned int x = hex_decode(str[i]);
44
2/2
✓ Branch 0 (3→4) taken 177 times.
✓ Branch 1 (3→7) taken 7 times.
184 if (unlikely(x > 0xF)) {
45 break;
46 }
47
2/2
✓ Branch 0 (4→5) taken 175 times.
✓ Branch 1 (4→8) taken 2 times.
177 if (unlikely(val > (UINT_MAX >> 4))) {
48 return 0; // Overflow
49 }
50 175 val = val << 4 | x;
51 }
52 57 *valp = val;
53 57 return i;
54 }
55
56 size_t size_str_width(size_t x) CONST_FN WARN_UNUSED_RESULT;
57 size_t buf_parse_uintmax(const char *str, size_t len, uintmax_t *valp) WARN_UNUSED_RESULT;
58 size_t buf_parse_ulong(const char *str, size_t len, unsigned long *valp) WARN_UNUSED_RESULT;
59 size_t buf_parse_uint(const char *str, size_t len, unsigned int *valp) WARN_UNUSED_RESULT;
60 size_t buf_parse_size(const char *str, size_t len, size_t *valp) WARN_UNUSED_RESULT;
61 bool str_to_int(const char *str, int *valp) NONNULL_ARGS WARN_UNUSED_RESULT;
62 bool str_to_uint(const char *str, unsigned int *valp) NONNULL_ARGS WARN_UNUSED_RESULT;
63 bool str_to_size(const char *str, size_t *valp) NONNULL_ARGS WARN_UNUSED_RESULT;
64 bool str_to_ulong(const char *str, unsigned long *valp) NONNULL_ARGS WARN_UNUSED_RESULT;
65 bool str_to_filepos(const char *str, size_t *linep, size_t *colp) NONNULL_ARGS WARN_UNUSED_RESULT;
66 bool str_to_xfilepos(const char *str, size_t *linep, size_t *colp) NONNULL_ARGS WARN_UNUSED_RESULT;
67 intmax_t parse_filesize(const char *str) NONNULL_ARGS WARN_UNUSED_RESULT;
68
69 #endif
70