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
|
/**
* Copyright 2019-2020 DigitalOcean Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stddef.h>
// Public
#include "prom_alloc.h"
// Private
#include "prom_assert.h"
#include "prom_string_builder_i.h"
#include "prom_string_builder_t.h"
// The initial size of a string created via prom_string_builder
#define PROM_STRING_BUILDER_INIT_SIZE 32
// prom_string_builder_init prototype declaration
int prom_string_builder_init(prom_string_builder_t *self);
struct prom_string_builder {
char *str; /**< the target string */
size_t allocated; /**< the size allocated to the string in bytes */
size_t len; /**< the length of str */
size_t init_size; /**< the initialize size of space to allocate */
};
prom_string_builder_t *prom_string_builder_new(void) {
int r = 0;
prom_string_builder_t *self = (prom_string_builder_t *)prom_malloc(sizeof(prom_string_builder_t));
self->init_size = PROM_STRING_BUILDER_INIT_SIZE;
r = prom_string_builder_init(self);
if (r) {
prom_string_builder_destroy(self);
return NULL;
}
return self;
}
int prom_string_builder_init(prom_string_builder_t *self) {
PROM_ASSERT(self != NULL);
if (self == NULL) return 1;
self->str = (char *)prom_malloc(self->init_size);
*self->str = '\0';
self->allocated = self->init_size;
self->len = 0;
return 0;
}
int prom_string_builder_destroy(prom_string_builder_t *self) {
PROM_ASSERT(self != NULL);
if (self == NULL) return 0;
prom_free(self->str);
self->str = NULL;
prom_free(self);
self = NULL;
return 0;
}
/**
* @brief API PRIVATE Grows the size of the string given the value we want to add
*
* The method continuously shifts left until the new size is large enough to accommodate add_len. This private method
* is called in methods that need to add one or more characters to the underlying string.
*/
static int prom_string_builder_ensure_space(prom_string_builder_t *self, size_t add_len) {
PROM_ASSERT(self != NULL);
if (self == NULL) return 1;
if (add_len == 0 || self->allocated >= self->len + add_len + 1) return 0;
while (self->allocated < self->len + add_len + 1) self->allocated <<= 1;
self->str = (char *)prom_realloc(self->str, self->allocated);
return 0;
}
int prom_string_builder_add_str(prom_string_builder_t *self, const char *str) {
PROM_ASSERT(self != NULL);
int r = 0;
if (self == NULL) return 1;
if (str == NULL || *str == '\0') return 0;
size_t len = strlen(str);
r = prom_string_builder_ensure_space(self, len);
if (r) return r;
memcpy(self->str + self->len, str, len);
self->len += len;
self->str[self->len] = '\0';
return 0;
}
int prom_string_builder_add_char(prom_string_builder_t *self, char c) {
PROM_ASSERT(self != NULL);
int r = 0;
if (self == NULL) return 1;
r = prom_string_builder_ensure_space(self, 1);
if (r) return r;
self->str[self->len] = c;
self->len++;
self->str[self->len] = '\0';
return 0;
}
int prom_string_builder_truncate(prom_string_builder_t *self, size_t len) {
PROM_ASSERT(self != NULL);
if (self == NULL) return 1;
if (len >= self->len) return 0;
self->len = len;
self->str[self->len] = '\0';
return 0;
}
int prom_string_builder_clear(prom_string_builder_t *self) {
PROM_ASSERT(self != NULL);
prom_free(self->str);
self->str = NULL;
return prom_string_builder_init(self);
}
size_t prom_string_builder_len(prom_string_builder_t *self) {
PROM_ASSERT(self != NULL);
return self->len;
}
char *prom_string_builder_dump(prom_string_builder_t *self) {
PROM_ASSERT(self != NULL);
// +1 to accommodate \0
char *out = (char *)prom_malloc((self->len + 1) * sizeof(char));
memcpy(out, self->str, self->len + 1);
return out;
}
char *prom_string_builder_str(prom_string_builder_t *self) {
PROM_ASSERT(self != NULL);
return self->str;
}
|