File Coverage

lib/SPVM/Builder/src/spvm_string_buffer.c
Criterion Covered Total %
statement 93 118 78.8
branch 17 30 56.6
condition n/a
subroutine n/a
pod n/a
total 110 148 74.3


line stmt bran cond sub pod time code
1             // Copyright (c) 2023 Yuki Kimoto
2             // MIT License
3              
4             #include
5             #include
6             #include
7             #include
8             #include
9              
10             #include "spvm_string_buffer.h"
11             #include "spvm_allocator.h"
12             #include "spvm_allocator.h"
13             #include "spvm_native.h"
14              
15 60179           SPVM_STRING_BUFFER* SPVM_STRING_BUFFER_new(SPVM_ALLOCATOR* allocator, int32_t capacity, int32_t memory_block_type) {
16            
17 60179 100         if (capacity == 0) {
18 515           capacity = 16;
19             }
20            
21             SPVM_STRING_BUFFER* string_buffer;
22 60179 100         if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
23 583           string_buffer = (SPVM_STRING_BUFFER*)SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, sizeof(SPVM_STRING_BUFFER));
24             }
25 59596 50         else if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
26 59596           string_buffer = (SPVM_STRING_BUFFER*)SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, sizeof(SPVM_STRING_BUFFER));
27             }
28             else {
29 0           assert(0);
30             }
31            
32 60179           string_buffer->capacity = capacity;
33 60179 100         if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
34 583           string_buffer->string = (char*)SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, capacity);
35             }
36 59596 50         else if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
37 59596           string_buffer->string = (char*)SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, capacity);
38             }
39             else {
40 0           assert(0);
41             }
42              
43 60179           string_buffer->allocator = allocator;
44            
45 60179           string_buffer->memory_block_type = memory_block_type;
46            
47 60179           return string_buffer;
48             }
49              
50              
51 515           SPVM_STRING_BUFFER* SPVM_STRING_BUFFER_new_tmp(SPVM_ALLOCATOR* allocator, int32_t capacity) {
52 515           return SPVM_STRING_BUFFER_new(allocator, capacity, SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP);
53             }
54              
55 0           char* SPVM_STRING_BUFFER_get_buffer(SPVM_STRING_BUFFER* string_buffer) {
56            
57 0           return string_buffer->string;
58             }
59              
60 5414085           void SPVM_STRING_BUFFER_maybe_extend(SPVM_STRING_BUFFER* string_buffer, int32_t new_length) {
61              
62 5414085           SPVM_ALLOCATOR* allocator = string_buffer->allocator;
63              
64             // Extend
65 5418770 100         while (new_length + 1 > string_buffer->capacity) {
66 4685           int32_t new_capacity = string_buffer->capacity * 2;
67             char* new_buffer;
68 4685 50         if (string_buffer->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
69 4685           new_buffer = (char*)SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, new_capacity);
70             }
71 0 0         else if (string_buffer->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
72 0           new_buffer = (char*)SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, new_capacity);
73             }
74             else {
75 0           assert(0);
76             }
77              
78 4685           memcpy(new_buffer, string_buffer->string, string_buffer->length);
79              
80 4685 50         if (string_buffer->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
81 4685           SPVM_ALLOCATOR_free_memory_block_tmp(allocator, string_buffer->string);
82             }
83 0 0         else if (string_buffer->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
84             // Nothing
85             }
86             else {
87 0           assert(0);
88             }
89              
90 4685           string_buffer->string = new_buffer;
91 4685           string_buffer->capacity = new_capacity;
92             }
93 5414085           }
94              
95 3808799           int32_t SPVM_STRING_BUFFER_add(SPVM_STRING_BUFFER* string_buffer, const char* string) {
96            
97 3808799           int32_t id = string_buffer->length;
98            
99 3808799           int32_t string_length = strlen(string);
100            
101 3808799           int32_t new_length = string_buffer->length + string_length;
102            
103             // Extend
104 3808799           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_length);
105            
106 3808799           memcpy(string_buffer->string + string_buffer->length, string, string_length);
107            
108 3808799           string_buffer->length = new_length;
109            
110 3808799           return id;
111             }
112              
113 53366           int32_t SPVM_STRING_BUFFER_add_len(SPVM_STRING_BUFFER* string_buffer, char* string, int32_t string_length) {
114            
115 53366           int32_t id = string_buffer->length;
116            
117 53366           int32_t new_length = string_buffer->length + string_length;
118            
119             // Extend
120 53366           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_length);
121            
122 53366           memcpy(string_buffer->string + string_buffer->length, string, string_length);
123            
124 53366           string_buffer->length = new_length;
125            
126 53366           return id;
127             }
128              
129 791484           int32_t SPVM_STRING_BUFFER_add_len_nullstr(SPVM_STRING_BUFFER* string_buffer, char* string, int32_t string_length) {
130            
131 791484           int32_t id = string_buffer->length;
132            
133 791484           int32_t new_length = string_buffer->length + string_length + 1;
134            
135             // Extend
136 791484           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_length);
137            
138 791484           memcpy(string_buffer->string + string_buffer->length, string, string_length);
139 791484           *(string_buffer->string + string_buffer->length + string_length) = '\0';
140            
141 791484           string_buffer->length = new_length;
142            
143 791484           return id;
144             }
145              
146 43595           int32_t SPVM_STRING_BUFFER_add_hex_char(SPVM_STRING_BUFFER* string_buffer, char ch) {
147            
148 43595           int32_t id = string_buffer->length;
149              
150 43595           int32_t new_length = string_buffer->length + 4;
151            
152             // Extend
153 43595           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_length);
154            
155 43595           sprintf(string_buffer->string + string_buffer->length, "\\x%02X", ch & 0x000000FF);
156            
157 43595           string_buffer->length = new_length;
158            
159 43595           return id;
160             }
161              
162 432           int32_t SPVM_STRING_BUFFER_add_byte(SPVM_STRING_BUFFER* string_buffer, int8_t value) {
163            
164 432           int32_t id = string_buffer->length;
165            
166 432           int32_t max_length = 20;
167            
168 432           int32_t new_max_length = string_buffer->length + max_length;
169            
170             // Extend
171 432           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_max_length);
172            
173 432           int32_t write_length = sprintf(string_buffer->string + string_buffer->length, "%" PRId8, value);
174            
175 432           string_buffer->length += write_length;
176            
177 432           return id;
178             }
179              
180 0           int32_t SPVM_STRING_BUFFER_add_short(SPVM_STRING_BUFFER* string_buffer, int16_t value) {
181            
182 0           int32_t id = string_buffer->length;
183            
184 0           int32_t max_length = 20;
185            
186 0           int32_t new_max_length = string_buffer->length + max_length;
187            
188             // Extend
189 0           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_max_length);
190            
191 0           int32_t write_length = sprintf(string_buffer->string + string_buffer->length, "%" PRId16, value);
192            
193 0           string_buffer->length += write_length;
194            
195 0           return id;
196             }
197              
198 715517           int32_t SPVM_STRING_BUFFER_add_int(SPVM_STRING_BUFFER* string_buffer, int32_t value) {
199            
200 715517           int32_t id = string_buffer->length;
201            
202 715517           int32_t max_length = 20;
203            
204 715517           int32_t new_max_length = string_buffer->length + max_length;
205            
206             // Extend
207 715517           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_max_length);
208            
209             int32_t write_length;
210 715517 100         if (value == INT32_MIN) {
211 28           write_length = sprintf(string_buffer->string + string_buffer->length, "INT32_MIN");
212             }
213             else {
214 715489           write_length = sprintf(string_buffer->string + string_buffer->length, "%" PRId32, value);
215             }
216            
217 715517           string_buffer->length += write_length;
218            
219 715517           return id;
220             }
221              
222 892           int32_t SPVM_STRING_BUFFER_add_long(SPVM_STRING_BUFFER* string_buffer, int64_t value) {
223            
224 892           int32_t id = string_buffer->length;
225              
226 892           int32_t max_length = 20 + 2;
227            
228 892           int32_t new_max_length = string_buffer->length + max_length;
229            
230             // Extend
231 892           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_max_length);
232            
233             int32_t write_length;
234 892 100         if (value == INT64_MIN) {
235 22           write_length = sprintf(string_buffer->string + string_buffer->length, "INT64_MIN");
236             }
237             else {
238 870           write_length = sprintf(string_buffer->string + string_buffer->length, "%" PRId64 "LL", value);
239             }
240            
241 892           string_buffer->length += write_length;
242            
243 892           return id;
244             }
245              
246 583           void SPVM_STRING_BUFFER_free(SPVM_STRING_BUFFER* string_buffer) {
247              
248 583           SPVM_ALLOCATOR* allocator = string_buffer->allocator;
249            
250 583 50         if (string_buffer->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
251 583           SPVM_ALLOCATOR_free_memory_block_tmp(allocator, string_buffer->string);
252 583           SPVM_ALLOCATOR_free_memory_block_tmp(allocator, string_buffer);
253             }
254 0 0         else if (string_buffer->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
255             // Nothing
256             }
257             else {
258 0           assert(0);
259             }
260 583           }
261              
262 0           int32_t SPVM_STRING_BUFFER_contains(SPVM_STRING_BUFFER* string_buffer, int32_t offset, const char* string) {
263 0           const char* found_ptr = strstr(string_buffer->string + offset, string);
264            
265 0           int32_t found = 0;
266 0 0         if (found_ptr) {
267 0           found = 1;
268             }
269            
270 0           return found;
271             }