GCC Code Coverage Report


Directory: ../src/
File: extensions/string_extensions.c
Date: 2026-10-16 14:49:47
Coverage Exec Excl Total
Lines: 94.9% 430 0 453
Functions: 100.0% 21 0 21
Branches: 87.3% 288 0 330

Line Branch Exec Source
1 /*
2 Copyright 2022, 2023 - 2026 Joel Svensson svenssonjoel@yahoo.se
3 Copyright 2022, 2023 Benjamin Vedder
4 Copyright 2024 Rasmus Söderhielm rasmus.soderhielm@gmail.com
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "extensions.h"
21 #include "lbm_memory.h"
22 #include "heap.h"
23 #include "fundamental.h"
24 #include "lbm_c_interop.h"
25 #include "eval_cps.h"
26 #include "print.h"
27
28 #include <ctype.h>
29
30 #ifdef LBM_OPT_STRING_EXTENSIONS_SIZE
31 #pragma GCC optimize ("-Os")
32 #endif
33 #ifdef LBM_OPT_STRING_EXTENSIONS_SIZE_AGGRESSIVE
34 #pragma GCC optimize ("-Oz")
35 #endif
36
37 #ifndef MIN
38 #define MIN(a,b) (((a)<(b))?(a):(b))
39 #endif
40 #ifndef MAX
41 #define MAX(a,b) (((a)>(b))?(a):(b))
42 #endif
43
44 static char print_val_buffer[256];
45
46 static lbm_uint sym_left;
47 static lbm_uint sym_case_insensitive;
48
49 118 static bool str_from_n_format_is_safe(const char *format, bool is_float, bool *format_is_float) {
50 118 const char *p = format;
51 118 int num_specs = 0;
52
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 118 times.
308 while (*p) {
53
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 118 times.
190 if (*p != '%') {
54 72 p++;
55 72 continue;
56 }
57 118 p++;
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 if (*p == '%') {
59 ✗ p++;
60 ✗ continue;
61 }
62 118 num_specs++;
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 if (num_specs > 1) return false;
64
65
6/10
✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 154 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 154 times.
✓ Branch 6 taken 36 times.
✓ Branch 7 taken 118 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 118 times.
154 while (*p == '-' || *p == '+' || *p == ' ' || *p == '0' || *p == '#') p++;
66
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 118 times.
154 while (isdigit((unsigned char)*p)) p++;
67
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 36 times.
118 if (*p == '.') {
68 82 p++;
69
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 82 times.
164 while (isdigit((unsigned char)*p)) p++;
70 }
71
72 118 char c = *p;
73
4/8
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 36 times.
✗ Branch 7 not taken.
36 bool spec_is_float = (c == 'f' || c == 'F' || c == 'e' || c == 'E' ||
74
5/8
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 36 times.
154 c == 'g' || c == 'G' || c == 'a' || c == 'A');
75
3/6
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 118 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 118 times.
✗ Branch 5 not taken.
118 bool spec_is_int = (c == 'd' || c == 'i' || c == 'o' ||
76
5/8
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 118 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 82 times.
✓ Branch 5 taken 36 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 82 times.
236 c == 'u' || c == 'x' || c == 'X' || c == 'c');
77
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
118 if (!spec_is_float && !spec_is_int) return false;
78
3/4
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 82 times.
118 if (is_float && !spec_is_float) return false;
79 118 *format_is_float = spec_is_float;
80 118 p++;
81 }
82 118 return num_specs == 1;
83 }
84
85 51827 static lbm_value ext_str_from_n(lbm_value *args, lbm_uint argn) {
86
4/4
✓ Branch 0 taken 268 times.
✓ Branch 1 taken 51559 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 160 times.
51827 if (argn != 1 && argn != 2) {
87 108 lbm_set_error_reason((char*)lbm_error_str_num_args);
88 108 return ENC_SYM_EERROR;
89 }
90
2/2
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 51644 times.
51719 if (!lbm_is_number(args[0])) {
91 75 return ENC_SYM_TERROR;
92 }
93
94
4/4
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 51484 times.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 118 times.
51644 if (argn == 2 && !lbm_is_array_r(args[1])) {
95 42 return ENC_SYM_TERROR;
96 }
97
98 51602 char *format = 0;
99
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 51484 times.
51602 if (argn == 2) {
100 118 format = lbm_dec_str(args[1]);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 if (!format) {
102 ✗ return ENC_SYM_TERROR;
103 }
104 }
105
106
4/4
✓ Branch 1 taken 51527 times.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 223 times.
✓ Branch 4 taken 51304 times.
103129 bool is_float = (lbm_type_of_functional(args[0]) == LBM_TYPE_DOUBLE ||
107 51527 lbm_type_of_functional(args[0]) == LBM_TYPE_FLOAT);
108 51602 bool format_is_float = is_float;
109
110
3/4
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 51484 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 118 times.
51602 if (format && !str_from_n_format_is_safe(format, is_float, &format_is_float)) {
111 ✗ lbm_set_error_reason((char*)lbm_error_str_incorrect_arg);
112 ✗ lbm_set_error_suspect(args[1]);
113 ✗ return ENC_SYM_TERROR;
114 }
115
116
2/2
✓ Branch 0 taken 51484 times.
✓ Branch 1 taken 118 times.
51602 if (!format) {
117
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 51268 times.
51484 format = is_float ? "%g" : "%d";
118 }
119
120 char buffer[100];
121 51602 size_t len = 0;
122
123
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 51304 times.
51602 if (format_is_float) {
124 298 len = (size_t)snprintf(buffer, sizeof(buffer), format, lbm_dec_as_double(args[0]));
125 } else {
126 51304 len = (size_t)snprintf(buffer, sizeof(buffer), format, lbm_dec_as_i32(args[0]));
127 }
128
129 51602 len = MIN(len, sizeof(buffer));
130
131 51602 return span_to_lbm(buffer, len);
132 }
133
134 // signature: (str-join strings [delim]) -> str
135 123687 static lbm_value ext_str_join(lbm_value *args, lbm_uint argn) {
136 // This function does not check that the string arguments contain any
137 // terminating null bytes.
138
139
4/4
✓ Branch 0 taken 108483 times.
✓ Branch 1 taken 15204 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 108447 times.
123687 if (argn != 1 && argn != 2) {
140 36 lbm_set_error_reason((char *)lbm_error_str_num_args);
141 36 return ENC_SYM_EERROR;
142 }
143
144 123651 size_t str_len = 0;
145 123651 size_t str_count = 0;
146
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 123612 times.
123651 if (!lbm_is_list(args[0])) {
147 39 lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
148 39 lbm_set_error_suspect(args[0]);
149 39 return ENC_SYM_TERROR;
150 }
151 123612 lbm_value current = args[0];
152
2/2
✓ Branch 1 taken 247572 times.
✓ Branch 2 taken 123573 times.
371145 while (lbm_is_cons(current)) {
153 247572 lbm_cons_t *cell = lbm_ref_cell(current);
154 247572 lbm_value car_val = cell->car;
155 247572 char *str = NULL;
156 247572 size_t arr_size = 0;
157
2/2
✓ Branch 1 taken 247533 times.
✓ Branch 2 taken 39 times.
247572 if (lbm_dec_str_size(car_val, &str, &arr_size)) {
158 247533 str_len += strlen_max(str, arr_size);
159 247533 str_count += 1;
160 } else {
161 39 lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
162 39 lbm_set_error_suspect(args[0]);
163 39 return ENC_SYM_TERROR;
164 }
165 247533 current = cell->cdr;
166 }
167
168 123573 const char *delim = "";
169
2/2
✓ Branch 0 taken 108375 times.
✓ Branch 1 taken 15198 times.
123573 if (argn >= 2) {
170 108375 delim = lbm_dec_str(args[1]);
171
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 108336 times.
108375 if (!delim) {
172 39 lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
173 39 lbm_set_error_suspect(args[1]);
174 39 return ENC_SYM_TERROR;
175 }
176 }
177
178 123534 size_t delim_len = strlen(delim);
179
2/2
✓ Branch 0 taken 87414 times.
✓ Branch 1 taken 36120 times.
123534 if (str_count > 0) {
180 87414 str_len += (str_count - 1) * delim_len;
181 }
182
183 lbm_value result;
184
2/2
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 123390 times.
123534 if (!lbm_create_array(&result, str_len + 1)) {
185 144 return ENC_SYM_MERROR;
186 }
187 123390 char *result_str = lbm_dec_str(result);
188
189 123390 size_t i = 0;
190 123390 size_t offset = 0;
191 123390 current = args[0];
192
2/2
✓ Branch 1 taken 247164 times.
✓ Branch 2 taken 123390 times.
370554 while (lbm_is_cons(current)) {
193 247164 lbm_cons_t *cell = lbm_ref_cell(current);
194 247164 lbm_value car_val = cell->car;
195 // All arrays have been prechecked.
196 247164 lbm_array_header_t *array = (lbm_array_header_t*) lbm_car(car_val);
197 247164 char *str = (char*)array->data;
198 247164 size_t len = strlen_max(str, array->size);
199
200 247164 memcpy(result_str + offset, str, len);
201 247164 offset += len;
202
203
2/2
✓ Branch 0 taken 159846 times.
✓ Branch 1 taken 87318 times.
247164 if (i != str_count - 1) {
204 159846 memcpy(result_str + offset, delim, delim_len);
205 159846 offset += delim_len;
206 }
207 247164 i++;
208 247164 current = cell->cdr;
209 }
210
211 123390 result_str[str_len] = '\0';
212
213 123390 return result;
214 }
215
216 585 static lbm_value ext_str_to_i(lbm_value *args, lbm_uint argn) {
217
4/4
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 510 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 39 times.
585 if (argn != 1 && argn != 2) {
218 36 lbm_set_error_reason((char*)lbm_error_str_num_args);
219 36 return ENC_SYM_EERROR;
220 }
221
222 549 char *str = lbm_dec_str(args[0]);
223
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 510 times.
549 if (!str) {
224 39 return ENC_SYM_TERROR;
225 }
226
227 510 int base = 0;
228
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 471 times.
510 if (argn == 2) {
229
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 36 times.
39 if (!lbm_is_number(args[1])) {
230 3 return ENC_SYM_TERROR;
231 }
232
233 36 base = (int)lbm_dec_as_u32(args[1]);
234 }
235
236 507 return lbm_enc_i32((int32_t)strtol(str, NULL, base));
237 }
238
239 147 static lbm_value ext_str_to_f(lbm_value *args, lbm_uint argn) {
240
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 111 times.
147 if (argn != 1) {
241 36 lbm_set_error_reason((char*)lbm_error_str_num_args);
242 36 return ENC_SYM_EERROR;
243 }
244
245 111 char *str = lbm_dec_str(args[0]);
246
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 72 times.
111 if (!str) {
247 39 return ENC_SYM_TERROR;
248 }
249
250 72 return lbm_enc_float(strtof(str, NULL));
251 }
252
253 213 static lbm_value ext_str_part(lbm_value *args, lbm_uint argn) {
254
6/6
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 129 times.
✓ Branch 3 taken 36 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 174 times.
213 if ((argn != 2 && argn != 3) || !lbm_is_number(args[1])) {
255 39 lbm_set_error_reason((char*)lbm_error_str_num_args);
256 39 return ENC_SYM_TERROR;
257 }
258
259 174 size_t str_arr_len = 0;
260 174 char *str = NULL;//lbm_dec_str(args[0]);
261
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 171 times.
174 if (!lbm_dec_str_size(args[0], &str, &str_arr_len)) {
262 3 return ENC_SYM_TERROR;
263 }
264
265 171 uint32_t len = (uint32_t)strlen_max(str, str_arr_len);
266
267 171 uint32_t start = lbm_dec_as_u32(args[1]);
268
269
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 165 times.
171 if (start >= len) {
270 6 return ENC_SYM_EERROR;
271 }
272
273 165 uint32_t n = len - start;
274
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 36 times.
165 if (argn == 3) {
275
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 126 times.
129 if (!lbm_is_number(args[2])) {
276 3 return ENC_SYM_TERROR;
277 }
278
279
2/2
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 21 times.
126 n = MIN(lbm_dec_as_u32(args[2]), n);
280 }
281
282 162 return span_to_lbm(str + start, n);
283 }
284
285 1863 static bool char_in(char c, char *delim, unsigned int max_ix) {
286 1863 char *d = delim;
287 1863 unsigned int i = 0;
288
3/4
✓ Branch 0 taken 3522 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2259 times.
✓ Branch 3 taken 1263 times.
3522 while (i < max_ix && *d != '\0' ) {
289
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 1659 times.
2259 if (c == *d) return true;
290 1659 d++; i++;
291 }
292 1263 return false;
293 }
294
295 330 static lbm_value ext_str_split(lbm_value *args, lbm_uint argn) {
296
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 291 times.
330 if (argn != 2) {
297 39 lbm_set_error_reason((char*)lbm_error_str_num_args);
298 39 return ENC_SYM_TERROR;
299 }
300
301 291 size_t str_arr_size = 0;
302 291 char *str = NULL; //lbm_dec_str(args[0]);
303
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 288 times.
291 if (!lbm_dec_str_size(args[0], &str, &str_arr_size)) {
304 3 return ENC_SYM_TERROR;
305 }
306
307 288 char *delim = NULL;
308 288 size_t delim_arr_size = 0;
309
310
2/2
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 210 times.
288 if (lbm_is_number(args[1])) {
311
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 36 times.
78 int step = MAX(lbm_dec_as_i32(args[1]), 1);
312 78 lbm_value res = ENC_SYM_NIL;
313 78 int len = (int)strlen_max(str, str_arr_size);
314
2/2
✓ Branch 0 taken 726 times.
✓ Branch 1 taken 78 times.
804 for (int i = len / step;i >= 0;i--) {
315 726 int ind_now = i * step;
316
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 690 times.
726 if (ind_now >= len) {
317 36 continue;
318 }
319
320 690 int step_now = step;
321
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 690 times.
930 while ((ind_now + step_now) > len) {
322 240 step_now--;
323 }
324
325 690 lbm_value tok = span_to_lbm(str + ind_now, (size_t)step_now);
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 690 times.
690 if (tok == ENC_SYM_MERROR) return tok;
327 690 res = lbm_cons(tok, res);
328 }
329 78 return res;
330
1/2
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
210 } else if (lbm_dec_str_size(args[1], &delim, &delim_arr_size)) {
331 210 lbm_value res = ENC_SYM_NIL;
332
333 210 unsigned int i_start = 0;
334 210 unsigned int i_end = 0;
335
336 // Abort when larger that array size. Protection against abuse
337 // with byte-arrays.
338
1/2
✓ Branch 0 taken 810 times.
✗ Branch 1 not taken.
810 while (i_end < str_arr_size) {
339
340
4/4
✓ Branch 0 taken 1863 times.
✓ Branch 1 taken 210 times.
✓ Branch 3 taken 1263 times.
✓ Branch 4 taken 600 times.
2073 while (str[i_end] != '\0' && !char_in(str[i_end], delim, (unsigned int)delim_arr_size)) {
341 1263 i_end ++;
342 }
343
344 810 unsigned int len = i_end - i_start;
345 810 char *s = &str[i_start];
346 810 lbm_value tok = span_to_lbm(s, len);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 if (tok == ENC_SYM_MERROR) return tok;
348 810 res = lbm_cons(tok, res);
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 if (res == ENC_SYM_MERROR) return res;
350
351
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 600 times.
810 if (str[i_end] == '\0') break;
352 600 i_start = i_end + 1;
353 600 i_end = i_end + 1;
354
355 }
356 210 return lbm_list_destructive_reverse(res);
357 }
358 ✗ return ENC_SYM_TERROR;
359 }
360
361 // Todo: Clean this up for 64bit
362 156 static lbm_value ext_str_replace(lbm_value *args, lbm_uint argn) {
363
4/4
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 72 times.
156 if (argn != 2 && argn != 3) {
364 42 lbm_set_error_reason((char*)lbm_error_str_num_args);
365 42 return ENC_SYM_EERROR;
366 }
367
368 114 size_t orig_arr_size = 0;
369 114 char *orig = NULL; // lbm_dec_str(args[0]);
370
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 108 times.
114 if (!lbm_dec_str_size(args[0], &orig, &orig_arr_size)) {
371 6 return ENC_SYM_TERROR;
372 }
373
374 108 size_t rep_arr_size = 0;
375 108 char *rep = NULL; //lbm_dec_str(args[1]);
376
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 99 times.
108 if (!lbm_dec_str_size(args[1], &rep, &rep_arr_size)) {
377 9 return ENC_SYM_TERROR;
378 }
379
380 99 size_t with_arr_size = 0;
381 99 char *with = "";
382
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 39 times.
99 if (argn == 3) {
383
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 54 times.
60 if (!lbm_dec_str_size(args[2], &with, &with_arr_size)) {
384 6 return ENC_SYM_TERROR;
385 }
386 }
387
388 // See https://stackoverflow.com/questions/779875/what-function-is-to-replace-a-substring-from-a-string-in-c
389 //char *result; // the return string
390 char *ins; // the next insert point
391 char *tmp; // varies
392 size_t len_rep; // length of rep (the string to remove)
393 size_t len_with; // length of with (the string to replace rep with)
394 //size_t len_front; // distance between rep and end of last rep
395 int count; // number of replacements
396
397 93 len_rep = strlen_max(rep, rep_arr_size);
398
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 87 times.
93 if (len_rep == 0) {
399 6 return args[0]; // empty rep causes infinite loop during count
400 }
401
402 87 len_with = strlen_max(with,with_arr_size);
403
404 // count the number of replacements needed
405 87 ins = orig;
406
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 87 times.
318 for (count = 0; (tmp = strstr(ins, rep)); ++count) {
407 231 ins = tmp + len_rep;
408 }
409
410 87 size_t len_res = strlen_max(orig, orig_arr_size) + (len_with - len_rep) * (unsigned int)count + 1;
411 lbm_value lbm_res;
412
1/2
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
87 if (lbm_create_array(&lbm_res, len_res)) {
413 87 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(lbm_res);
414 87 tmp = (char*)arr->data;
415 } else {
416 ✗ return ENC_SYM_MERROR;
417 }
418
419 // first time through the loop, all the variable are set correctly
420 // from here on,
421 // tmp points to the end of the result string
422 // ins points to the next occurrence of rep in orig
423 // orig points to the remainder of orig after "end of rep"
424
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 87 times.
318 while (count--) {
425 231 ins = strstr(orig, rep);
426 231 size_t len_front = (size_t)ins - (size_t)orig;
427 231 tmp = strncpy(tmp, orig, len_front) + len_front;
428 231 tmp = strncpy(tmp, with, len_with) + len_with;
429 231 orig += len_front + len_rep; // move to next "end of rep"
430 }
431 87 strcpy(tmp, orig);
432
433 87 return lbm_res;
434 }
435
436 189 static lbm_value change_case(lbm_value *args, lbm_uint argn, bool to_upper) {
437
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 117 times.
189 if (argn != 1) {
438 72 lbm_set_error_reason((char*)lbm_error_str_num_args);
439 72 return ENC_SYM_EERROR;
440 }
441
442 117 size_t orig_arr_size = 0;
443 117 char *orig = NULL; //lbm_dec_str(args[0]);
444
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 111 times.
117 if (!lbm_dec_str_size(args[0], &orig, &orig_arr_size)) {
445 6 return ENC_SYM_TERROR;
446 }
447
448 111 size_t len = strlen_max(orig,orig_arr_size);
449 lbm_value lbm_res;
450
1/2
✓ Branch 1 taken 111 times.
✗ Branch 2 not taken.
111 if (lbm_create_array(&lbm_res, len + 1)) {
451 111 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(lbm_res);
452
2/2
✓ Branch 0 taken 540 times.
✓ Branch 1 taken 111 times.
651 for (unsigned int i = 0;i < len;i++) {
453
2/2
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 243 times.
540 if (to_upper) {
454 297 ((char*)(arr->data))[i] = (char)toupper(orig[i]);
455 } else {
456 243 ((char*)(arr->data))[i] = (char)tolower(orig[i]);
457 }
458 }
459 111 ((char*)(arr->data))[len] = '\0';
460 111 return lbm_res;
461 } else {
462 ✗ return ENC_SYM_MERROR;
463 }
464 }
465
466 90 static lbm_value ext_str_to_lower(lbm_value *args, lbm_uint argn) {
467 90 return change_case(args, argn, false);
468 }
469
470 99 static lbm_value ext_str_to_upper(lbm_value *args, lbm_uint argn) {
471 99 return change_case(args,argn, true);
472 }
473
474 516 static lbm_value ext_str_cmp(lbm_value *args, lbm_uint argn) {
475
4/4
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 438 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 42 times.
516 if (argn != 2 && argn != 3) {
476 36 lbm_set_error_reason((char*)lbm_error_str_num_args);
477 36 return ENC_SYM_EERROR;
478 }
479
480 480 char *str1 = lbm_dec_str(args[0]);
481
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 441 times.
480 if (!str1) {
482 39 return ENC_SYM_TERROR;
483 }
484
485 441 char *str2 = lbm_dec_str(args[1]);
486
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 402 times.
441 if (!str2) {
487 39 return ENC_SYM_TERROR;
488 }
489
490 402 int n = -1;
491
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 360 times.
402 if (argn == 3) {
492
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 3 times.
42 if (!lbm_is_number(args[2])) {
493 39 return ENC_SYM_TERROR;
494 }
495
496 3 n = lbm_dec_as_i32(args[2]);
497 }
498
499
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 360 times.
363 if (n > 0) {
500 3 return lbm_enc_i(strncmp(str1, str2, (unsigned int)n));
501 } else {
502 360 return lbm_enc_i(strcmp(str1, str2));
503 }
504 }
505
506 // TODO: This is very similar to ext-print. Maybe they can share code.
507 1431 static lbm_value to_str(char *delimiter, lbm_value *args, lbm_uint argn) {
508 1431 const int str_len = 300;
509 1431 char *str = lbm_malloc((lbm_uint)str_len);
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1431 times.
1431 if (!str) {
511 ✗ return ENC_SYM_MERROR;
512 }
513
514 1431 int str_ofs = 0;
515
516
2/2
✓ Branch 0 taken 1827 times.
✓ Branch 1 taken 1431 times.
3258 for (lbm_uint i = 0; i < argn; i ++) {
517 1827 lbm_value t = args[i];
518 1827 int max = str_len - str_ofs - 1;
519
520 char *arr_str;
521 1827 int chars = 0;
522
523
2/2
✓ Branch 1 taken 351 times.
✓ Branch 2 taken 1476 times.
1827 if (lbm_value_is_printable_string(t, &arr_str)) {
524
2/2
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 108 times.
351 if (str_ofs == 0) {
525 243 chars = snprintf(str + str_ofs, (unsigned int)max, "%s", arr_str);
526 } else {
527 108 chars = snprintf(str + str_ofs, (unsigned int)max, "%s%s", delimiter, arr_str);
528 }
529 } else {
530 1476 lbm_print_value(print_val_buffer, 256, t);
531
2/2
✓ Branch 0 taken 1188 times.
✓ Branch 1 taken 288 times.
1476 if (str_ofs == 0) {
532 1188 chars = snprintf(str + str_ofs, (unsigned int)max, "%s", print_val_buffer);
533 } else {
534 288 chars = snprintf(str + str_ofs, (unsigned int)max, "%s%s", delimiter, print_val_buffer);
535 }
536 }
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1827 times.
1827 if (chars >= max) {
538 ✗ str_ofs += max;
539 } else {
540 1827 str_ofs += chars;
541 }
542 }
543
544 lbm_value res;
545
1/2
✓ Branch 1 taken 1431 times.
✗ Branch 2 not taken.
1431 if (lbm_create_array(&res, (lbm_uint)str_ofs + 1)) {
546 1431 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(res);
547 1431 strncpy((char*)arr->data, str, (unsigned int)str_ofs + 1);
548 1431 lbm_free(str);
549 1431 return res;
550 } else {
551 ✗ lbm_free(str);
552 ✗ return ENC_SYM_MERROR;
553 }
554 }
555
556 1395 static lbm_value ext_to_str(lbm_value *args, lbm_uint argn) {
557 1395 return to_str(" ", args, argn);
558 }
559
560 42 static lbm_value ext_to_str_delim(lbm_value *args, lbm_uint argn) {
561
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 39 times.
42 if (argn < 1) {
562 3 lbm_set_error_reason((char*)lbm_error_str_num_args);
563 3 return ENC_SYM_EERROR;
564 }
565
566 39 char *delim = lbm_dec_str(args[0]);
567
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 36 times.
39 if (!delim) {
568 3 return ENC_SYM_TERROR;
569 }
570
571 36 return to_str(delim, args + 1, argn - 1);
572 }
573
574 201 static lbm_value ext_str_len(lbm_value *args, lbm_uint argn) {
575
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 165 times.
201 LBM_CHECK_ARGN(1);
576
577 165 size_t str_arr_size = 0;
578 165 char *str = NULL; //lbm_dec_str(args[0]);
579
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 126 times.
165 if (!lbm_dec_str_size(args[0], &str, &str_arr_size)) {
580 39 return ENC_SYM_TERROR;
581 }
582
583 126 return lbm_enc_i((int)strlen_max(str, str_arr_size));
584 }
585
586 165 static lbm_value ext_str_replicate(lbm_value *args, lbm_uint argn) {
587
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 93 times.
165 if (argn != 2) {
588 72 lbm_set_error_reason((char*)lbm_error_str_num_args);
589 72 return ENC_SYM_EERROR;
590 }
591
592 93 lbm_value res = ENC_SYM_TERROR;
593
594
3/4
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 87 times.
✗ Branch 4 not taken.
180 if (lbm_is_number(args[0]) &&
595 87 lbm_is_number(args[1])) {
596 87 uint32_t len = lbm_dec_as_u32(args[0]);
597 87 uint8_t c = lbm_dec_as_char(args[1]);
598
599 lbm_value lbm_res;
600
1/2
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
87 if (lbm_create_array(&lbm_res, len + 1)) {
601 87 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(lbm_res);
602
2/2
✓ Branch 0 taken 4404 times.
✓ Branch 1 taken 87 times.
4491 for (unsigned int i = 0;i < len;i++) {
603 4404 ((char*)(arr->data))[i] = (char)c;
604 }
605 87 ((char*)(arr->data))[len] = '\0';
606 87 res = lbm_res;
607 } else {
608 ✗ res = ENC_SYM_MERROR;
609 }
610 }
611 93 return res;
612 }
613
614 999 bool ci_strncmp(const char *str1, const char *str2,int n) {
615 999 bool res = true;
616
2/2
✓ Branch 0 taken 1527 times.
✓ Branch 1 taken 330 times.
1857 for (int i = 0; i < n; i ++) {
617
2/2
✓ Branch 0 taken 669 times.
✓ Branch 1 taken 858 times.
1527 if (tolower(str1[i]) != tolower(str2[i])) {
618 669 res = false;
619 669 break;
620 }
621 }
622 999 return res;
623 }
624
625 // signature: (str-find str:byte-array substr [start:int] [occurrence:int] [dir] [case_sensitivity]) -> int
626 // where
627 // seq = string|(..string)
628 // dir = 'left|'right
629 // case_sensitivity = 'case-sensitive | 'case-insensitive
630 1335 static lbm_value ext_str_find(lbm_value *args, lbm_uint argn) {
631
4/4
✓ Branch 0 taken 1296 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1293 times.
1335 if (argn < 2 || 6 < argn) {
632 42 lbm_set_error_reason((char *)lbm_error_str_num_args);
633 42 return ENC_SYM_EERROR;
634 }
635 1293 lbm_array_header_t *str_header = lbm_dec_array_r(args[0]);
636
2/2
✓ Branch 0 taken 1257 times.
✓ Branch 1 taken 36 times.
1293 if (str_header) {
637 1257 const char *str = (const char *)str_header->data;
638 1257 lbm_int str_size = (lbm_int)str_header->size;
639
640 // Guaranteed to be list containing strings.
641 lbm_value substrings;
642 1257 lbm_int min_substr_len = LBM_INT_MAX;
643
2/2
✓ Branch 1 taken 1035 times.
✓ Branch 2 taken 222 times.
1257 if (lbm_is_array_r(args[1])) {
644 1035 substrings = lbm_cons(args[1], ENC_SYM_NIL);
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1035 times.
1035 if (substrings == ENC_SYM_MERROR) {
646 ✗ return ENC_SYM_MERROR;
647 }
648 1035 lbm_array_header_t *header = (lbm_array_header_t *)lbm_car(args[1]);
649 // lbm_is_array_r holds for args[1], so header should be non-null.
650 #ifdef __INFER__
651 __infer_assume(header != NULL);
652 #endif
653
654
655 1035 lbm_int len = (lbm_int)header->size - 1;
656
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1032 times.
1035 if (len < 0) {
657 // substr is zero length array
658 3 return lbm_enc_i(-1);
659 }
660 1032 min_substr_len = len;
661
1/2
✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
222 } else if (lbm_is_list(args[1])) {
662 222 lbm_value current = args[1];
663
2/2
✓ Branch 1 taken 297 times.
✓ Branch 2 taken 222 times.
519 while (lbm_is_cons(current)) {
664 297 lbm_cons_t *cell = lbm_ref_cell(current);
665 297 lbm_value car_val = cell->car;
666 297 lbm_array_header_t *header = lbm_dec_array_r(car_val);
667
1/2
✓ Branch 0 taken 297 times.
✗ Branch 1 not taken.
297 if (header) {
668 297 lbm_int len = (lbm_int)header->size - 1;
669
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 294 times.
297 if (len < 0) {
670 // substr is zero length array
671 3 current = cell->cdr;
672 3 continue;
673 }
674
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 111 times.
294 if (len < min_substr_len) {
675 183 min_substr_len = len;
676 }
677 } else {
678 ✗ lbm_set_error_suspect(args[1]);
679 ✗ lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
680 ✗ return ENC_SYM_TERROR;
681 }
682 294 current = cell->cdr;
683 }
684 222 substrings = args[1];
685 } else {
686 ✗ lbm_set_error_suspect(args[1]);
687 ✗ lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
688 ✗ return ENC_SYM_TERROR;
689 }
690
691 1254 bool to_right = true;
692 1254 bool case_sensitive = true;
693
694 1254 int nums[2] = {0, 0};
695 1254 bool nums_set[2] = {false, false};
696 1254 int num_ix = 0;
697
698
699
2/2
✓ Branch 0 taken 4020 times.
✓ Branch 1 taken 1254 times.
5274 for (int i = 0; i < (int)argn; i ++ ) {
700
3/4
✓ Branch 1 taken 849 times.
✓ Branch 2 taken 3171 times.
✓ Branch 3 taken 849 times.
✗ Branch 4 not taken.
4020 if (lbm_is_number(args[i]) && num_ix < 2) {
701 849 nums_set[num_ix] = true;
702 849 nums[num_ix++] = (int)lbm_dec_as_int(args[i]);
703 }
704
2/2
✓ Branch 1 taken 699 times.
✓ Branch 2 taken 3321 times.
4020 if (lbm_is_symbol(args[i])) {
705 699 lbm_uint symbol = lbm_dec_sym(args[i]);
706
2/2
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 330 times.
699 if (symbol == sym_left) {
707 369 to_right = false;
708
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 72 times.
330 } else if (symbol == sym_case_insensitive) {
709 258 case_sensitive = false;
710 }
711 }
712 }
713
714 1254 uint32_t occurrence = 0;
715
2/2
✓ Branch 0 taken 885 times.
✓ Branch 1 taken 369 times.
1254 lbm_int start = to_right ? 0 : str_size - min_substr_len;
716
2/2
✓ Branch 0 taken 663 times.
✓ Branch 1 taken 591 times.
1254 if (nums_set[0]) {
717 663 start = nums[0];
718 }
719
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 1068 times.
1254 if (nums_set[1]) {
720 186 occurrence = (uint32_t)nums[1];
721 }
722
723
2/2
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 960 times.
1254 if (start < 0) {
724 // start: -1 starts the search at the character index before the final null
725 // byte index.
726 294 start = str_size - 1 + start;
727 }
728
729
4/4
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 885 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 330 times.
1254 if (!to_right && (start > str_size - min_substr_len)) {
730 39 start = str_size - min_substr_len;
731 }
732
4/4
✓ Branch 0 taken 885 times.
✓ Branch 1 taken 330 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 846 times.
1215 else if (to_right && (start < 0)) {
733 39 start = 0;
734 }
735
736
2/2
✓ Branch 0 taken 885 times.
✓ Branch 1 taken 369 times.
1254 lbm_int dir = to_right ? 1 : -1;
737
4/4
✓ Branch 0 taken 3297 times.
✓ Branch 1 taken 639 times.
✓ Branch 2 taken 3747 times.
✓ Branch 3 taken 189 times.
3936 for (lbm_int i = start; to_right ? (i <= str_size - min_substr_len) : (i >= 0); i += dir) {
738 3747 lbm_value current = substrings;
739
2/2
✓ Branch 1 taken 4092 times.
✓ Branch 2 taken 2682 times.
6774 while (lbm_is_cons(current)) {
740 4092 lbm_cons_t *cell = lbm_ref_cell(current);
741 4092 lbm_array_header_t *header = (lbm_array_header_t *)lbm_car(cell->car);
742 4092 lbm_int substr_len = (lbm_int)header->size - 1;
743 4092 const char *substr = (const char *)header->data;
744
745 4092 if (
746
1/2
✓ Branch 0 taken 4092 times.
✗ Branch 1 not taken.
4092 i > str_size - substr_len // substr length runs over str end.
747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4092 times.
4092 || substr_len < 0 // empty substr substr was zero bytes in size
748 ) {
749 ✗ current = cell->cdr;
750 ✗ continue;
751 }
752
753
4/4
✓ Branch 0 taken 3093 times.
✓ Branch 1 taken 999 times.
✓ Branch 2 taken 2172 times.
✓ Branch 3 taken 921 times.
4092 if ((case_sensitive && memcmp(&str[i], substr, (size_t)substr_len) == 0) ||
754
4/4
✓ Branch 0 taken 999 times.
✓ Branch 1 taken 2172 times.
✓ Branch 3 taken 330 times.
✓ Branch 4 taken 669 times.
3171 (!case_sensitive && ci_strncmp(&str[i], substr, (int)substr_len))) {
755
2/2
✓ Branch 0 taken 1065 times.
✓ Branch 1 taken 186 times.
1251 if (occurrence == 0) {
756 1065 return lbm_enc_i(i);
757 }
758 186 occurrence -= 1;
759 }
760 3027 current = cell->cdr;
761 }
762 }
763 189 return lbm_enc_i(-1);
764 } else {
765 36 lbm_set_error_suspect(args[0]);
766 36 lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
767 36 return ENC_SYM_TERROR;
768 }
769 }
770
771 29167 void lbm_string_extensions_init(void) {
772
773 29167 lbm_add_symbol_const("left", &sym_left);
774 29167 lbm_add_symbol_const("nocase", &sym_case_insensitive);
775
776 29167 lbm_add_extension("str-from-n", ext_str_from_n);
777 29167 lbm_add_extension("str-join", ext_str_join);
778 29167 lbm_add_extension("str-to-i", ext_str_to_i);
779 29167 lbm_add_extension("str-to-f", ext_str_to_f);
780 29167 lbm_add_extension("str-part", ext_str_part);
781 29167 lbm_add_extension("str-split", ext_str_split);
782 29167 lbm_add_extension("str-replace", ext_str_replace);
783 29167 lbm_add_extension("str-to-lower", ext_str_to_lower);
784 29167 lbm_add_extension("str-to-upper", ext_str_to_upper);
785 29167 lbm_add_extension("str-cmp", ext_str_cmp);
786 29167 lbm_add_extension("to-str", ext_to_str);
787 29167 lbm_add_extension("to-str-delim", ext_to_str_delim);
788 29167 lbm_add_extension("str-len", ext_str_len);
789 29167 lbm_add_extension("str-replicate", ext_str_replicate);
790 29167 lbm_add_extension("str-find", ext_str_find);
791 29167 }
792