dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 34 / 0 / 34
Functions: 100.0% 12 / 0 / 12
Branches: 83.3% 5 / 0 / 6

src/util/string.h
Line Branch Exec Source
1 #ifndef UTIL_STRING_H
2 #define UTIL_STRING_H
3
4 #include <stddef.h>
5 #include <string.h>
6 #include "arith.h"
7 #include "bit.h"
8 #include "macros.h"
9 #include "str-util.h"
10 #include "string-view.h"
11 #include "unicode.h"
12 #include "xmalloc.h"
13
14 enum {
15 STRING_ALLOC_MULTIPLE = 16,
16 };
17
18 typedef struct {
19 char NONSTRING *buffer;
20 size_t alloc;
21 size_t len;
22 } String;
23
24 #define STRING_INIT { \
25 .buffer = NULL, \
26 .alloc = 0, \
27 .len = 0 \
28 }
29
30 #define string_append_literal(s, x) string_append_buf(s, x, STRLEN(x))
31
32 size_t string_append_buf(String *s, const char *ptr, size_t len) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);
33
34 34339 static inline size_t string_size_round_up(size_t min_size)
35 {
36 34339 size_t size = next_multiple(min_size, STRING_ALLOC_MULTIPLE);
37 34339 return MAX(size, min_size); // See comment above next_multiple()
38 }
39
40 115 static inline size_t string_next_alloc_size(size_t alloc, size_t min_alloc)
41 {
42
2/2
✓ Branch 5 → 3 taken 228 times.
✓ Branch 5 → 6 taken 115 times.
343 while (alloc < min_alloc) {
43 228 alloc = string_size_round_up(xadd3(alloc, alloc / 2, 1));
44 }
45 115 return alloc;
46 }
47
48 34109 static inline String string_new(size_t min_size)
49 {
50 34109 size_t size = string_size_round_up(min_size);
51 68218 return (String) {
52
2/2
✓ Branch 2 → 3 taken 34085 times.
✓ Branch 2 → 4 taken 24 times.
34109 .buffer = size ? xmalloc(size) : NULL,
53 .alloc = size,
54 .len = 0
55 };
56 }
57
58 2 static inline String string_new_from_buf(const char *buf, size_t len)
59 {
60 2 size_t alloc = string_size_round_up(len);
61 4 return (String) {
62
1/2
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 5 not taken.
2 .buffer = len ? memcpy(xmalloc(alloc), buf, len) : NULL,
63 .alloc = alloc,
64 .len = len,
65 };
66 }
67
68 9 static inline size_t string_append_string(String *s1, const String *s2)
69 {
70 9 return string_append_buf(s1, s2->buffer, s2->len);
71 }
72
73 1082 static inline size_t string_append_cstring(String *s, const char *cstr)
74 {
75 1082 return string_append_buf(s, cstr, strlen(cstr));
76 }
77
78 11896 static inline size_t string_append_strview(String *s, StringView sv)
79 {
80 11896 return string_append_buf(s, sv.data, sv.length);
81 }
82
83 5 static inline void string_replace_byte(String *s, char byte, char rep)
84 {
85 5 strn_replace_byte(s->buffer, s->len, byte, rep);
86 5 }
87
88 262 static inline StringView strview_from_string(const String *s)
89 {
90 262 return string_view(s->buffer, s->len);
91 }
92
93 9447 static inline size_t string_clear(String *s)
94 {
95 9447 size_t oldlen = s->len;
96 9447 s->len = 0;
97 9447 return oldlen;
98 }
99
100 2 static inline bool string_has_prefix(const String *s, StringView prefix)
101 {
102 2 return strview_has_sv_prefix(strview_from_string(s), prefix);
103 }
104
105 3 static inline bool string_has_suffix(const String *s, StringView suffix)
106 {
107 3 return strview_has_sv_suffix(strview_from_string(s), suffix);
108 }
109
110 char *string_reserve_space(String *s, size_t more) NONNULL_ARGS_AND_RETURN;
111 void string_append_byte(String *s, unsigned char byte) NONNULL_ARGS;
112 size_t string_append_codepoint(String *s, CodePoint u) NONNULL_ARGS;
113 size_t string_insert_codepoint(String *s, size_t pos, CodePoint u) NONNULL_ARGS;
114 size_t string_insert_buf(String *s, size_t pos, const char *buf, size_t len) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(3, 4);
115 size_t string_append_memset(String *s, unsigned char byte, size_t len) NONNULL_ARGS;
116 void string_sprintf(String *s, const char *fmt, ...) PRINTF(2) NONNULL_ARGS;
117 char *string_steal_cstring(String *s) NONNULL_ARGS_AND_RETURN WARN_UNUSED_RESULT;
118 char *string_clone_cstring(const String *s) XSTRDUP WARN_UNUSED_RESULT;
119 const char *string_borrow_cstring(String *s) NONNULL_ARGS_AND_RETURN WARN_UNUSED_RESULT;
120 void string_remove(String *s, size_t pos, size_t len) NONNULL_ARGS;
121 void string_free(String *s) NONNULL_ARGS;
122
123 #endif
124