[Issue 5543] to!int to see a char as a single-char string

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Dec 21 07:34:15 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=5543



--- Comment #9 from monarchdodra at gmail.com 2012-12-21 07:34:14 PST ---
> Ok I think there are two enhancements here, one for the simple ascii int->char,
> char->int, and the other more complicated Unicode implementation which
> monarch/dmitry know more about.
> 
> I think we should split up the Unicode enhancement into a new bugzilla entry
> since the ASCII one can be implemented right now so this issue can be closed
> soon.

I'm a bit too busy to do the actual pull, but I wrote code, doc and test for
this already.

//----
/++
    If $(D c) is an ASCII digit, returns the
    corresponding numeric value. Returns -1 otherwise.
  +/
int numericValue(dchar c) @safe pure nothrow
{
    return ('0' <= c && c <= '9') ? (c - '0') : -1;
}
unittest
{
    int counter = 0;
    foreach (char c; 0 .. 80)
    {
        if (isDigit(c))
            assert(numericValue(c) == counter++);
        else
            assert(numericValue(c) == -1);
    }
}
//----

Not much, but there is never any reason to do the same work twice...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list