Repository navigation
Expand file tree
/
Copy pathstring_builder.c
More file actions
238 lines (179 loc) · 4.63 KB
/
Copy pathstring_builder.c
File metadata and controls
238 lines (179 loc) · 4.63 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Licensed under the MIT License.
#ifndef STRING_BUILDER_4d4532d6f2db4b4088af8db287de600b
#define STRING_BUILDER_4d4532d6f2db4b4088af8db287de600b
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "string_builder.h"
Exception string_builder(StringBuilder instance, size_t capacity)
{
if (capacity < 4)
{
capacity = 4;
}
instance->buffer = malloc((capacity + 1) * sizeof instance->buffer);
if (!instance->buffer)
{
return EXCEPTION_OUT_OF_MEMORY;
}
instance->length = 0;
instance->capacity = capacity;
instance->buffer[0] = '\0';
return 0;
}
void string_builder_from_string(StringBuilder instance, String value)
{
instance->buffer = value;
instance->length = strlen(value);
instance->capacity = 0;
}
Exception string_builder_copy(StringBuilder result, StringBuilder instance)
{
result->buffer = malloc((instance->length + 1) * sizeof * result->buffer);
if (!result)
{
return EXCEPTION_OUT_OF_MEMORY;
}
memcpy(result->buffer, instance->buffer, instance->length);
result->buffer[instance->length] = '\0';
result->capacity = instance->length;
result->length = instance->length;
return 0;
}
Exception string_builder_ensure_capacity(
StringBuilder instance,
size_t capacity)
{
if (instance->capacity >= capacity)
{
return 0;
}
size_t newCapacity = (instance->capacity - 1) * 2;
if (capacity > newCapacity)
{
newCapacity = capacity;
}
char* newBuffer;
size_t newSize = (newCapacity + 1) * sizeof * newBuffer;
newBuffer = realloc(instance->buffer, newSize);
if (!newBuffer)
{
return EXCEPTION_OUT_OF_MEMORY;
}
newBuffer[instance->length] = '\0';
instance->capacity = newCapacity;
instance->buffer = newBuffer;
return 0;
}
Exception string_builder_append_char(StringBuilder instance, char item)
{
size_t newCount = instance->length + 1;
Exception ex = string_builder_ensure_capacity(instance, newCount);
if (ex)
{
return ex;
}
instance->buffer[instance->length] = item;
instance->length++;
instance->buffer[instance->length] = '\0';
return 0;
}
Exception string_builder_append_string(StringBuilder instance, String value)
{
size_t length = strlen(value);
size_t newCount = instance->length + length;
Exception ex = string_builder_ensure_capacity(instance, newCount);
if (ex)
{
return ex;
}
memcpy(instance->buffer + instance->length, value, length);
instance->length += length;
instance->buffer[instance->length] = '\0';
return 0;
}
Exception string_builder_append_format(
StringBuilder instance,
String format,
...)
{
va_list argl;
va_start(argl, format);
int size = vsnprintf(NULL, 0, format, argl);
va_end(argl);
if (size < 1)
{
return EXCEPTION_FORMAT;
}
size_t capacity = instance->length + size + 1;
Exception ex = string_builder_ensure_capacity(instance, capacity);
if (ex)
{
return ex;
}
va_start(argl, format);
int newSize = vsprintf(instance->buffer + instance->length, format, argl);
va_end(argl);
if (size != newSize)
{
return EXCEPTION_FORMAT;
}
instance->length += size;
return 0;
}
void string_builder_trim_right(StringBuilder instance)
{
for (size_t i = instance->length - 1; i != SIZE_MAX; i--)
{
if (isspace(instance->buffer[i]))
{
instance->buffer[i] = '\0';
instance->length--;
break;
}
}
}
void string_builder_clear(StringBuilder instance)
{
instance->length = 0;
}
String string_builder_to_string(StringBuilder instance)
{
String result = malloc((instance->length + 1) * sizeof * result);
if (!result)
{
return NULL;
}
memcpy(result, instance->buffer, instance->length);
result[instance->length] = '\0';
return result;
}
bool string_builder_equals(Object left, Object right)
{
StringBuilder p = left;
StringBuilder q = right;
if (p->length != q->length)
{
return false;
}
return memcmp(p->buffer, q->buffer, p->length) == 0;
}
size_t string_builder_hash(Object item, size_t size)
{
assert(size == sizeof(struct StringBuilder));
StringBuilder p = item;
return djb2_hash(p->buffer, p->length);
}
void finalize_string_builder(StringBuilder instance)
{
free(instance->buffer);
instance->buffer = NULL;
instance->length = 0;
instance->capacity = 0;
}
#endif