given a list, return a list after inserting given value at a given position list.insert(list, idx, val, dups = TRUE) -> list RETURN list.insert([0, 1], 0, 2) -> [2, 0, 1]
| 507 | // list.insert(list, idx, val, dups = TRUE) -> list |
| 508 | // RETURN list.insert([0, 1], 0, 2) -> [2, 0, 1] |
| 509 | SIValue AR_INSERT(SIValue *argv, int argc, void *private_data) { |
| 510 | SIValue list = argv[0]; |
| 511 | if(SI_TYPE(list) == T_NULL) { |
| 512 | return SI_NullVal(); |
| 513 | } |
| 514 | |
| 515 | SIValue val = argv[2]; |
| 516 | if(SI_TYPE(val) == T_NULL) { |
| 517 | // in the case where the added value is NULL |
| 518 | // simply return a clone of the list unmodified |
| 519 | return SIArray_Clone(list); |
| 520 | } |
| 521 | |
| 522 | int32_t index = (int32_t)SI_GET_NUMERIC(argv[1]); |
| 523 | uint32_t arrayLen = SIArray_Length(list); |
| 524 | if(!normalize_index(&index, arrayLen, true)) { |
| 525 | // index out of bounds, simply return a clone of the list unmodified |
| 526 | return SIArray_Clone(list); |
| 527 | } |
| 528 | |
| 529 | bool allow_dups = true; // default value |
| 530 | if(argc == 4) { |
| 531 | allow_dups = SIValue_IsTrue(argv[3]); |
| 532 | } |
| 533 | |
| 534 | if(!allow_dups && SIArray_ContainsValue(list, val, NULL)) { |
| 535 | // caller requested no duplicates |
| 536 | // if value already exists in list return the original list |
| 537 | return SIArray_Clone(list); |
| 538 | } |
| 539 | |
| 540 | // we're guarantee value will be added |
| 541 | // allocate a new array |
| 542 | SIValue array = SI_Array(arrayLen + 1); |
| 543 | |
| 544 | // append elements up to index |
| 545 | for(uint i = 0; i < index; i++) { |
| 546 | SIArray_Append(&array, SIArray_Get(list, i)); |
| 547 | } |
| 548 | |
| 549 | // append new value |
| 550 | SIArray_Append(&array, val); |
| 551 | |
| 552 | // append remaining elements |
| 553 | for(uint i = index; i < arrayLen; i++) { |
| 554 | SIArray_Append(&array, SIArray_Get(list, i)); |
| 555 | } |
| 556 | |
| 557 | return array; |
| 558 | } |
| 559 | |
| 560 | static dict *_list2dict |
| 561 | ( |
nothing calls this directly
no test coverage detected