Automated D code editing?

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Oct 12 12:43:07 PDT 2012


On 10/12/12, Lubos Pintes <lubos.pintes at gmail.com> wrote:
> Hi,
> I am still playing with DGUI library. Besides other things, I would like
> to convert enum names

You mean at compile-time? Try this: http://dpaste.dzfl.pl/06b95c3f

Copied below:

import std.conv;
import std.string;
import std.traits;

private @property string toCamelCase(E)()
    if (is(E == enum))
{
    string result;
    result ~= "enum CamelCase {\n";
    foreach (Member; EnumMembers!E)
    {
        foreach (word; to!string(Member).split("_"))
        {
            result ~= word.capitalize;
        }
        result ~= " = " ~ Member.stringof ~ ",\n";
    }
    result ~= "}";
    return result;
}

template CamelCase(E)
    if (is(E == enum))
{
    mixin(toCamelCase!E);
}

enum SOME_ENUM
{
    VAL_FIRST = -4,
    VAL_SECOND
}

alias CamelCase!SOME_ENUM SomeEnum;

void main()
{
    SomeEnum en1 = SomeEnum.ValFirst;
    SOME_ENUM en2 = SOME_ENUM.VAL_FIRST;
    assert(en1 == en2);
}


More information about the Digitalmars-d-learn mailing list