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 | 78 | static COLD void string_grow(String *s, size_t min_alloc) | |
11 | { | ||
12 | 78 | size_t alloc = s->alloc; | |
13 |
2/2✓ Branch 0 (4→3) taken 247 times.
✓ Branch 1 (4→5) taken 78 times.
|
325 | while (alloc < min_alloc) { |
14 | 247 | alloc = (alloc * 3 + 2) / 2; | |
15 | } | ||
16 | 78 | alloc = next_multiple(alloc, 16); | |
17 | 78 | s->alloc = alloc; | |
18 | 78 | s->buffer = xrealloc(s->buffer, alloc); | |
19 | 78 | } | |
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 | 239450 | char *string_reserve_space(String *s, size_t more) | |
25 | { | ||
26 | 239450 | BUG_ON(more == 0); | |
27 | 239450 | size_t min_alloc = xadd(s->len, more); | |
28 |
2/2✓ Branch 0 (5→6) taken 78 times.
✓ Branch 1 (5→7) taken 239372 times.
|
239450 | if (unlikely(s->alloc < min_alloc)) { |
29 | 78 | string_grow(s, min_alloc); | |
30 | } | ||
31 | 239450 | return s->buffer + s->len; | |
32 | } | ||
33 | |||
34 | 443 | void string_free(String *s) | |
35 | { | ||
36 | 443 | free(s->buffer); | |
37 | 443 | *s = (String) STRING_INIT; | |
38 | 443 | } | |
39 | |||
40 | 183720 | void string_append_byte(String *s, unsigned char byte) | |
41 | { | ||
42 | 183720 | string_reserve_space(s, 1); | |
43 | 183720 | s->buffer[s->len++] = byte; | |
44 | 183720 | } | |
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 0 (2→3) taken 1 times.
✓ Branch 1 (2→5) taken 1 times.
|
2 | if (len == 0) { |
74 | return; | ||
75 | } | ||
76 | 1 | memcpy(string_make_space(s, pos, len), buf, len); | |
77 | } | ||
78 | |||
79 | 13531 | void string_append_buf(String *s, const char *ptr, size_t len) | |
80 | { | ||
81 |
2/2✓ Branch 0 (2→3) taken 11779 times.
✓ Branch 1 (2→5) taken 1752 times.
|
13531 | if (len == 0) { |
82 | return; | ||
83 | } | ||
84 | 11779 | char *reserved = string_reserve_space(s, len); | |
85 | 11779 | s->len += len; | |
86 | 11779 | 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 0 (2→3) taken 1 times.
✓ Branch 1 (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 | 6 | static void string_vsprintf(String *s, const char *fmt, va_list ap) | |
101 | { | ||
102 | 6 | va_list ap2; | |
103 | 6 | va_copy(ap2, ap); | |
104 | // Calculate the required size | ||
105 | 6 | int n = vsnprintf(NULL, 0, fmt, ap2); | |
106 | 6 | va_end(ap2); | |
107 | 6 | BUG_ON(n < 0); | |
108 | 6 | string_reserve_space(s, n + 1); | |
109 | 6 | int wrote = vsnprintf(s->buffer + s->len, n + 1, fmt, ap); | |
110 | 6 | BUG_ON(wrote != n); | |
111 | 6 | s->len += wrote; | |
112 | 6 | } | |
113 | |||
114 | 6 | void string_sprintf(String *s, const char *fmt, ...) | |
115 | { | ||
116 | 6 | va_list ap; | |
117 | 6 | va_start(ap, fmt); | |
118 | 6 | string_vsprintf(s, fmt, ap); | |
119 | 6 | va_end(ap); | |
120 | 6 | } | |
121 | |||
122 | 41901 | static char *null_terminate(String *s) | |
123 | { | ||
124 | 41901 | string_reserve_space(s, 1); | |
125 | 41901 | s->buffer[s->len] = '\0'; | |
126 | 41901 | return s->buffer; | |
127 | } | ||
128 | |||
129 | 32759 | char *string_steal_cstring(String *s) | |
130 | { | ||
131 | 32759 | char *buf = null_terminate(s); | |
132 | 32759 | *s = (String) STRING_INIT; | |
133 | 32759 | return buf; | |
134 | } | ||
135 | |||
136 | 89 | char *string_clone_cstring(const String *s) | |
137 | { | ||
138 | 89 | size_t len = s->len; | |
139 | 89 | char *b = xmalloc(len + 1); | |
140 | 89 | b[len] = '\0'; | |
141 |
2/2✓ Branch 0 (3→4) taken 86 times.
✓ Branch 1 (3→5) taken 3 times.
|
89 | 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 | 9142 | const char *string_borrow_cstring(String *s) | |
155 | { | ||
156 | 9142 | return null_terminate(s); | |
157 | } | ||
158 | |||
159 | 9 | void string_remove(String *s, size_t pos, size_t len) | |
160 | { | ||
161 |
2/2✓ Branch 0 (2→3) taken 8 times.
✓ Branch 1 (2→6) taken 1 times.
|
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 |