Defining a static array with values in a range

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 22 02:21:25 PST 2015


On Thursday, January 22, 2015 05:56:39 tcak via Digitalmars-d-learn wrote:
> I want to define alphanumeric characters in an easy way.
> Something like that:
>
> char[] arr = ['a'..'z', 'A'..'Z', '0'..'9'];
>
> Though above example doesn't work. Is there any easy way to do
> this?
>
> I am trying to do something like EBNF definitions. So, I do not
> want to use loops, or define every single thing one by one by
> hand.

std.range.iota is what's used to creat ranges of values, and std.array.array
can be used to convert a range to an array. So, the functionality si in the
standard library, even if it's not quite as clean as you might like it to
be. e.g.

import std.range;
alias uiota = iota!(ubyte, ubyte);
auto r = chain(uiota('a', 'z'), uiota('A', 'Z'), uiota('0', '9'));
auto arr = cast(char[])array(r);

And actually, it's worse with chars than it would be with integers, because
for some reason, iota doesn't seem to want to operate directly on char, but
presumably that could be fixed, which would help clean up the code. But
regardless, the functionality is there without needing to add it to the
language, much as it might be nice to have it in the language where it would
look a bit cleaner.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list