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 | 28511 | static inline String string_new(size_t size) | |
30 | { | ||
31 | 28511 | size = next_multiple(size, 16); | |
32 | 57022 | return (String) { | |
33 |
2/2✓ Branch 0 (2→3) taken 28510 times.
✓ Branch 1 (2→4) taken 1 times.
|
28511 | .buffer = size ? xmalloc(size) : NULL, |
34 | .alloc = size, | ||
35 | .len = 0 | ||
36 | }; | ||
37 | } | ||
38 | |||
39 | 1 | static inline void string_append_string(String *s1, const String *s2) | |
40 | { | ||
41 | 1 | string_append_buf(s1, s2->buffer, s2->len); | |
42 | 1 | } | |
43 | |||
44 | 627 | static inline void string_append_cstring(String *s, const char *cstr) | |
45 | { | ||
46 | 627 | string_append_buf(s, cstr, strlen(cstr)); | |
47 | 627 | } | |
48 | |||
49 | 8850 | static inline void string_append_strview(String *s, const StringView *sv) | |
50 | { | ||
51 | 8850 | string_append_buf(s, sv->data, sv->length); | |
52 | 8850 | } | |
53 | |||
54 | 5 | static inline void string_replace_byte(String *s, char byte, char rep) | |
55 | { | ||
56 |
2/2✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→4) taken 1 times.
|
5 | if (s->len) { |
57 | 4 | strn_replace_byte(s->buffer, s->len, byte, rep); | |
58 | } | ||
59 | 5 | } | |
60 | |||
61 | 2 | static inline StringView strview_from_string(const String *s) | |
62 | { | ||
63 | 2 | return string_view(s->buffer, s->len); | |
64 | } | ||
65 | |||
66 | 8314 | static inline size_t string_clear(String *s) | |
67 | { | ||
68 | 8314 | size_t oldlen = s->len; | |
69 | 8314 | s->len = 0; | |
70 | 8314 | return oldlen; | |
71 | } | ||
72 | |||
73 | char *string_reserve_space(String *s, size_t more) NONNULL_ARGS_AND_RETURN; | ||
74 | void string_append_byte(String *s, unsigned char byte) NONNULL_ARGS; | ||
75 | size_t string_append_codepoint(String *s, CodePoint u) NONNULL_ARGS; | ||
76 | size_t string_insert_codepoint(String *s, size_t pos, CodePoint u) NONNULL_ARGS; | ||
77 | 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); | ||
78 | void string_append_memset(String *s, unsigned char byte, size_t len) NONNULL_ARGS; | ||
79 | void string_sprintf(String *s, const char *fmt, ...) PRINTF(2) NONNULL_ARGS; | ||
80 | char *string_steal_cstring(String *s) NONNULL_ARGS_AND_RETURN; | ||
81 | char *string_clone_cstring(const String *s) XSTRDUP; | ||
82 | const char *string_borrow_cstring(String *s) NONNULL_ARGS_AND_RETURN; | ||
83 | void string_remove(String *s, size_t pos, size_t len) NONNULL_ARGS; | ||
84 | void string_free(String *s) NONNULL_ARGS; | ||
85 | |||
86 | #endif | ||
87 |