dte test coverage


Directory: ./
File: src/util/string.h
Date: 2025-02-14 16:55: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) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);
27
28 26965 static inline String string_new(size_t size)
29 {
30 26965 size = next_multiple(size, 16);
31 53930 return (String) {
32
2/2
✓ Branch 0 (2→3) taken 26964 times.
✓ Branch 1 (2→4) taken 1 times.
26965 .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 623 static inline void string_append_cstring(String *s, const char *cstr)
44 {
45 623 string_append_buf(s, cstr, strlen(cstr));
46 623 }
47
48 8436 static inline void string_append_strview(String *s, const StringView *sv)
49 {
50 8436 string_append_buf(s, sv->data, sv->length);
51 8436 }
52
53 5 static inline void string_replace_byte(String *s, char byte, char rep)
54 {
55
2/2
✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→4) 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 7901 static inline size_t string_clear(String *s)
66 {
67 7901 size_t oldlen = s->len;
68 7901 s->len = 0;
69 7901 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) NONNULL_ARG_IF_NONZERO_LENGTH(3, 4);
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