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