GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions/string_extensions.c
Date: 2025-12-14 15:34:39
Exec Total Coverage
Lines: 430 450 95.6%
Functions: 22 22 100.0%
Branches: 248 268 92.5%

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