dte test coverage


Directory: ./
File: src/util/str-util.h
Date: 2025-05-08 15:05:54
Exec Total Coverage
Lines: 60 60 100.0%
Functions: 14 14 100.0%
Branches: 33 34 97.1%

Line Branch Exec Source
1 #ifndef UTIL_STR_UTIL_H
2 #define UTIL_STR_UTIL_H
3
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "debug.h"
9 #include "macros.h"
10 #include "string-view.h"
11 #include "xstring.h"
12
13 #define copyliteral(dest, lit) copystrn(dest, lit, STRLEN(lit))
14
15 894 static inline size_t copystrn(char *dest, const char *src, size_t len)
16 {
17 894 memcpy(dest, src, len);
18 894 return len;
19 }
20
21 // Like getenv(3), but also returning NULL for empty strings
22 25 static inline const char *xgetenv(const char *name)
23 {
24 25 const char *val = getenv(name);
25
3/4
✓ Branch 0 (3→4) taken 10 times.
✓ Branch 1 (3→5) taken 15 times.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 10 times.
25 return (!val || val[0] == '\0') ? NULL : val;
26 }
27
28 PURE NONNULL_ARGS
29 2077 static inline bool str_has_strn_prefix(const char *str, const char *prefix, size_t plen)
30 {
31 // Note that `prefix` must be a null-terminated string and `plen`
32 // should be equal to strlen(prefix). The explicit length parameter
33 // is simply for the sake of efficiency, e.g. to allow strlen() to
34 // be hoisted out of loops when `prefix` is loop-invariant.
35
4/4
✓ Branch 0 (2→3) taken 1530 times.
✓ Branch 1 (2→5) taken 547 times.
✓ Branch 2 (3→4) taken 1429 times.
✓ Branch 3 (3→5) taken 101 times.
2077 return plen == 0 || strncmp(str, prefix, plen) == 0;
36 }
37
38 PURE NONNULL_ARGS
39 209 static inline bool str_has_prefix(const char *str, const char *prefix)
40 {
41 209 return str_has_strn_prefix(str, prefix, strlen(prefix));
42 }
43
44 PURE NONNULL_ARGS
45 19 static inline bool str_has_suffix(const char *str, const char *suffix)
46 {
47 19 size_t l1 = strlen(str);
48 19 size_t l2 = strlen(suffix);
49
4/4
✓ Branch 0 (2→3) taken 15 times.
✓ Branch 1 (2→6) taken 4 times.
✓ Branch 2 (4→5) taken 8 times.
✓ Branch 3 (4→6) taken 7 times.
19 return (l1 >= l2) && mem_equal(str + l1 - l2, suffix, l2);
50 }
51
52 NONNULL_ARGS
53 128 static inline bool strn_has_strview_prefix(const char *s, size_t n, const StringView *p)
54 {
55
4/4
✓ Branch 0 (2→3) taken 80 times.
✓ Branch 1 (2→6) taken 48 times.
✓ Branch 2 (4→5) taken 77 times.
✓ Branch 3 (4→6) taken 3 times.
128 return n >= p->length && mem_equal(s, p->data, p->length);
56 }
57
58 NONNULL_ARGS
59 142 static inline size_t str_common_prefix_length(const char *a, const char *b)
60 {
61 142 size_t n = 0;
62
4/4
✓ Branch 0 (4→5) taken 3044 times.
✓ Branch 1 (4→6) taken 82 times.
✓ Branch 2 (5→3) taken 2984 times.
✓ Branch 3 (5→6) taken 60 times.
3126 while (a[n] && a[n] == b[n]) {
63 2984 n++;
64 }
65 142 return n;
66 }
67
68 // Replaces all occurrences of a specific byte with a replacement byte
69 // and returns the length of the string (like strlen(str))
70 NONNULL_ARGS
71 1 static inline size_t str_replace_byte(char *str, char byte, char replacement)
72 {
73 1 size_t n = 0;
74
2/2
✓ Branch 0 (6→3) taken 17 times.
✓ Branch 1 (6→7) taken 1 times.
18 for (char c; (c = str[n]); n++) {
75
2/2
✓ Branch 0 (3→4) taken 10 times.
✓ Branch 1 (3→5) taken 7 times.
17 if (c == byte) {
76 10 str[n] = replacement;
77 }
78 }
79 1 return n;
80 }
81
82 NONNULL_ARGS
83 5 static inline void strn_replace_byte(char *str, size_t n, char byte, char rep)
84 {
85
2/2
✓ Branch 0 (6→3) taken 70 times.
✓ Branch 1 (6→7) taken 5 times.
75 for (size_t i = 0; i < n; i++) {
86
2/2
✓ Branch 0 (3→4) taken 23 times.
✓ Branch 1 (3→5) taken 47 times.
70 if (str[i] == byte) {
87 23 str[i] = rep;
88 }
89 }
90 5 }
91
92 // Extract a substring between `buf + pos` and either the next `delim`
93 // byte (if found) or `buf + size` (the remainder of the string). The
94 // substring is returned as a StringView and the `posp` in-out param
95 // is set to the offset one byte after the found delimiter (or to the
96 // end of the size-bounded string, if no delimiter was found).
97 NONNULL_ARGS READONLY(1, 3) READWRITE(2)
98 10890 static inline StringView get_delim(const char *buf, size_t *posp, size_t size, int delim)
99 {
100 10890 size_t pos = *posp;
101 10890 BUG_ON(pos >= size);
102 10890 size_t len = size - pos;
103 10890 const char *start = buf + pos;
104 10890 const char *found = memchr(start, delim, len);
105
2/2
✓ Branch 0 (4→5) taken 10547 times.
✓ Branch 1 (4→6) taken 343 times.
10890 len = found ? (size_t)(found - start) : len;
106 10890 size_t delim_len = found ? 1 : 0;
107 10890 *posp += len + delim_len;
108 10890 return string_view(start, len);
109 }
110
111 // Similar to get_delim(), but returning a null-terminated substring
112 // instead of a StringView, by mutating the contents of `buf` (i.e.
113 // replacing the delimiter with a NUL byte). Using get_delim() should
114 // be preferred, unless the substring specifically needs to be
115 // null-terminated (e.g. for passing to a library function).
116 NONNULL_ARGS
117 871 static inline char *get_delim_str(char *buf, size_t *posp, size_t size, int delim)
118 {
119 871 char *substr = buf + *posp;
120 871 StringView sv = get_delim(buf, posp, size, delim);
121
122 // If no delimiter was found, this writes the null-terminator 1 byte
123 // beyond the `size` bound. Callers must ensure this is safe to do.
124 // Thus, when calling this function (perhaps repeatedly) to consume
125 // an entire string, either buf[size-1] must be a delim byte or
126 // buf[size] must be in-bounds, writable memory.
127 871 substr[sv.length] = '\0';
128 871 return substr;
129 }
130
131 NONNULL_ARGS
132 9095 static inline StringView buf_slice_next_line(const char *buf, size_t *posp, size_t size)
133 {
134 9095 return get_delim(buf, posp, size, '\n');
135 }
136
137 NONNULL_ARGS
138 572 static inline char *buf_next_line(char *buf, size_t *posp, size_t size)
139 {
140 572 return get_delim_str(buf, posp, size, '\n');
141 }
142
143 NONNULL_ARGS
144 915 static inline size_t count_nl(const char *buf, size_t size)
145 {
146 915 const char *end = buf + size;
147 915 size_t nl = 0;
148
2/2
✓ Branch 0 (5→3) taken 3166 times.
✓ Branch 1 (5→6) taken 711 times.
3877 while (buf < end) {
149 3166 buf = memchr(buf, '\n', end - buf);
150
2/2
✓ Branch 0 (3→4) taken 2962 times.
✓ Branch 1 (3→6) taken 204 times.
3166 if (!buf) {
151 break;
152 }
153 2962 buf++;
154 2962 nl++;
155 }
156 915 return nl;
157 }
158
159 #endif
160