The OpenD Programming Language

getEnumIndexFromKey

template getEnumIndexFromKey(T, bool caseInsensitive = true, getKeysTemplate...)
@safe pure nothrow @nogc
bool
getEnumIndexFromKey
(
C
)
(
scope const(C)[] key
,
ref uint index
)
if (
is(C == char) ||
is(C == wchar)
||
is(C == dchar)
)
if (
is(T == enum) &&
getKeysTemplate.length <= 1
)

Members

Functions

getEnumIndexFromKey
bool getEnumIndexFromKey(const(C)[] key, uint index)

Parameters

T

enum type to introspect

key

some string that corresponds to some key name of the given enum

index

resulting enum index if this method returns true.

Return Value

boolean whether the key was found in the enum keys and if so, index is set.

Examples

enum Short
{
    hello,
    world
}

enum Long
{
    This,
    Is,
    An,
    Enum,
    With,
    Lots,
    Of,
    Very,
    Long,
    EntriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCode_tm
}

uint i;
assert(getEnumIndexFromKey!Short("hello", i));
assert(i == 0);
assert(getEnumIndexFromKey!Short("world", i));
assert(i == 1);
assert(!getEnumIndexFromKey!Short("foo", i));

assert(getEnumIndexFromKey!Short("HeLlO", i));
assert(i == 0);
assert(getEnumIndexFromKey!Short("WoRLd", i));
assert(i == 1);

assert(!getEnumIndexFromKey!(Short, false)("HeLlO", i));
assert(!getEnumIndexFromKey!(Short, false)("WoRLd", i));

assert(getEnumIndexFromKey!Long("Is", i));
assert(i == 1);
assert(getEnumIndexFromKey!Long("Long", i));
assert(i == 8);
assert(getEnumIndexFromKey!Long("EntriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCode_tm", i));
assert(i == 9);
assert(!getEnumIndexFromKey!Long("EntriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCodeatm", i));

assert(!getEnumIndexFromKey!(Long, false)("EntriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCode_tM", i));
assert(!getEnumIndexFromKey!(Long, false)("entriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCode_tm", i));

Meta