Avoid subtracting form .length

user1234 user1234 at 12.de
Thu Nov 14 14:31:05 UTC 2024


On Thursday, 14 November 2024 at 06:53:01 UTC, Salih Dincer wrote:
> On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote:
>> I _very_ often use this pattern:
>>
>> ```d
>> fun(ref int[] a)
>> {
>>    assert(a.length && a.length<=100);
>>    int[100] b;
>>    b[0 .. a.length-1] = a[];
>>    b[a.length .. 100] = 5;
>> }
>> ```
>>
>> I consider this perfectly safe, but DScanner gives warnings 
>> for this, no matter if a check is present or not.
>> What's the recommended fix for this? I have no idea what else 
>> to use.
>
> Why not use foreach in your code? Is there a specific reason 
> for this? If I'm not mistaken, this is what you want to do:
>
> [...]
>
> SDB79

I dont know if that's the reason but slice assignments are likely 
faster, e.g for builtin types that certainly lead to generate a 
stdc `memove`s. (that must be indirectly done in DRT function 
`_d_array_slice_copy`).


More information about the Digitalmars-d-learn mailing list