digitalmars.D.bugs - std.string.atoi() - int or long?
- Deewiant (27/27) Sep 18 2005 std.string.atoi() claims to return a long. However, it's just a wrapper,
std.string.atoi() claims to return a long. However, it's just a wrapper, consisting of no more than: long atoi(char[] s) { return std.c.stdlib.atoi(toStringz(s)); } Which is basically fine, but std.c.stdlib.atoi() returns an int. Thus, atoi() anything greater than int.max or lesser than int.min and it wraps around. The only thing wrong with that is that to match the function signature, it should be long.max and long.min. Some example code, which fails: import std.string; void main() { assert (atoi( "12345678900") == 12345678900); assert (atoi("-12345678900") == -12345678900); } So, either change the function to return an int (and correct the docs at http://www.digitalmars.com/d/phobos/std_string.html to match) or make the function work throughout [long.min, long.max]. I've put such a function up at http://per.us.to/~deewiant/extra/atoi.d - like it says in the comment, it's basically just a non-error-checking version of std.conv.toLong(). But that's what I expected std.string.atoi() to be in the first place, after reading the docs... I suggest that atoi() is simply changed to return an int, as that's what its name implies - "alpha to integer". Plus, it wouldn't impact any existing code at all, unless something relies on the bug, which is doubtful. But an atol() would be handy nevertheless, and I've just supplied a ready-made one... ;)
Sep 18 2005