www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile time string parse

reply Paolo Invernizzi <arathorn fastwebnet.iit> writes:
Hi all,
Someone can point me to a way of doing something like this with DMD 2.026:

class A {
    invariant(int) rev = std.conv.to!(int)("$Rev: 182 $");
}

I've messed up with metastrings and std.conv, but I cannot find a working way.

Cheers, Paolo
Mar 17 2009
next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Tue, Mar 17, 2009 at 10:54 AM, Paolo Invernizzi
<arathorn fastwebnet.iit> wrote:
 Hi all,
 Someone can point me to a way of doing something like this with DMD 2.026=
:
 class A {
 =A0 =A0invariant(int) rev =3D std.conv.to!(int)("$Rev: 182 $");
 }

 I've messed up with metastrings and std.conv, but I cannot find a working=
way.
 Cheers, Paolo
int parseRevision(string s) { // I'm assuming it'll always be of the form "\$ Rev: (\d+) \$" return to!(int)(s["$ Rev: ".length .. $ - 2]); } void main() { immutable(int) x =3D parseRevision("$ Rev: 182 $"); writefln(x); }
Mar 17 2009
prev sibling next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Tue, Mar 17, 2009 at 11:32 AM, Jarrett Billingsley
<jarrett.billingsley gmail.com> wrote:

Sigh, I'll get it eventually.

template Atoi(string s)
{
	static if(s.length == 1)
		enum int Atoi = s[$ - 1] - '0';
	else
		enum int Atoi = 10 * Atoi!(s[0 .. $ - 1]) + (s[$ - 1] - '0');
}
Mar 17 2009
prev sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Tue, Mar 17, 2009 at 11:28 AM, Jarrett Billingsley
<jarrett.billingsley gmail.com> wrote:
 On Tue, Mar 17, 2009 at 10:54 AM, Paolo Invernizzi
 <arathorn fastwebnet.iit> wrote:
 Hi all,
 Someone can point me to a way of doing something like this with DMD 2.02=
6:
 class A {
 =A0 =A0invariant(int) rev =3D std.conv.to!(int)("$Rev: 182 $");
 }

 I've messed up with metastrings and std.conv, but I cannot find a workin=
g way.
 Cheers, Paolo
int parseRevision(string s) { =A0 =A0 =A0 =A0// I'm assuming it'll always be of the form "\$ Rev: (\d+)=
\$"
 =A0 =A0 =A0 =A0return to!(int)(s["$ Rev: ".length .. $ - 2]);
 }

 void main()
 {
 =A0 =A0 =A0 =A0immutable(int) x =3D parseRevision("$ Rev: 182 $");
 =A0 =A0 =A0 =A0writefln(x);
 }
Oh, oops, didn't notice it was in a class. template Atoi(string s) { static if(s.length =3D=3D 1) enum int Atoi =3D s[0] - '0'; else enum int Atoi =3D 10 * Atoi!(s[0 .. $ - 1]) + (s[0] - '0'); } template parseRevision(string s) { // I'm assuming it'll always be of the form "\$ Rev: (\d+) \$" enum parseRevision =3D Atoi!(s["$ Rev: ".length .. $ - 2]); } class A { immutable(int) x =3D parseRevision!("$ Rev: 182 $"); }
Mar 17 2009