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_append_literal(s, x) string_append_buf(s, x, STRLEN(x))
25
26 size_t string_append_buf(String *s, const char *ptr, size_t len) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);
27
28 34405 static inline size_t string_size_round_up(size_t min_size)
29 {
30 34405 size_t size = next_multiple(min_size, STRING_ALLOC_MULTIPLE);
31 34405 return MAX(size, min_size); // See comment above next_multiple()
32 }
33
34 74 static inline size_t string_next_alloc_size(size_t alloc, size_t min_alloc)
35 {
36
2/2
✓ Branch 5 → 3 taken 176 times.
✓ Branch 5 → 6 taken 74 times.
250 while (alloc < min_alloc) {
37 176 alloc = string_size_round_up(xadd3(alloc, alloc / 2, 1));
38 }
39 74 return alloc;
40 }
41
42 34227 static inline String string_new(size_t min_size)
43 {
44 34227 size_t size = string_size_round_up(min_size);
45 68454 return (String) {
46
2/2
✓ Branch 2 → 3 taken 34135 times.
✓ Branch 2 → 4 taken 92 times.
34227 .buffer = size ? xmalloc(size) : NULL,
47 .alloc = size,
48 .len = 0
49 };
50 }
51
52 2 static inline String string_new_from_buf(const char *buf, size_t len)
53 {
54 2 size_t alloc = string_size_round_up(len);
55 4 return (String) {
56
1/2
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 5 not taken.
2 .buffer = len ? memcpy(xmalloc(alloc), buf, len) : NULL,
57 .alloc = alloc,
58 .len = len,
59 };
60 }
61
62 9 static inline size_t string_append_string(String *s1, const String *s2)
63 {
64 9 return string_append_buf(s1, s2->buffer, s2->len);
65 }
66
67 1083 static inline size_t string_append_cstring(String *s, const char *cstr)
68 {
69 1083 return string_append_buf(s, cstr, strlen(cstr));
70 }
71
72 11895 static inline size_t string_append_strview(String *s, StringView sv)
73 {
74 11895 return string_append_buf(s, sv.data, sv.length);
75 }
76
77 5 static inline void string_replace_byte(String *s, char byte, char rep)
78 {
79 5 strn_replace_byte(s->buffer, s->len, byte, rep);
80 5 }
81
82 262 static inline StringView strview_from_string(const String *s)
83 {
84 262 return string_view(s->buffer, s->len);
85 }
86
87 9447 static inline size_t string_clear(String *s)
88 {
89 9447 size_t oldlen = s->len;
90 9447 s->len = 0;
91 9447 return oldlen;
92 }
93
94 2 static inline bool string_has_prefix(const String *s, StringView prefix)
95 {
96 2 return strview_has_sv_prefix(strview_from_string(s), prefix);
97 }
98
99 3 static inline bool string_has_suffix(const String *s, StringView suffix)
100 {
101 3 return strview_has_sv_suffix(strview_from_string(s), suffix);
102 }
103
104 char *string_reserve_space(String *s, size_t more) NONNULL_ARGS_AND_RETURN;
105 void string_append_byte(String *s, unsigned char byte) NONNULL_ARGS;
106 size_t string_append_codepoint(String *s, CodePoint u) NONNULL_ARGS;
107 size_t string_insert_codepoint(String *s, size_t pos, CodePoint u) NONNULL_ARGS;
108 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);
109 size_t string_append_memset(String *s, unsigned char byte, size_t len) NONNULL_ARGS;
110 void string_sprintf(String *s, const char *fmt, ...) PRINTF(2) NONNULL_ARGS;
111 char *string_steal_cstring(String *s) NONNULL_ARGS_AND_RETURN WARN_UNUSED_RESULT;
112 char *string_clone_cstring(const String *s) XSTRDUP WARN_UNUSED_RESULT;
113 const char *string_borrow_cstring(String *s) NONNULL_ARGS_AND_RETURN WARN_UNUSED_RESULT;
114 void string_remove(String *s, size_t pos, size_t len) NONNULL_ARGS;
115 void string_free(String *s) NONNULL_ARGS;
116
117 #endif
118