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