GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions/string_extensions.c
Date: 2026-09-05 08:38:23
Exec Total Coverage
Lines: 432 458 94.3%
Functions: 21 21 100.0%
Branches: 274 324 84.6%

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 262 static bool str_from_n_format_is_safe(const char *format, bool is_float) {
50 262 const char *p = format;
51 262 int num_specs = 0;
52
2/2
✓ Branch 0 taken 430 times.
✓ Branch 1 taken 262 times.
692 while (*p) {
53
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 262 times.
430 if (*p != '%') {
54 168 p++;
55 168 continue;
56 }
57 262 p++;
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (*p == '%') {
59 ✗ p++;
60 ✗ continue;
61 }
62 262 num_specs++;
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (num_specs > 1) return false;
64
65
6/10
✗ Branch 0 not taken.
✓ Branch 1 taken 346 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 346 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 346 times.
✓ Branch 6 taken 84 times.
✓ Branch 7 taken 262 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 262 times.
346 while (*p == '-' || *p == '+' || *p == ' ' || *p == '0' || *p == '#') p++;
66
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 262 times.
346 while (isdigit((unsigned char)*p)) p++;
67
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 84 times.
262 if (*p == '.') {
68 178 p++;
69
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 178 times.
356 while (isdigit((unsigned char)*p)) p++;
70 }
71
72 262 char c = *p;
73
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 84 times.
262 if (is_float) {
74
1/12
✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
178 if (!(c == 'f' || c == 'F' || c == 'e' || c == 'E' ||
75 ✗ c == 'g' || c == 'G' || c == 'a' || c == 'A')) {
76 ✗ return false;
77 }
78 } else {
79
4/10
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 84 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
84 if (!(c == 'd' || c == 'i' || c == 'o' ||
80
2/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
84 c == 'u' || c == 'x' || c == 'X' || c == 'c')) {
81 ✗ return false;
82 }
83 }
84 262 p++;
85 }
86 262 return num_specs == 1;
87 }
88
89 120653 static lbm_value ext_str_from_n(lbm_value *args, lbm_uint argn) {
90
4/4
✓ Branch 0 taken 604 times.
✓ Branch 1 taken 120049 times.
✓ Branch 2 taken 252 times.
✓ Branch 3 taken 352 times.
120653 if (argn != 1 && argn != 2) {
91 252 lbm_set_error_reason((char*)lbm_error_str_num_args);
92 252 return ENC_SYM_EERROR;
93 }
94
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 120230 times.
120401 if (!lbm_is_number(args[0])) {
95 171 return ENC_SYM_TERROR;
96 }
97
98
4/4
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 119878 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 262 times.
120230 if (argn == 2 && !lbm_is_array_r(args[1])) {
99 90 return ENC_SYM_TERROR;
100 }
101
102 120140 char *format = 0;
103
2/2
✓ Branch 0 taken 262 times.
✓ Branch 1 taken 119878 times.
120140 if (argn == 2) {
104 262 format = lbm_dec_str(args[1]);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (!format) {
106 ✗ return ENC_SYM_TERROR;
107 }
108 }
109
110
4/4
✓ Branch 0 taken 119969 times.
✓ Branch 1 taken 171 times.
✓ Branch 2 taken 511 times.
✓ Branch 3 taken 119458 times.
240109 bool is_float = (lbm_type_of_functional(args[0]) == LBM_TYPE_DOUBLE ||
111 119969 lbm_type_of_functional(args[0]) == LBM_TYPE_FLOAT);
112
113
3/4
✓ Branch 0 taken 262 times.
✓ Branch 1 taken 119878 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 262 times.
120140 if (format && !str_from_n_format_is_safe(format, is_float)) {
114 ✗ lbm_set_error_reason((char*)lbm_error_str_incorrect_arg);
115 ✗ lbm_set_error_suspect(args[1]);
116 ✗ return ENC_SYM_TERROR;
117 }
118
119 char buffer[100];
120 120140 size_t len = 0;
121
122
2/2
✓ Branch 0 taken 682 times.
✓ Branch 1 taken 119458 times.
120140 switch (lbm_type_of_functional(args[0])) {
123 682 case LBM_TYPE_DOUBLE: /* fall through */
124 case LBM_TYPE_FLOAT:
125
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 178 times.
682 if (!format) {
126 504 format = "%g";
127 }
128 682 len = (size_t)snprintf(buffer, sizeof(buffer), format, lbm_dec_as_double(args[0]));
129 682 break;
130
131 119458 default:
132
2/2
✓ Branch 0 taken 119374 times.
✓ Branch 1 taken 84 times.
119458 if (!format) {
133 119374 format = "%d";
134 }
135 119458 len = (size_t)snprintf(buffer, sizeof(buffer), format, lbm_dec_as_i32(args[0]));
136 119458 break;
137 }
138
139 120140 len = MIN(len, sizeof(buffer));
140
141 120140 return span_to_lbm(buffer, len);
142 }
143
144 // signature: (str-join strings [delim]) -> str
145 288375 static lbm_value ext_str_join(lbm_value *args, lbm_uint argn) {
146 // This function does not check that the string arguments contain any
147 // terminating null bytes.
148
149
4/4
✓ Branch 0 taken 253059 times.
✓ Branch 1 taken 35316 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 252975 times.
288375 if (argn != 1 && argn != 2) {
150 84 lbm_set_error_reason((char *)lbm_error_str_num_args);
151 84 return ENC_SYM_EERROR;
152 }
153
154 288291 size_t str_len = 0;
155 288291 size_t str_count = 0;
156
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 288204 times.
288291 if (!lbm_is_list(args[0])) {
157 87 lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
158 87 lbm_set_error_suspect(args[0]);
159 87 return ENC_SYM_TERROR;
160 }
161 288204 lbm_value current = args[0];
162
2/2
✓ Branch 0 taken 576900 times.
✓ Branch 1 taken 288117 times.
865017 while (lbm_is_cons(current)) {
163 576900 lbm_cons_t *cell = lbm_ref_cell(current);
164 576900 lbm_value car_val = cell->car;
165 576900 char *str = NULL;
166 576900 size_t arr_size = 0;
167
2/2
✓ Branch 0 taken 576813 times.
✓ Branch 1 taken 87 times.
576900 if (lbm_dec_str_size(car_val, &str, &arr_size)) {
168 576813 str_len += strlen_max(str, arr_size);
169 576813 str_count += 1;
170 } else {
171 87 lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
172 87 lbm_set_error_suspect(args[0]);
173 87 return ENC_SYM_TERROR;
174 }
175 576813 current = cell->cdr;
176 }
177
178 288117 const char *delim = "";
179
2/2
✓ Branch 0 taken 252807 times.
✓ Branch 1 taken 35310 times.
288117 if (argn >= 2) {
180 252807 delim = lbm_dec_str(args[1]);
181
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 252720 times.
252807 if (!delim) {
182 87 lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
183 87 lbm_set_error_suspect(args[1]);
184 87 return ENC_SYM_TERROR;
185 }
186 }
187
188 288030 size_t delim_len = strlen(delim);
189
2/2
✓ Branch 0 taken 203766 times.
✓ Branch 1 taken 84264 times.
288030 if (str_count > 0) {
190 203766 str_len += (str_count - 1) * delim_len;
191 }
192
193 lbm_value result;
194
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 287742 times.
288030 if (!lbm_create_array(&result, str_len + 1)) {
195 288 return ENC_SYM_MERROR;
196 }
197 287742 char *result_str = lbm_dec_str(result);
198
199 287742 size_t i = 0;
200 287742 size_t offset = 0;
201 287742 current = args[0];
202
2/2
✓ Branch 0 taken 576060 times.
✓ Branch 1 taken 287742 times.
863802 while (lbm_is_cons(current)) {
203 576060 lbm_cons_t *cell = lbm_ref_cell(current);
204 576060 lbm_value car_val = cell->car;
205 // All arrays have been prechecked.
206 576060 lbm_array_header_t *array = (lbm_array_header_t*) lbm_car(car_val);
207 576060 char *str = (char*)array->data;
208 576060 size_t len = strlen_max(str, array->size);
209
210 576060 memcpy(result_str + offset, str, len);
211 576060 offset += len;
212
213
2/2
✓ Branch 0 taken 372486 times.
✓ Branch 1 taken 203574 times.
576060 if (i != str_count - 1) {
214 372486 memcpy(result_str + offset, delim, delim_len);
215 372486 offset += delim_len;
216 }
217 576060 i++;
218 576060 current = cell->cdr;
219 }
220
221 287742 result_str[str_len] = '\0';
222
223 287742 return result;
224 }
225
226 1353 static lbm_value ext_str_to_i(lbm_value *args, lbm_uint argn) {
227
4/4
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 1182 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 87 times.
1353 if (argn != 1 && argn != 2) {
228 84 lbm_set_error_reason((char*)lbm_error_str_num_args);
229 84 return ENC_SYM_EERROR;
230 }
231
232 1269 char *str = lbm_dec_str(args[0]);
233
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 1182 times.
1269 if (!str) {
234 87 return ENC_SYM_TERROR;
235 }
236
237 1182 int base = 0;
238
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 1095 times.
1182 if (argn == 2) {
239
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 84 times.
87 if (!lbm_is_number(args[1])) {
240 3 return ENC_SYM_TERROR;
241 }
242
243 84 base = (int)lbm_dec_as_u32(args[1]);
244 }
245
246 1179 return lbm_enc_i32((int32_t)strtol(str, NULL, base));
247 }
248
249 339 static lbm_value ext_str_to_f(lbm_value *args, lbm_uint argn) {
250
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 255 times.
339 if (argn != 1) {
251 84 lbm_set_error_reason((char*)lbm_error_str_num_args);
252 84 return ENC_SYM_EERROR;
253 }
254
255 255 char *str = lbm_dec_str(args[0]);
256
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 168 times.
255 if (!str) {
257 87 return ENC_SYM_TERROR;
258 }
259
260 168 return lbm_enc_float(strtof(str, NULL));
261 }
262
263 405 static lbm_value ext_str_part(lbm_value *args, lbm_uint argn) {
264
6/6
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 225 times.
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 318 times.
405 if ((argn != 2 && argn != 3) || !lbm_is_number(args[1])) {
265 87 lbm_set_error_reason((char*)lbm_error_str_num_args);
266 87 return ENC_SYM_TERROR;
267 }
268
269 318 size_t str_arr_len = 0;
270 318 char *str = NULL;//lbm_dec_str(args[0]);
271
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 315 times.
318 if (!lbm_dec_str_size(args[0], &str, &str_arr_len)) {
272 3 return ENC_SYM_TERROR;
273 }
274
275 315 uint32_t len = (uint32_t)strlen_max(str, str_arr_len);
276
277 315 uint32_t start = lbm_dec_as_u32(args[1]);
278
279
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 309 times.
315 if (start >= len) {
280 6 return ENC_SYM_EERROR;
281 }
282
283 309 uint32_t n = len - start;
284
2/2
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 84 times.
309 if (argn == 3) {
285
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 222 times.
225 if (!lbm_is_number(args[2])) {
286 3 return ENC_SYM_TERROR;
287 }
288
289
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 21 times.
222 n = MIN(lbm_dec_as_u32(args[2]), n);
290 }
291
292 306 return span_to_lbm(str + start, n);
293 }
294
295 3975 static bool char_in(char c, char *delim, unsigned int max_ix) {
296 3975 char *d = delim;
297 3975 unsigned int i = 0;
298
3/4
✓ Branch 0 taken 7602 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4899 times.
✓ Branch 3 taken 2703 times.
7602 while (i < max_ix && *d != '\0' ) {
299
2/2
✓ Branch 0 taken 1272 times.
✓ Branch 1 taken 3627 times.
4899 if (c == *d) return true;
300 3627 d++; i++;
301 }
302 2703 return false;
303 }
304
305 714 static lbm_value ext_str_split(lbm_value *args, lbm_uint argn) {
306
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 627 times.
714 if (argn != 2) {
307 87 lbm_set_error_reason((char*)lbm_error_str_num_args);
308 87 return ENC_SYM_TERROR;
309 }
310
311 627 size_t str_arr_size = 0;
312 627 char *str = NULL; //lbm_dec_str(args[0]);
313
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 624 times.
627 if (!lbm_dec_str_size(args[0], &str, &str_arr_size)) {
314 3 return ENC_SYM_TERROR;
315 }
316
317 624 char *delim = NULL;
318 624 size_t delim_arr_size = 0;
319
320
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 450 times.
624 if (lbm_is_number(args[1])) {
321
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 84 times.
174 int step = MAX(lbm_dec_as_i32(args[1]), 1);
322 174 lbm_value res = ENC_SYM_NIL;
323 174 int len = (int)strlen_max(str, str_arr_size);
324
2/2
✓ Branch 0 taken 1686 times.
✓ Branch 1 taken 174 times.
1860 for (int i = len / step;i >= 0;i--) {
325 1686 int ind_now = i * step;
326
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1602 times.
1686 if (ind_now >= len) {
327 84 continue;
328 }
329
330 1602 int step_now = step;
331
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 1602 times.
1890 while ((ind_now + step_now) > len) {
332 288 step_now--;
333 }
334
335 1602 lbm_value tok = span_to_lbm(str + ind_now, (size_t)step_now);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1602 times.
1602 if (tok == ENC_SYM_MERROR) return tok;
337 1602 res = lbm_cons(tok, res);
338 }
339 174 return res;
340
1/2
✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
450 } else if (lbm_dec_str_size(args[1], &delim, &delim_arr_size)) {
341 450 lbm_value res = ENC_SYM_NIL;
342
343 450 unsigned int i_start = 0;
344 450 unsigned int i_end = 0;
345
346 // Abort when larger that array size. Protection against abuse
347 // with byte-arrays.
348
1/2
✓ Branch 0 taken 1722 times.
✗ Branch 1 not taken.
1722 while (i_end < str_arr_size) {
349
350
4/4
✓ Branch 0 taken 3975 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 2703 times.
✓ Branch 3 taken 1272 times.
4425 while (str[i_end] != '\0' && !char_in(str[i_end], delim, (unsigned int)delim_arr_size)) {
351 2703 i_end ++;
352 }
353
354 1722 unsigned int len = i_end - i_start;
355 1722 char *s = &str[i_start];
356 1722 lbm_value tok = span_to_lbm(s, len);
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1722 times.
1722 if (tok == ENC_SYM_MERROR) return tok;
358 1722 res = lbm_cons(tok, res);
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1722 times.
1722 if (res == ENC_SYM_MERROR) return res;
360
361
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 1272 times.
1722 if (str[i_end] == '\0') break;
362 1272 i_start = i_end + 1;
363 1272 i_end = i_end + 1;
364
365 }
366 450 return lbm_list_destructive_reverse(res);
367 }
368 ✗ return ENC_SYM_TERROR;
369 }
370
371 // Todo: Clean this up for 64bit
372 300 static lbm_value ext_str_replace(lbm_value *args, lbm_uint argn) {
373
4/4
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 120 times.
300 if (argn != 2 && argn != 3) {
374 90 lbm_set_error_reason((char*)lbm_error_str_num_args);
375 90 return ENC_SYM_EERROR;
376 }
377
378 210 size_t orig_arr_size = 0;
379 210 char *orig = NULL; // lbm_dec_str(args[0]);
380
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 204 times.
210 if (!lbm_dec_str_size(args[0], &orig, &orig_arr_size)) {
381 6 return ENC_SYM_TERROR;
382 }
383
384 204 size_t rep_arr_size = 0;
385 204 char *rep = NULL; //lbm_dec_str(args[1]);
386
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 195 times.
204 if (!lbm_dec_str_size(args[1], &rep, &rep_arr_size)) {
387 9 return ENC_SYM_TERROR;
388 }
389
390 195 size_t with_arr_size = 0;
391 195 char *with = "";
392
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 87 times.
195 if (argn == 3) {
393
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 102 times.
108 if (!lbm_dec_str_size(args[2], &with, &with_arr_size)) {
394 6 return ENC_SYM_TERROR;
395 }
396 }
397
398 // See https://stackoverflow.com/questions/779875/what-function-is-to-replace-a-substring-from-a-string-in-c
399 //char *result; // the return string
400 char *ins; // the next insert point
401 char *tmp; // varies
402 size_t len_rep; // length of rep (the string to remove)
403 size_t len_with; // length of with (the string to replace rep with)
404 //size_t len_front; // distance between rep and end of last rep
405 int count; // number of replacements
406
407 189 len_rep = strlen_max(rep, rep_arr_size);
408
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 183 times.
189 if (len_rep == 0) {
409 6 return args[0]; // empty rep causes infinite loop during count
410 }
411
412 183 len_with = strlen_max(with,with_arr_size);
413
414 // count the number of replacements needed
415 183 ins = orig;
416
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 183 times.
510 for (count = 0; (tmp = strstr(ins, rep)); ++count) {
417 327 ins = tmp + len_rep;
418 }
419
420 183 size_t len_res = strlen_max(orig, orig_arr_size) + (len_with - len_rep) * (unsigned int)count + 1;
421 lbm_value lbm_res;
422
1/2
✓ Branch 0 taken 183 times.
✗ Branch 1 not taken.
183 if (lbm_create_array(&lbm_res, len_res)) {
423 183 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(lbm_res);
424 183 tmp = (char*)arr->data;
425 } else {
426 ✗ return ENC_SYM_MERROR;
427 }
428
429 // first time through the loop, all the variable are set correctly
430 // from here on,
431 // tmp points to the end of the result string
432 // ins points to the next occurrence of rep in orig
433 // orig points to the remainder of orig after "end of rep"
434
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 183 times.
510 while (count--) {
435 327 ins = strstr(orig, rep);
436 327 size_t len_front = (size_t)ins - (size_t)orig;
437 327 tmp = strncpy(tmp, orig, len_front) + len_front;
438 327 tmp = strncpy(tmp, with, len_with) + len_with;
439 327 orig += len_front + len_rep; // move to next "end of rep"
440 }
441 183 strcpy(tmp, orig);
442
443 183 return lbm_res;
444 }
445
446 381 static lbm_value change_case(lbm_value *args, lbm_uint argn, bool to_upper) {
447
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 213 times.
381 if (argn != 1) {
448 168 lbm_set_error_reason((char*)lbm_error_str_num_args);
449 168 return ENC_SYM_EERROR;
450 }
451
452 213 size_t orig_arr_size = 0;
453 213 char *orig = NULL; //lbm_dec_str(args[0]);
454
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 207 times.
213 if (!lbm_dec_str_size(args[0], &orig, &orig_arr_size)) {
455 6 return ENC_SYM_TERROR;
456 }
457
458 207 size_t len = strlen_max(orig,orig_arr_size);
459 lbm_value lbm_res;
460
1/2
✓ Branch 0 taken 207 times.
✗ Branch 1 not taken.
207 if (lbm_create_array(&lbm_res, len + 1)) {
461 207 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(lbm_res);
462
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 207 times.
1227 for (unsigned int i = 0;i < len;i++) {
463
2/2
✓ Branch 0 taken 537 times.
✓ Branch 1 taken 483 times.
1020 if (to_upper) {
464 537 ((char*)(arr->data))[i] = (char)toupper(orig[i]);
465 } else {
466 483 ((char*)(arr->data))[i] = (char)tolower(orig[i]);
467 }
468 }
469 207 ((char*)(arr->data))[len] = '\0';
470 207 return lbm_res;
471 } else {
472 ✗ return ENC_SYM_MERROR;
473 }
474 }
475
476 186 static lbm_value ext_str_to_lower(lbm_value *args, lbm_uint argn) {
477 186 return change_case(args, argn, false);
478 }
479
480 195 static lbm_value ext_str_to_upper(lbm_value *args, lbm_uint argn) {
481 195 return change_case(args,argn, true);
482 }
483
484 1092 static lbm_value ext_str_cmp(lbm_value *args, lbm_uint argn) {
485
4/4
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 918 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 90 times.
1092 if (argn != 2 && argn != 3) {
486 84 lbm_set_error_reason((char*)lbm_error_str_num_args);
487 84 return ENC_SYM_EERROR;
488 }
489
490 1008 char *str1 = lbm_dec_str(args[0]);
491
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 921 times.
1008 if (!str1) {
492 87 return ENC_SYM_TERROR;
493 }
494
495 921 char *str2 = lbm_dec_str(args[1]);
496
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 834 times.
921 if (!str2) {
497 87 return ENC_SYM_TERROR;
498 }
499
500 834 int n = -1;
501
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 744 times.
834 if (argn == 3) {
502
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 3 times.
90 if (!lbm_is_number(args[2])) {
503 87 return ENC_SYM_TERROR;
504 }
505
506 3 n = lbm_dec_as_i32(args[2]);
507 }
508
509
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 744 times.
747 if (n > 0) {
510 3 return lbm_enc_i(strncmp(str1, str2, (unsigned int)n));
511 } else {
512 744 return lbm_enc_i(strcmp(str1, str2));
513 }
514 }
515
516 // TODO: This is very similar to ext-print. Maybe they can share code.
517 3303 static lbm_value to_str(char *delimiter, lbm_value *args, lbm_uint argn) {
518 3303 const int str_len = 300;
519 3303 char *str = lbm_malloc((lbm_uint)str_len);
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3303 times.
3303 if (!str) {
521 ✗ return ENC_SYM_MERROR;
522 }
523
524 3303 int str_ofs = 0;
525
526
2/2
✓ Branch 0 taken 4227 times.
✓ Branch 1 taken 3303 times.
7530 for (lbm_uint i = 0; i < argn; i ++) {
527 4227 lbm_value t = args[i];
528 4227 int max = str_len - str_ofs - 1;
529
530 char *arr_str;
531 4227 int chars = 0;
532
533
2/2
✓ Branch 0 taken 783 times.
✓ Branch 1 taken 3444 times.
4227 if (lbm_value_is_printable_string(t, &arr_str)) {
534
2/2
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 252 times.
783 if (str_ofs == 0) {
535 531 chars = snprintf(str + str_ofs, (unsigned int)max, "%s", arr_str);
536 } else {
537 252 chars = snprintf(str + str_ofs, (unsigned int)max, "%s%s", delimiter, arr_str);
538 }
539 } else {
540 3444 lbm_print_value(print_val_buffer, 256, t);
541
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 672 times.
3444 if (str_ofs == 0) {
542 2772 chars = snprintf(str + str_ofs, (unsigned int)max, "%s", print_val_buffer);
543 } else {
544 672 chars = snprintf(str + str_ofs, (unsigned int)max, "%s%s", delimiter, print_val_buffer);
545 }
546 }
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4227 times.
4227 if (chars >= max) {
548 ✗ str_ofs += max;
549 } else {
550 4227 str_ofs += chars;
551 }
552 }
553
554 lbm_value res;
555
1/2
✓ Branch 0 taken 3303 times.
✗ Branch 1 not taken.
3303 if (lbm_create_array(&res, (lbm_uint)str_ofs + 1)) {
556 3303 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(res);
557 3303 strncpy((char*)arr->data, str, (unsigned int)str_ofs + 1);
558 3303 lbm_free(str);
559 3303 return res;
560 } else {
561 ✗ lbm_free(str);
562 ✗ return ENC_SYM_MERROR;
563 }
564 }
565
566 3219 static lbm_value ext_to_str(lbm_value *args, lbm_uint argn) {
567 3219 return to_str(" ", args, argn);
568 }
569
570 90 static lbm_value ext_to_str_delim(lbm_value *args, lbm_uint argn) {
571
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 87 times.
90 if (argn < 1) {
572 3 lbm_set_error_reason((char*)lbm_error_str_num_args);
573 3 return ENC_SYM_EERROR;
574 }
575
576 87 char *delim = lbm_dec_str(args[0]);
577
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 84 times.
87 if (!delim) {
578 3 return ENC_SYM_TERROR;
579 }
580
581 84 return to_str(delim, args + 1, argn - 1);
582 }
583
584 345 static lbm_value ext_str_len(lbm_value *args, lbm_uint argn) {
585
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 261 times.
345 LBM_CHECK_ARGN(1);
586
587 261 size_t str_arr_size = 0;
588 261 char *str = NULL; //lbm_dec_str(args[0]);
589
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 174 times.
261 if (!lbm_dec_str_size(args[0], &str, &str_arr_size)) {
590 87 return ENC_SYM_TERROR;
591 }
592
593 174 return lbm_enc_i((int)strlen_max(str, str_arr_size));
594 }
595
596 309 static lbm_value ext_str_replicate(lbm_value *args, lbm_uint argn) {
597
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 141 times.
309 if (argn != 2) {
598 168 lbm_set_error_reason((char*)lbm_error_str_num_args);
599 168 return ENC_SYM_EERROR;
600 }
601
602 141 lbm_value res = ENC_SYM_TERROR;
603
604
3/4
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 135 times.
✗ Branch 3 not taken.
276 if (lbm_is_number(args[0]) &&
605 135 lbm_is_number(args[1])) {
606 135 uint32_t len = lbm_dec_as_u32(args[0]);
607 135 uint8_t c = lbm_dec_as_char(args[1]);
608
609 lbm_value lbm_res;
610
1/2
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
135 if (lbm_create_array(&lbm_res, len + 1)) {
611 135 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(lbm_res);
612
2/2
✓ Branch 0 taken 4596 times.
✓ Branch 1 taken 135 times.
4731 for (unsigned int i = 0;i < len;i++) {
613 4596 ((char*)(arr->data))[i] = (char)c;
614 }
615 135 ((char*)(arr->data))[len] = '\0';
616 135 res = lbm_res;
617 } else {
618 ✗ res = ENC_SYM_MERROR;
619 }
620 }
621 141 return res;
622 }
623
624 2295 bool ci_strncmp(const char *str1, const char *str2,int n) {
625 2295 bool res = true;
626
2/2
✓ Branch 0 taken 3495 times.
✓ Branch 1 taken 762 times.
4257 for (int i = 0; i < n; i ++) {
627
2/2
✓ Branch 0 taken 1533 times.
✓ Branch 1 taken 1962 times.
3495 if (tolower(str1[i]) != tolower(str2[i])) {
628 1533 res = false;
629 1533 break;
630 }
631 }
632 2295 return res;
633 }
634
635 // signature: (str-find str:byte-array substr [start:int] [occurrence:int] [dir] [case_sensitivity]) -> int
636 // where
637 // seq = string|(..string)
638 // dir = 'left|'right
639 // case_sensitivity = 'case-sensitive | 'case-insensitive
640 2967 static lbm_value ext_str_find(lbm_value *args, lbm_uint argn) {
641
4/4
✓ Branch 0 taken 2880 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2877 times.
2967 if (argn < 2 || 6 < argn) {
642 90 lbm_set_error_reason((char *)lbm_error_str_num_args);
643 90 return ENC_SYM_EERROR;
644 }
645 2877 lbm_array_header_t *str_header = lbm_dec_array_r(args[0]);
646
2/2
✓ Branch 0 taken 2793 times.
✓ Branch 1 taken 84 times.
2877 if (str_header) {
647 2793 const char *str = (const char *)str_header->data;
648 2793 lbm_int str_size = (lbm_int)str_header->size;
649
650 // Guaranteed to be list containing strings.
651 lbm_value substrings;
652 2793 lbm_int min_substr_len = LBM_INT_MAX;
653
2/2
✓ Branch 0 taken 2283 times.
✓ Branch 1 taken 510 times.
2793 if (lbm_is_array_r(args[1])) {
654 2283 substrings = lbm_cons(args[1], ENC_SYM_NIL);
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2283 times.
2283 if (substrings == ENC_SYM_MERROR) {
656 ✗ return ENC_SYM_MERROR;
657 }
658 2283 lbm_array_header_t *header = (lbm_array_header_t *)lbm_car(args[1]);
659 // lbm_is_array_r holds for args[1], so header should be non-null.
660 #ifdef __INFER__
661 __infer_assume(header != NULL);
662 #endif
663
664
665 2283 lbm_int len = (lbm_int)header->size - 1;
666
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2280 times.
2283 if (len < 0) {
667 // substr is zero length array
668 3 return lbm_enc_i(-1);
669 }
670 2280 min_substr_len = len;
671
1/2
✓ Branch 0 taken 510 times.
✗ Branch 1 not taken.
510 } else if (lbm_is_list(args[1])) {
672 510 lbm_value current = args[1];
673
2/2
✓ Branch 0 taken 681 times.
✓ Branch 1 taken 510 times.
1191 while (lbm_is_cons(current)) {
674 681 lbm_cons_t *cell = lbm_ref_cell(current);
675 681 lbm_value car_val = cell->car;
676 681 lbm_array_header_t *header = lbm_dec_array_r(car_val);
677
1/2
✓ Branch 0 taken 681 times.
✗ Branch 1 not taken.
681 if (header) {
678 681 lbm_int len = (lbm_int)header->size - 1;
679
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 678 times.
681 if (len < 0) {
680 // substr is zero length array
681 3 current = cell->cdr;
682 3 continue;
683 }
684
2/2
✓ Branch 0 taken 423 times.
✓ Branch 1 taken 255 times.
678 if (len < min_substr_len) {
685 423 min_substr_len = len;
686 }
687 } else {
688 ✗ lbm_set_error_suspect(args[1]);
689 ✗ lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
690 ✗ return ENC_SYM_TERROR;
691 }
692 678 current = cell->cdr;
693 }
694 510 substrings = args[1];
695 } else {
696 ✗ lbm_set_error_suspect(args[1]);
697 ✗ lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
698 ✗ return ENC_SYM_TERROR;
699 }
700
701 2790 bool to_right = true;
702 2790 bool case_sensitive = true;
703
704 2790 int nums[2] = {0, 0};
705 2790 bool nums_set[2] = {false, false};
706 2790 int num_ix = 0;
707
708
709
2/2
✓ Branch 0 taken 9060 times.
✓ Branch 1 taken 2790 times.
11850 for (int i = 0; i < (int)argn; i ++ ) {
710
3/4
✓ Branch 0 taken 1953 times.
✓ Branch 1 taken 7107 times.
✓ Branch 2 taken 1953 times.
✗ Branch 3 not taken.
9060 if (lbm_is_number(args[i]) && num_ix < 2) {
711 1953 nums_set[num_ix] = true;
712 1953 nums[num_ix++] = (int)lbm_dec_as_int(args[i]);
713 }
714
2/2
✓ Branch 0 taken 1611 times.
✓ Branch 1 taken 7449 times.
9060 if (lbm_is_symbol(args[i])) {
715 1611 lbm_uint symbol = lbm_dec_sym(args[i]);
716
2/2
✓ Branch 0 taken 849 times.
✓ Branch 1 taken 762 times.
1611 if (symbol == sym_left) {
717 849 to_right = false;
718
2/2
✓ Branch 0 taken 594 times.
✓ Branch 1 taken 168 times.
762 } else if (symbol == sym_case_insensitive) {
719 594 case_sensitive = false;
720 }
721 }
722 }
723
724 2790 uint32_t occurrence = 0;
725
2/2
✓ Branch 0 taken 1941 times.
✓ Branch 1 taken 849 times.
2790 lbm_int start = to_right ? 0 : str_size - min_substr_len;
726
2/2
✓ Branch 0 taken 1527 times.
✓ Branch 1 taken 1263 times.
2790 if (nums_set[0]) {
727 1527 start = nums[0];
728 }
729
2/2
✓ Branch 0 taken 426 times.
✓ Branch 1 taken 2364 times.
2790 if (nums_set[1]) {
730 426 occurrence = (uint32_t)nums[1];
731 }
732
733
2/2
✓ Branch 0 taken 678 times.
✓ Branch 1 taken 2112 times.
2790 if (start < 0) {
734 // start: -1 starts the search at the character index before the final null
735 // byte index.
736 678 start = str_size - 1 + start;
737 }
738
739
4/4
✓ Branch 0 taken 849 times.
✓ Branch 1 taken 1941 times.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 762 times.
2790 if (!to_right && (start > str_size - min_substr_len)) {
740 87 start = str_size - min_substr_len;
741 }
742
4/4
✓ Branch 0 taken 1941 times.
✓ Branch 1 taken 762 times.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 1854 times.
2703 else if (to_right && (start < 0)) {
743 87 start = 0;
744 }
745
746
2/2
✓ Branch 0 taken 1941 times.
✓ Branch 1 taken 849 times.
2790 lbm_int dir = to_right ? 1 : -1;
747
4/4
✓ Branch 0 taken 5841 times.
✓ Branch 1 taken 1455 times.
✓ Branch 2 taken 6867 times.
✓ Branch 3 taken 429 times.
7296 for (lbm_int i = start; to_right ? (i <= str_size - min_substr_len) : (i >= 0); i += dir) {
748 6867 lbm_value current = substrings;
749
2/2
✓ Branch 0 taken 7644 times.
✓ Branch 1 taken 4506 times.
12150 while (lbm_is_cons(current)) {
750 7644 lbm_cons_t *cell = lbm_ref_cell(current);
751 7644 lbm_array_header_t *header = (lbm_array_header_t *)lbm_car(cell->car);
752 7644 lbm_int substr_len = (lbm_int)header->size - 1;
753 7644 const char *substr = (const char *)header->data;
754
755 7644 if (
756
1/2
✓ Branch 0 taken 7644 times.
✗ Branch 1 not taken.
7644 i > str_size - substr_len // substr length runs over str end.
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7644 times.
7644 || substr_len < 0 // empty substr substr was zero bytes in size
758 ) {
759 ✗ current = cell->cdr;
760 ✗ continue;
761 }
762
763
4/4
✓ Branch 0 taken 5349 times.
✓ Branch 1 taken 2295 times.
✓ Branch 2 taken 3324 times.
✓ Branch 3 taken 2025 times.
7644 if ((case_sensitive && memcmp(&str[i], substr, (size_t)substr_len) == 0) ||
764
4/4
✓ Branch 0 taken 2295 times.
✓ Branch 1 taken 3324 times.
✓ Branch 2 taken 762 times.
✓ Branch 3 taken 1533 times.
5619 (!case_sensitive && ci_strncmp(&str[i], substr, (int)substr_len))) {
765
2/2
✓ Branch 0 taken 2361 times.
✓ Branch 1 taken 426 times.
2787 if (occurrence == 0) {
766 2361 return lbm_enc_i(i);
767 }
768 426 occurrence -= 1;
769 }
770 5283 current = cell->cdr;
771 }
772 }
773 429 return lbm_enc_i(-1);
774 } else {
775 84 lbm_set_error_suspect(args[0]);
776 84 lbm_set_error_reason((char *)lbm_error_str_incorrect_arg);
777 84 return ENC_SYM_TERROR;
778 }
779 }
780
781 66906 void lbm_string_extensions_init(void) {
782
783 66906 lbm_add_symbol_const("left", &sym_left);
784 66906 lbm_add_symbol_const("nocase", &sym_case_insensitive);
785
786 66906 lbm_add_extension("str-from-n", ext_str_from_n);
787 66906 lbm_add_extension("str-join", ext_str_join);
788 66906 lbm_add_extension("str-to-i", ext_str_to_i);
789 66906 lbm_add_extension("str-to-f", ext_str_to_f);
790 66906 lbm_add_extension("str-part", ext_str_part);
791 66906 lbm_add_extension("str-split", ext_str_split);
792 66906 lbm_add_extension("str-replace", ext_str_replace);
793 66906 lbm_add_extension("str-to-lower", ext_str_to_lower);
794 66906 lbm_add_extension("str-to-upper", ext_str_to_upper);
795 66906 lbm_add_extension("str-cmp", ext_str_cmp);
796 66906 lbm_add_extension("to-str", ext_to_str);
797 66906 lbm_add_extension("to-str-delim", ext_to_str_delim);
798 66906 lbm_add_extension("str-len", ext_str_len);
799 66906 lbm_add_extension("str-replicate", ext_str_replicate);
800 66906 lbm_add_extension("str-find", ext_str_find);
801 66906 }
802