Question about iteger literals
bearophile via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jun 22 03:29:19 PDT 2014
Uranuz:
> bool isDigit(char c) nothrow
This function is already in Phobos, it's std.ascii.isDigit.
> ushort hexValue(char c) nothrow
Better to use this signature (assuming you want a ushort result,
despite a ubyte suffices and a uint is faster):
ushort hexValue(in char c) pure nothrow @safe @nogc
> else
> return ushort.max;
Special error values are not very safe. Consider the usage of a
Nullable!(ubyte, ubyte.max) instead, if you can stand the little
abstraction penalty.
> why these expressions are casted to int?
Because in C/C++/D if you perform arithmetic operations among
types shorter than int you produce an int.
> I was thinking that result should be of char type.
In general it can't be a char.
> And it could be implicitly converted to ushort
> (because there is enough place to store result).
The precedent compiler version was wrong, and the bug has being
fixed by the great Kenji.
If you have an expression like:
char - 'a' + 10
It means:
[0, 255] - 87
That is [-87, 168] that can't fit in the [0, 65535] range.
Currently in D the range value analysis works only on the current
expression, so the information from the "if(c >= 'a' && c <=
'f')" condition is ignored here.
There are discussions and even code to fix this:
http://forum.dlang.org/thread/lnrc8l$1254$1@digitalmars.com
https://github.com/lionello/dmd/compare/if-else-range
https://github.com/D-Programming-Language/dmd/pull/3679
But both Walter and Andrei have so far ignored this significant D
improvement, so I don't know if and when it will be added.
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list