MAX_CHAR

Ben Hanson Ben.Hanson at tfbplc.co.uk
Sat Jun 19 08:15:08 PDT 2010


Hi BCS,

== Quote from BCS (none at anon.com)'s article
> Hello Ben,
> > Hi there,
> >
> > Can anyone tell me how to get the maximum char value from a string
> > type? I have a template struct which can take string or wstring
and
> > need to determine the character type from the string type.
> >
> If you are looking for the X sutch that the following is true (as
long as
> fn returns a valid string)
> someStringType foo = fn();
> assert(foo[rand()%$] <= X);  // check a random position.
> Bits out of the following should work:
> template Max(T)  {
>   static if(is(T U : U[]))
>     const string Max = U.stringof ~ ":" ~ (cast(int)
(U.max)).stringof;
>   else static assert(false, "oops");
> }
> import std.stdio;
> void main()
> {
>    writef("%s\n", Max!(string));
>    writef("%s\n", Max!(wchar[]));
>    writef("%s\n", Max!(dchar[]));
> }
> Output:
>   char:cast(int)'\xff'
>   wchar:cast(int)'\U0000ffff'
>   dchar:cast(int)'\U0010ffff'
> > Thanks,
> >
> > Ben
> >

Yep, that's it!

Thanks a lot

Ben


More information about the Digitalmars-d mailing list