dte test coverage


Directory: ./
File: src/util/string.h
Date: 2025-07-05 20:19:48
Exec Total Coverage
Lines: 27 27 100.0%
Functions: 8 8 100.0%
Branches: 5 6 83.3%

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 "bit.h"
7 #include "macros.h"
8 #include "str-util.h"
9 #include "string-view.h"
10 #include "unicode.h"
11 #include "xmalloc.h"
12
13 typedef struct {
14 unsigned char NONSTRING *buffer;
15 size_t alloc;
16 size_t len;
17 } String;
18
19 #define STRING_INIT { \
20 .buffer = NULL, \
21 .alloc = 0, \
22 .len = 0 \
23 }
24
25 #define string_append_literal(s, x) string_append_buf(s, x, STRLEN(x))
26
27 void string_append_buf(String *s, const char *ptr, size_t len) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);
28
29 33153 static inline String string_new(size_t size)
30 {
31 33153 size = next_multiple(size, 16);
32 66306 return (String) {
33
2/2
✓ Branch 0 (2→3) taken 33149 times.
✓ Branch 1 (2→4) taken 4 times.
33153 .buffer = size ? xmalloc(size) : NULL,
34 .alloc = size,
35 .len = 0
36 };
37 }
38
39 2 static inline String string_new_from_buf(const char *buf, size_t len)
40 {
41 2 size_t alloc = next_multiple(len, 16);
42 4 return (String) {
43
1/2
✓ Branch 0 (2→3) taken 2 times.
✗ Branch 1 (2→5) not taken.
2 .buffer = len ? memcpy(xmalloc(alloc), buf, len) : NULL,
44 .alloc = alloc,
45 .len = len,
46 };
47 }
48
49 1 static inline void string_append_string(String *s1, const String *s2)
50 {
51 1 string_append_buf(s1, s2->buffer, s2->len);
52 1 }
53
54 1038 static inline void string_append_cstring(String *s, const char *cstr)
55 {
56 1038 string_append_buf(s, cstr, strlen(cstr));
57 1038 }
58
59 9778 static inline void string_append_strview(String *s, const StringView *sv)
60 {
61 9778 string_append_buf(s, sv->data, sv->length);
62 9778 }
63
64 5 static inline void string_replace_byte(String *s, char byte, char rep)
65 {
66
2/2
✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→4) taken 1 times.
5 if (s->len) {
67 4 strn_replace_byte(s->buffer, s->len, byte, rep);
68 }
69 5 }
70
71 2 static inline StringView strview_from_string(const String *s)
72 {
73 2 return string_view(s->buffer, s->len);
74 }
75
76 9221 static inline size_t string_clear(String *s)
77 {
78 9221 size_t oldlen = s->len;
79 9221 s->len = 0;
80 9221 return oldlen;
81 }
82
83 char *string_reserve_space(String *s, size_t more) NONNULL_ARGS_AND_RETURN;
84 void string_append_byte(String *s, unsigned char byte) NONNULL_ARGS;
85 size_t string_append_codepoint(String *s, CodePoint u) NONNULL_ARGS;
86 size_t string_insert_codepoint(String *s, size_t pos, CodePoint u) NONNULL_ARGS;
87 void string_insert_buf(String *s, size_t pos, const char *buf, size_t len) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(3, 4);
88 void string_append_memset(String *s, unsigned char byte, size_t len) NONNULL_ARGS;
89 void string_sprintf(String *s, const char *fmt, ...) PRINTF(2) NONNULL_ARGS;
90 char *string_steal_cstring(String *s) NONNULL_ARGS_AND_RETURN;
91 char *string_clone_cstring(const String *s) XSTRDUP;
92 const char *string_borrow_cstring(String *s) NONNULL_ARGS_AND_RETURN;
93 void string_remove(String *s, size_t pos, size_t len) NONNULL_ARGS;
94 void string_free(String *s) NONNULL_ARGS;
95
96 #endif
97