GCC Code Coverage Report


Directory: ../src/
File: extensions/string_extensions.c
Date: 2026-07-02 10:10:49
Coverage Exec Excl Total
Lines: 95.5% 426 0 446
Functions: 100.0% 21 0 21
Branches: 92.4% 244 0 264

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