blob: 6abe4cc073429dbcbc5d7f2505c48cb9d302a567 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include "internal.h"
// grow ensures that the string can encompass at least delta bytes.
// if it already can, this is a no op.
// if it can't, the string will be reallocated.
void
string·grow(string *s, vlong delta)
{
Hdr *h, *newh;
vlong cap = string·cap(*s);
vlong len = string·len(*s);
assert(cap >= len); // To prevent unsigned behavior
if (cap - len >= delta) return;
h = (Hdr*)(*s - sizeof(Hdr));
vlong newCap = cap + delta;
assert(newCap >= cap); // To prevent unsigned behavior
if (newCap < MAX_STRING_ALLOC) {
newCap *= 2;
} else
newCap += MAX_STRING_ALLOC;
newh = (Hdr*)realloc(h, sizeof(*h) + newCap + 1);
if (newh == nil) return;
memset(newh->buf + len, '\0', newCap - len);
newh->cap = newCap;
newh->len = len;
*s = newh->buf;
}
|