dte test coverage


Directory: ./
File: src/util/string.h
Date: 2024-12-21 16:03:22
Exec Total Coverage
Lines: 23 23 100.0%
Functions: 7 7 100.0%
Branches: 4 4 100.0%

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