-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.c
105 lines (90 loc) · 2.27 KB
/
str.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// (c) 2020 shdown
// This code is licensed under MIT license (see LICENSE.MIT for details)
#include "str.h"
#include "hash.h"
static String *unsafe_reallocate(String *s, size_t new_capacity)
{
size_t n;
if (UU_UNLIKELY(__builtin_add_overflow(new_capacity, sizeof(String), &n)))
goto oom;
s = realloc(s, n);
if (UU_UNLIKELY(!s))
goto oom;
s->capacity = new_capacity;
return s;
oom:
UU_PANIC_OOM();
}
String *string_new_with_capacity(const char *x, size_t nx, size_t capacity)
{
String *s = unsafe_reallocate(NULL, capacity);
s->gc_hdr = (GcHeader) {.nrefs = 1, .kind = VK_STR};
s->size = nx;
s->hash = hash_str(x, nx);
if (nx)
memcpy(s->data, x, nx);
return s;
}
String *string_hot_append_begin(String *s, size_t n)
{
// Calculate the minimum required capacity as (s->size + n).
size_t req_capacity;
if (UU_UNLIKELY(__builtin_add_overflow(s->size, n, &req_capacity)))
goto oom;
if (UU_UNLIKELY(req_capacity > SIZE_MAX / 2))
goto oom;
size_t c = s->capacity;
if (!c)
c = 1;
while (c < req_capacity)
c *= 2;
if (s->gc_hdr.nrefs == 1) {
if (c > s->capacity) {
s = unsafe_reallocate(s, c);
}
return s;
} else {
String *t = string_new_with_capacity(s->data, s->size, c);
value_unref((Value) s);
return t;
}
oom:
UU_PANIC_OOM();
}
char *string_hot_append_buf(String *s)
{
return s->data + s->size;
}
String *string_hot_append_end(String *s, size_t n)
{
s->hash = hash_str_concat(s->hash, s->data + s->size, n);
s->size += n;
return s;
}
int string_compare(String *s, String *t)
{
size_t ns = s->size;
size_t nt = t->size;
size_t nmin = ns < nt ? ns : nt;
if (nmin) {
int r = memcmp(s->data, t->data, nmin);
if (r != 0) {
return r < 0 ? COMPARE_LESS : COMPARE_GREATER;
}
}
if (ns < nt)
return COMPARE_LESS;
if (ns == nt)
return COMPARE_EQ;
return COMPARE_GREATER;
}
bool string_equal(String *s, String *t)
{
size_t ns = s->size;
size_t nt = t->size;
if (ns != nt)
return false;
if (s->hash != t->hash)
return false;
return ns == 0 || memcmp(s->data, t->data, ns) == 0;
}