-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstralloc.c
176 lines (135 loc) · 3.13 KB
/
stralloc.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "alloc.h"
#include "e.h"
#include "stralloc.h"
int stralloc_readyplus(stralloc *r, long long len) {
char *newdata;
long long i;
if (!r || len < 0) {
errno = EINVAL;
return 0;
}
if (len == 0) return 1;
if (r->len + len + 1 > r->alloc) {
while (r->len + len + 1 > r->alloc) r->alloc = 2 * r->alloc + 32;
newdata = alloc(r->alloc);
if (!newdata) return 0;
if (r->s) {
for (i = 0; i < r->len; ++i) newdata[i] = r->s[i];
alloc_free(r->s);
}
r->s = newdata;
}
return 1;
}
int stralloc_catb(stralloc *r, const void *xv, long long xlen) {
const char *x = xv;
long long i;
if (!r || !xv || xlen < 0) {
errno = EINVAL;
return 0;
}
if (xlen == 0) return 1;
if (!stralloc_readyplus(r, xlen)) return 0;
for (i = 0; i < xlen; ++i) r->s[r->len + i] = x[i];
r->len += xlen;
return 1;
}
int stralloc_cats(stralloc *r, const void *xv) {
const char *x = xv;
long long xlen;
if (!r || !xv) {
errno = EINVAL;
return 0;
}
for (xlen = 0; x[xlen]; ++xlen)
;
return stralloc_catb(r, x, xlen);
}
int stralloc_cat(stralloc *x, stralloc *y) {
if (!y) {
errno = EINVAL;
return 0;
}
return stralloc_catb(x, y->s, y->len);
}
int stralloc_copyb(stralloc *r, const void *xv, long long xlen) {
if (!r) {
errno = EINVAL;
return 0;
}
r->len = 0;
return stralloc_catb(r, xv, xlen);
}
int stralloc_copys(stralloc *r, const void *xv) {
if (!r) {
errno = EINVAL;
return 0;
}
r->len = 0;
return stralloc_cats(r, xv);
}
int stralloc_copy(stralloc *x, stralloc *y) {
if (!x || !y) {
errno = EINVAL;
return 0;
}
x->len = 0;
return stralloc_cat(x, y);
}
int stralloc_append(stralloc *r, const void *xv) {
return stralloc_catb(r, xv, 1);
}
int stralloc_0(stralloc *r) { return stralloc_append(r, ""); }
static int stralloc_catunum0(stralloc *sa, unsigned long long u, long long n) {
long long len;
unsigned long long q;
char *s;
if (!sa) {
errno = EINVAL;
return 0;
}
len = 1;
q = u;
while (q > 9) {
++len;
q /= 10;
}
if (len < n) len = n;
if (!stralloc_readyplus(sa, len)) return 0;
s = sa->s + sa->len;
sa->len += len;
while (len) {
s[--len] = '0' + (u % 10);
u /= 10;
}
return 1;
}
int stralloc_catnum0(stralloc *sa, long long l, long long n) {
if (!sa) {
errno = EINVAL;
return 0;
}
if (l < 0) {
if (!stralloc_append(sa, "-")) return 0;
l = -l;
}
return stralloc_catunum0(sa, l, n);
}
int stralloc_catnum(stralloc *r, long long num) {
return stralloc_catnum0(r, num, 0);
}
int stralloc_copynum(stralloc *r, long long num) {
if (!r) {
errno = EINVAL;
return 0;
}
r->len = 0;
return stralloc_catnum(r, num);
}
void stralloc_free(stralloc *r) {
if (!r) return;
if (r->s) alloc_free(r->s);
r->s = 0;
r->len = 0;
r->alloc = 0;
}