www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Fix iota for unsigned reverse counting

Issue 7982 [1] is caused by iota.length() [2] not working 
properly with unsigned values where the begin is greater than the 
end (the larger value is subtracted from the smaller value 
causing it to wrap around).

I asked Andrei in IRC if replacing:

     return unsigned((pastLast - current) / step);

with:

     return unsigned(((cast(Signed!Value)pastLast) - 
(cast(Signed!Value)current)) / step);

was a sane approach.  Although it does seem to fix the problem 
(though I haven't tested thoroughly) Andrei said it wasn't sane. 
I'm not sure what would be a better approach so I'm asking for 
ideas here.

(why does std.traits have unsigned() to take advantage of IFTI 
but no signed() equivalent?)

[1] http://d.puremagic.com/issues/show_bug.cgi?id=7982
[2] 
https://github.com/D-Programming-Language/phobos/blob/master/std/range.d#L4383

Regards,
Brad Anderson
Apr 26 2012