String Metaprogramming

Clayton via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 18 09:29:27 PDT 2015


On Saturday, 18 July 2015 at 16:01:25 UTC, Nicholas Wilson wrote:
> On Saturday, 18 July 2015 at 13:48:20 UTC, Clayton wrote:
>> [...]
>
>
>> [...]
>
> change function signature to
> int[char] function(string) or as the char type is the index 
> probably better of as
> int[256] function(string). also probably no need to take 
> pattern by ref as it is effectively struct{ size_t length; 
> char* ptr;}. also we aren't going to modify it.
>
> int[256] computeAtCompileTime(string pattern)
> {
>> [...]
> pattern.length is a size_t no need to change its type in 
> another variable.  you are unlikely to be dealing with string 
> longer than 2^32 (also signedness) but w/e
>         int[256] ret; // implicitly initialised to int.init 
> (i.e. 0)
>
>> [...]
> can just foreach over pattern
>         foreach(i, c; pattern)
>                 ret[c] = pattern.length - i -1;
>
>> [...]
> 	
>> [...]
>
> if you want this to be not callable at runtime then wrap the 
> main body (sans variable declaration) with
> if (__ctfe)
> {
>      ...
>
> }
Thanks  Nicholas , I have integrated some of your advice on the 
edited code i.e. foreach and ref in pattern . Hope I fully 
understood  what you meant. Am yet to look whether I still need 
to change the signature . I have heared there are two approaches 
to this, Where does one really draw the line between CTFE and 
Template metaprogramming?


More information about the Digitalmars-d-learn mailing list