Case When * Case Value [When Option i Then Result i] Else Default end */
| 12 | /* Case When |
| 13 | * Case Value [When Option i Then Result i] Else Default end */ |
| 14 | SIValue AR_CASEWHEN(SIValue *argv, int argc, void *private_data) { |
| 15 | int alternatives = argc - 1; |
| 16 | SIValue d = argv[argc - 1]; |
| 17 | |
| 18 | if((argc % 2) == 0) { |
| 19 | /* Simple form: |
| 20 | * argv[0] - Value |
| 21 | * argv[i] - Option i |
| 22 | * argv[i+1] - Result i |
| 23 | * argv[argc-1] - Default |
| 24 | * |
| 25 | * Evaluate alternatives in order, return first alternatives which |
| 26 | * is equals to Value. */ |
| 27 | SIValue v = argv[0]; |
| 28 | for(int i = 1; i < alternatives; i += 2) { |
| 29 | SIValue a = argv[i]; |
| 30 | int disjointOrNull; |
| 31 | if(SIValue_Compare(v, a, &disjointOrNull) == 0) { |
| 32 | // Return Result i. |
| 33 | // The value's ownership must be transferred to avoid a double free if it is an allocated value. |
| 34 | SIValue retval = argv[i + 1]; |
| 35 | SIValue_MakeVolatile(&argv[i + 1]); |
| 36 | return retval; |
| 37 | } |
| 38 | } |
| 39 | } else { |
| 40 | /* Generic form: |
| 41 | * argv[i] - Option i |
| 42 | * argv[i+1] - Result i |
| 43 | * arg[argc-1] - Default |
| 44 | * |
| 45 | * Evaluate alternatives in order, return first alternatives which |
| 46 | * is not NULL or false. */ |
| 47 | for(int i = 0; i < alternatives; i += 2) { |
| 48 | SIValue a = argv[i]; |
| 49 | // Skip NULL and false options. |
| 50 | if(SIValue_IsNull(a) || ((SI_TYPE(a) & T_BOOL) && SIValue_IsFalse(a))) continue; |
| 51 | // The option was truthy, return the associated value. |
| 52 | // The value's ownership must be transferred to avoid a double free if it is an allocated value. |
| 53 | SIValue retval = argv[i + 1]; |
| 54 | SIValue_MakeVolatile(&argv[i + 1]); |
| 55 | return retval; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | //Did not match against any Option return default. |
| 60 | SIValue_MakeVolatile(&argv[argc - 1]); |
| 61 | return d; |
| 62 | } |
| 63 | |
| 64 | // Coalesce - return the first value which is not null. Defaults to null. |
| 65 | SIValue AR_COALESCE(SIValue *argv, int argc, void *private_data) { |
nothing calls this directly
no test coverage detected