www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile time string manipulation

reply "js.mdnq" <js_adddot+mdng gmail.com> writes:
How do we manipulate strings at compile time when using static 
ifs?



static if (std.string.indexOf(S, ")") < -1)
    ...
else
    ...

This always returns false regardless if S contains a 7 or not.


from http://dpaste.dzfl.pl/64025e0a

mixin(StructNestType!("B", "b1"));

but I would like to modify so that I can do this

_Offset

as at the moment StructNestType2 doesn't handle struct's with 
more than 1 type parameter. I simply need to parse the string and 
determine if it contains type arguments.


e.g.,

struct B(T1,T2, int offset) { ... }
mixin(StructNestType!("B!(T1,T2)", "b1")); // (hide offset in 
string, not needed explicitly)


but I also need the code to work with

struct B(int offset) { ... }
mixin(StructNestType!("B", "b1"));

So I need to compare and replace the string to make it fit into 
StructNestType2. (as of right now I'm assuming only one argument, 
the ofs)

I thought CTFE would work with indexOf but I guess it won't?

Essentially I need to form B!(T1,T2, _my offset value_) and/or 
B!(_my offset value_) from the two cases above at compile time.
Dec 15 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
js.mdnq:

 static if (std.string.indexOf(S, ")") < -1)
    ...
 else
    ...

 This always returns false regardless if S contains a 7 or not.
If it doesn't find the substring it returns -1, so that's always false. So use: S.indexOf(")") < 0 Bye, bearophile
Dec 15 2012
parent "js.mdnq" <js_adddot+mdng gmail.com> writes:
On Saturday, 15 December 2012 at 12:33:13 UTC, bearophile wrote:
 js.mdnq:

 static if (std.string.indexOf(S, ")") < -1)
   ...
 else
   ...

 This always returns false regardless if S contains a 7 or not.
If it doesn't find the substring it returns -1, so that's always false. So use: S.indexOf(")") < 0 Bye, bearophile
OMG! duhie!!! I must be turning into a goofy goober!
Dec 15 2012