| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <stdarg.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include "string.h" | ||
| 5 | #include "arith.h" | ||
| 6 | #include "debug.h" | ||
| 7 | #include "utf8.h" | ||
| 8 | #include "xmalloc.h" | ||
| 9 | |||
| 10 | 81 | static COLD void string_grow(String *s, size_t min_alloc) | |
| 11 | { | ||
| 12 | 81 | size_t alloc = s->alloc; | |
| 13 |
2/2✓ Branch 4 → 3 taken 252 times.
✓ Branch 4 → 5 taken 81 times.
|
333 | while (alloc < min_alloc) { |
| 14 | 252 | alloc = (alloc * 3 + 2) / 2; | |
| 15 | } | ||
| 16 | 81 | alloc = next_multiple(alloc, 16); | |
| 17 | 81 | s->alloc = alloc; | |
| 18 | 81 | s->buffer = xrealloc(s->buffer, alloc); | |
| 19 | 81 | } | |
| 20 | |||
| 21 | // Reserve `more` bytes of additional space and return a pointer to the | ||
| 22 | // start of it, to allow writing directly to the buffer. Updating `s->len` | ||
| 23 | // to reflect the new length is left up to the caller. | ||
| 24 | 241462 | char *string_reserve_space(String *s, size_t more) | |
| 25 | { | ||
| 26 | 241462 | BUG_ON(more == 0); | |
| 27 | 241462 | size_t min_alloc = xadd(s->len, more); | |
| 28 |
2/2✓ Branch 5 → 6 taken 81 times.
✓ Branch 5 → 7 taken 241381 times.
|
241462 | if (unlikely(s->alloc < min_alloc)) { |
| 29 | 81 | string_grow(s, min_alloc); | |
| 30 | } | ||
| 31 | 241462 | return s->buffer + s->len; | |
| 32 | } | ||
| 33 | |||
| 34 | 447 | void string_free(String *s) | |
| 35 | { | ||
| 36 | 447 | free(s->buffer); | |
| 37 | 447 | *s = (String) STRING_INIT; | |
| 38 | 447 | } | |
| 39 | |||
| 40 | 185489 | void string_append_byte(String *s, unsigned char byte) | |
| 41 | { | ||
| 42 | 185489 | string_reserve_space(s, 1); | |
| 43 | 185489 | s->buffer[s->len++] = byte; | |
| 44 | 185489 | } | |
| 45 | |||
| 46 | 11 | size_t string_append_codepoint(String *s, CodePoint u) | |
| 47 | { | ||
| 48 | 11 | string_reserve_space(s, UTF8_MAX_SEQ_LEN); | |
| 49 | 11 | size_t n = u_set_char_raw(s->buffer + s->len, u); | |
| 50 | 11 | s->len += n; | |
| 51 | 11 | return n; | |
| 52 | } | ||
| 53 | |||
| 54 | 7 | static char *string_make_space(String *s, size_t pos, size_t len) | |
| 55 | { | ||
| 56 | 7 | BUG_ON(pos > s->len); | |
| 57 | 7 | BUG_ON(len == 0); | |
| 58 | 7 | string_reserve_space(s, len); | |
| 59 | 7 | char *start = s->buffer + pos; | |
| 60 | 7 | memmove(start + len, start, s->len - pos); | |
| 61 | 7 | s->len += len; | |
| 62 | 7 | return start; | |
| 63 | } | ||
| 64 | |||
| 65 | 6 | size_t string_insert_codepoint(String *s, size_t pos, CodePoint u) | |
| 66 | { | ||
| 67 | 6 | size_t len = u_char_size(u); | |
| 68 | 6 | return u_set_char_raw(string_make_space(s, pos, len), u); | |
| 69 | } | ||
| 70 | |||
| 71 | 2 | void string_insert_buf(String *s, size_t pos, const char *buf, size_t len) | |
| 72 | { | ||
| 73 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 5 taken 1 time.
|
2 | if (len == 0) { |
| 74 | return; | ||
| 75 | } | ||
| 76 | 1 | memcpy(string_make_space(s, pos, len), buf, len); | |
| 77 | } | ||
| 78 | |||
| 79 | 13650 | void string_append_buf(String *s, const char *ptr, size_t len) | |
| 80 | { | ||
| 81 |
2/2✓ Branch 2 → 3 taken 11892 times.
✓ Branch 2 → 5 taken 1758 times.
|
13650 | if (len == 0) { |
| 82 | return; | ||
| 83 | } | ||
| 84 | 11892 | char *reserved = string_reserve_space(s, len); | |
| 85 | 11892 | s->len += len; | |
| 86 | 11892 | memcpy(reserved, ptr, len); | |
| 87 | } | ||
| 88 | |||
| 89 | 3 | void string_append_memset(String *s, unsigned char byte, size_t len) | |
| 90 | { | ||
| 91 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 5 taken 2 times.
|
3 | if (len == 0) { |
| 92 | return; | ||
| 93 | } | ||
| 94 | 1 | char *reserved = string_reserve_space(s, len); | |
| 95 | 1 | s->len += len; | |
| 96 | 1 | memset(reserved, byte, len); | |
| 97 | } | ||
| 98 | |||
| 99 | VPRINTF(2) | ||
| 100 | 7 | static void string_vsprintf(String *s, const char *fmt, va_list ap) | |
| 101 | { | ||
| 102 | 7 | va_list ap2; | |
| 103 | 7 | va_copy(ap2, ap); | |
| 104 | // Calculate the required size | ||
| 105 | 7 | int n = vsnprintf(NULL, 0, fmt, ap2); | |
| 106 | 7 | va_end(ap2); | |
| 107 | 7 | BUG_ON(n < 0); | |
| 108 | 7 | string_reserve_space(s, n + 1); | |
| 109 | 7 | int wrote = vsnprintf(s->buffer + s->len, n + 1, fmt, ap); | |
| 110 | 7 | BUG_ON(wrote != n); | |
| 111 | 7 | s->len += wrote; | |
| 112 | 7 | } | |
| 113 | |||
| 114 | 7 | void string_sprintf(String *s, const char *fmt, ...) | |
| 115 | { | ||
| 116 | 7 | va_list ap; | |
| 117 | 7 | va_start(ap, fmt); | |
| 118 | 7 | string_vsprintf(s, fmt, ap); | |
| 119 | 7 | va_end(ap); | |
| 120 | 7 | } | |
| 121 | |||
| 122 | 42283 | static char *null_terminate(String *s) | |
| 123 | { | ||
| 124 | 42283 | string_reserve_space(s, 1); | |
| 125 | 42283 | s->buffer[s->len] = '\0'; | |
| 126 | 42283 | return s->buffer; | |
| 127 | } | ||
| 128 | |||
| 129 | 33101 | char *string_steal_cstring(String *s) | |
| 130 | { | ||
| 131 | 33101 | char *buf = null_terminate(s); | |
| 132 | 33101 | *s = (String) STRING_INIT; | |
| 133 | 33101 | return buf; | |
| 134 | } | ||
| 135 | |||
| 136 | 91 | char *string_clone_cstring(const String *s) | |
| 137 | { | ||
| 138 | 91 | size_t len = s->len; | |
| 139 | 91 | char *b = xmalloc(len + 1); | |
| 140 | 91 | b[len] = '\0'; | |
| 141 |
2/2✓ Branch 3 → 4 taken 88 times.
✓ Branch 3 → 5 taken 3 times.
|
91 | return likely(len) ? memcpy(b, s->buffer, len) : b; |
| 142 | } | ||
| 143 | |||
| 144 | /* | ||
| 145 | * This method first ensures that the String buffer is null-terminated | ||
| 146 | * and then returns a const pointer to it, without doing any copying. | ||
| 147 | * | ||
| 148 | * NOTE: the returned pointer only remains valid so long as no other | ||
| 149 | * methods are called on the String. There are no exceptions to this | ||
| 150 | * rule. If the buffer is realloc'd, the pointer will be dangling and | ||
| 151 | * using it will invoke undefined behaviour. If unsure, just use | ||
| 152 | * string_clone_cstring() instead. | ||
| 153 | */ | ||
| 154 | 9182 | const char *string_borrow_cstring(String *s) | |
| 155 | { | ||
| 156 | 9182 | return null_terminate(s); | |
| 157 | } | ||
| 158 | |||
| 159 | 9 | void string_remove(String *s, size_t pos, size_t len) | |
| 160 | { | ||
| 161 |
2/2✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 6 taken 1 time.
|
9 | if (len == 0) { |
| 162 | return; | ||
| 163 | } | ||
| 164 | 8 | size_t oldlen = s->len; | |
| 165 | 8 | BUG_ON(pos + len > oldlen); | |
| 166 | 8 | s->len -= len; | |
| 167 | 8 | memmove(s->buffer + pos, s->buffer + pos + len, oldlen - pos - len); | |
| 168 | } | ||
| 169 |