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 59366           SPVM_STRING_BUFFER* SPVM_STRING_BUFFER_new(SPVM_ALLOCATOR* allocator, int32_t capacity, int32_t memory_block_type) {
16            
17 59366 100         if (capacity == 0) {
18 522           capacity = 16;
19             }
20            
21             SPVM_STRING_BUFFER* string_buffer;
22 59366 100         if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
23 590           string_buffer = (SPVM_STRING_BUFFER*)SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, sizeof(SPVM_STRING_BUFFER));
24             }
25 58776 50         else if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
26 58776           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 59366           string_buffer->capacity = capacity;
33 59366 100         if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
34 590           string_buffer->string = (char*)SPVM_ALLOCATOR_alloc_memory_block_tmp(allocator, capacity);
35             }
36 58776 50         else if (memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_PERMANENT) {
37 58776           string_buffer->string = (char*)SPVM_ALLOCATOR_alloc_memory_block_permanent(allocator, capacity);
38             }
39             else {
40 0           assert(0);
41             }
42              
43 59366           string_buffer->allocator = allocator;
44            
45 59366           string_buffer->memory_block_type = memory_block_type;
46            
47 59366           return string_buffer;
48             }
49              
50              
51 522           SPVM_STRING_BUFFER* SPVM_STRING_BUFFER_new_tmp(SPVM_ALLOCATOR* allocator, int32_t capacity) {
52 522           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 5534625           void SPVM_STRING_BUFFER_maybe_extend(SPVM_STRING_BUFFER* string_buffer, int32_t new_length) {
61              
62 5534625           SPVM_ALLOCATOR* allocator = string_buffer->allocator;
63              
64             // Extend
65 5539356 100         while (new_length + 1 > string_buffer->capacity) {
66 4731           int32_t new_capacity = string_buffer->capacity * 2;
67             char* new_buffer;
68 4731 50         if (string_buffer->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
69 4731           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 4731           memcpy(new_buffer, string_buffer->string, string_buffer->length);
79              
80 4731 50         if (string_buffer->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
81 4731           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 4731           string_buffer->string = new_buffer;
91 4731           string_buffer->capacity = new_capacity;
92             }
93 5534625           }
94              
95 3926163           int32_t SPVM_STRING_BUFFER_add(SPVM_STRING_BUFFER* string_buffer, const char* string) {
96            
97 3926163           int32_t id = string_buffer->length;
98            
99 3926163           int32_t string_length = strlen(string);
100            
101 3926163           int32_t new_length = string_buffer->length + string_length;
102            
103             // Extend
104 3926163           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_length);
105            
106 3926163           memcpy(string_buffer->string + string_buffer->length, string, string_length);
107            
108 3926163           string_buffer->length = new_length;
109            
110 3926163           return id;
111             }
112              
113 52546           int32_t SPVM_STRING_BUFFER_add_len(SPVM_STRING_BUFFER* string_buffer, char* string, int32_t string_length) {
114            
115 52546           int32_t id = string_buffer->length;
116            
117 52546           int32_t new_length = string_buffer->length + string_length;
118            
119             // Extend
120 52546           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_length);
121            
122 52546           memcpy(string_buffer->string + string_buffer->length, string, string_length);
123            
124 52546           string_buffer->length = new_length;
125            
126 52546           return id;
127             }
128              
129 780192           int32_t SPVM_STRING_BUFFER_add_len_nullstr(SPVM_STRING_BUFFER* string_buffer, char* string, int32_t string_length) {
130            
131 780192           int32_t id = string_buffer->length;
132            
133 780192           int32_t new_length = string_buffer->length + string_length + 1;
134            
135             // Extend
136 780192           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_length);
137            
138 780192           memcpy(string_buffer->string + string_buffer->length, string, string_length);
139 780192           *(string_buffer->string + string_buffer->length + string_length) = '\0';
140            
141 780192           string_buffer->length = new_length;
142            
143 780192           return id;
144             }
145              
146 43060           int32_t SPVM_STRING_BUFFER_add_hex_char(SPVM_STRING_BUFFER* string_buffer, char ch) {
147            
148 43060           int32_t id = string_buffer->length;
149              
150 43060           int32_t new_length = string_buffer->length + 4;
151            
152             // Extend
153 43060           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_length);
154            
155 43060           sprintf(string_buffer->string + string_buffer->length, "\\x%02X", ch & 0x000000FF);
156            
157 43060           string_buffer->length = new_length;
158            
159 43060           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 731323           int32_t SPVM_STRING_BUFFER_add_int(SPVM_STRING_BUFFER* string_buffer, int32_t value) {
199            
200 731323           int32_t id = string_buffer->length;
201            
202 731323           int32_t max_length = 20;
203            
204 731323           int32_t new_max_length = string_buffer->length + max_length;
205            
206             // Extend
207 731323           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_max_length);
208            
209             int32_t write_length;
210 731323 100         if (value == INT32_MIN) {
211 28           write_length = sprintf(string_buffer->string + string_buffer->length, "INT32_MIN");
212             }
213             else {
214 731295           write_length = sprintf(string_buffer->string + string_buffer->length, "%" PRId32, value);
215             }
216            
217 731323           string_buffer->length += write_length;
218            
219 731323           return id;
220             }
221              
222 909           int32_t SPVM_STRING_BUFFER_add_long(SPVM_STRING_BUFFER* string_buffer, int64_t value) {
223            
224 909           int32_t id = string_buffer->length;
225              
226 909           int32_t max_length = 20 + 2;
227            
228 909           int32_t new_max_length = string_buffer->length + max_length;
229            
230             // Extend
231 909           SPVM_STRING_BUFFER_maybe_extend(string_buffer, new_max_length);
232            
233             int32_t write_length;
234 909 100         if (value == INT64_MIN) {
235 22           write_length = sprintf(string_buffer->string + string_buffer->length, "INT64_MIN");
236             }
237             else {
238 887           write_length = sprintf(string_buffer->string + string_buffer->length, "%" PRId64 "LL", value);
239             }
240            
241 909           string_buffer->length += write_length;
242            
243 909           return id;
244             }
245              
246 590           void SPVM_STRING_BUFFER_free(SPVM_STRING_BUFFER* string_buffer) {
247              
248 590           SPVM_ALLOCATOR* allocator = string_buffer->allocator;
249            
250 590 50         if (string_buffer->memory_block_type == SPVM_ALLOCATOR_C_ALLOC_TYPE_TMP) {
251 590           SPVM_ALLOCATOR_free_memory_block_tmp(allocator, string_buffer->string);
252 590           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 590           }
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             }