Array Slice Ranges
BCS
BCS at pathlink.com
Thu Nov 9 10:29:22 PST 2006
Nikita Kalaganov wrote:
> Hmm, as far as I understand, the non-inclusive slice avoids massive '+1'
> appearings in source.
>
> For example (slice five elements]:
>
> uint size = 5;
> data[head..head + size] //is better than data[head..head + size + 1] for inclusive
> slice
>
Wouldn't that be -1? But the point stands.
Also how do you get a 0 length slice with inclusive?
exclusive
data[0..0].length == 0;
inclusive
data[0..0].length == 1;
> But, there are better syntax available:
>
> [2..5] means 2,3,4,5 (inclusive slice)
> [2..:5] means 'take five elements, first is at index 2', to avoid '+1' problem
>
> So, example becomes:
>
> uint size = 5;
> data[head..:size] //good, isn't it ?
>
I'd go with [start->length] but thats just me.
More information about the Digitalmars-d
mailing list