numericValue for (unicode) characters

Dmitry Olshansky dmitry.olsh at gmail.com
Wed Jan 2 12:49:30 PST 2013


1/3/2013 12:21 AM, Andrei Alexandrescu пишет:
> On 1/2/13 3:13 PM, Dmitry Olshansky wrote:
>> 1/2/2013 7:24 PM, bearophile пишет:
>>> monarch_dodra:
>>>
>>>> The rationale for this:
>>>> std.ascii: I think returning -1 as a magic number should help keep the
>>>> code faster and with less clutter than with exceptions.
>>>
>>> For the ASCII version I have two use cases:
>>> - Where I want to go fast&unsafe I just use "c - '0'".
>>> - When I want more safety I'd like to use something as to!(), that
>>> raises exceptions in case of errors.
>>>
>>> A function that works on ASCII and returns -1 doesn't give me much more
>>> than "c - '0'". So maybe exceptions are good in the ASCII case too.
>>>
>>
>> Then we can maybe just drop this function? What's wrong with
>> if(std.ascii.isNumeric(a))
>> a -= '0';
>> else
>> enforce(false);
>
> Unnecessary flow :o).
>
> enforce(std.ascii.isNumeric(a));
> a -= '0';

Yup, and it's 2 lines then. And if one really wants to chain it:
map(a => enforce(std.ascii.isNumeric(a)), a -= '0')(...);

Hardly makes it Phobos candidate then ;)


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list