-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringbuilder.c
More file actions
232 lines (209 loc) · 7.21 KB
/
Copy pathstringbuilder.c
File metadata and controls
232 lines (209 loc) · 7.21 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* MIT License *
* *
* Copyright (c) 2020 Anotra *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include "stringbuilder.h"
stringbuilder *
stringbuilder_create(void) {
stringbuilder *sb = calloc(1, sizeof *sb);
if (sb) sb->free_on_destroy = true;
return sb;
}
void
stringbuilder_destroy(stringbuilder *sb) {
bool free_on_destroy = sb->free_on_destroy;
free(sb->string);
memset(sb, 0, sizeof *sb);
if (free_on_destroy)
free(sb);
}
static bool
increase_size(stringbuilder *sb, size_t space_needed) {
size_t new_capacity = 0x100;
for (; new_capacity - 1 < space_needed; new_capacity <<= 1)
if (!new_capacity) return false;
if (sb->capacity_max && new_capacity - 1 > sb->capacity_max) {
if (sb->capacity_max >= space_needed) {
new_capacity = sb->capacity_max + 1;
} else return false;
}
void *tmp = sb->string ? realloc(sb->string, new_capacity * sizeof *sb->string)
: calloc(new_capacity, sizeof *sb->string);
if (tmp) {
sb->capacity = new_capacity - 1;
sb->string = tmp;
}
return (bool)tmp;
}
static inline bool
ensure_space(stringbuilder *sb, size_t space_needed) {
space_needed += sb->length;
if (sb->capacity_max && space_needed > sb->capacity_max)
return false;
if (sb->capacity < space_needed || !sb->string)
return increase_size(sb, space_needed);
return true;
}
size_t
stringbuilder_length(stringbuilder *sb) {
return sb->length;
}
bool
stringbuilder_set_length(stringbuilder *sb, const size_t length) {
if (sb->length >= length) {
sb->length = length;
sb->string[sb->length] = '\0';
return true;
}
return false;
}
void
stringbuilder_reset(stringbuilder *sb) {
sb->length = 0;
if (sb->string)
sb->string[0] = '\0';
}
void
stringbuilder_set_max_capacity(stringbuilder *sb, size_t capacity) {
sb->capacity_max = capacity;
}
size_t
stringbuilder_max_capacity(stringbuilder *sb) {
return sb->capacity_max;
}
bool
stringbuilder_delete(stringbuilder *sb, const size_t position, const size_t length) {
if ((position + length) < sb->length) {
if (length)
memmove(&sb->string[position], &sb->string[position + length], (sb->length - position + 1) * sizeof *sb->string);
sb->length -= length;
return true;
}
return false;
}
bool
stringbuilder_insertl(stringbuilder *sb, const size_t position, const char *string, const size_t length) {
if (ensure_space(sb, length)) {
if (position > sb->length) {
return false;
} else if (position < sb->length) {
memmove(&sb->string[position + length], &sb->string[position], (sb->length - position) * sizeof *sb->string);
}
memcpy(&sb->string[position], string, length * sizeof *string);
sb->length += length;
sb->string[sb->length] = '\0';
return true;
}
return false;
}
bool
stringbuilder_appendl(stringbuilder *sb, const char *string, const size_t length) {
return stringbuilder_insertl(sb, sb->length, string, length);
}
bool
stringbuilder_insert(stringbuilder *sb, const size_t position, const char *string) {
return stringbuilder_insertl(sb, position, string, strlen(string));
}
bool
stringbuilder_append(stringbuilder *sb, const char *string) {
return stringbuilder_insert(sb, sb->length, string);
}
bool
stringbuilder_appendf(stringbuilder *sb, const char *format, ...) {
int printed = 0;
va_list args;
if (ensure_space(sb, 0)) {
va_start(args, format);
printed = vsnprintf(&sb->string[sb->length], sb->capacity - sb->length + 1, format, args);
va_end(args);
} else return false;
if (printed < 0) {
return false;
} else if (sb->length + printed > sb->capacity) {
if (ensure_space(sb, printed)) {
va_start(args, format);
printed = vsnprintf(&sb->string[sb->length], sb->capacity - sb->length + 1, format, args);
va_end(args);
} else {
//undo append
sb->string[sb->length] = '\0';
return false;
}
}
sb->length += printed;
sb->string[sb->length] = '\0';
return true;
}
bool
stringbuilder_insertf(stringbuilder *sb, const size_t position, const char *format, ...) {
va_list args;
char buffer[0x2000];
va_start(args, format);
int printed = vsnprintf(buffer, sizeof buffer, format, args);
va_end(args);
if (printed < 0) {
return false;
} else if ((size_t)printed < sizeof buffer) {
return stringbuilder_insertl(sb, position, buffer, printed);
} else {
char *string = malloc((printed + 1) * sizeof *string);
if (string) {
va_start(args, format);
printed = vsnprintf(string, printed + 1, format, args);
va_end(args);
bool succeeded = stringbuilder_insertl(sb, position, string, printed);
free(string);
return succeeded;
}
}
return false;
}
bool
stringbuilder_insert_sb(stringbuilder *dest, const size_t position, const stringbuilder *src) {
return stringbuilder_insertl(dest, position, src->string, src->length);
}
bool
stringbuilder_append_sb(stringbuilder *dest, const stringbuilder *src) {
return stringbuilder_insert_sb(dest, dest->length, src);
}
char *
stringbuilder_to_string(stringbuilder *sb) {
char *string = malloc((sb->length + 1) * sizeof *sb->string);
if (string) {
if (sb->string) {
memcpy(string, sb->string, (sb->length + 1) * sizeof *sb->string);
} else {
string[0] = '\0';
}
}
return string;
}
const char *
stringbuilder_string(stringbuilder *sb) {
ensure_space(sb, 0);
return sb->string;
}