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