On Friday, 31 July 2020 at 17:57:58 UTC, H. S. Teoh wrote:

I choosed the following way regarding:
2) The regex is not initialized by ctRegex in order to avoid the
compile-time overhead; instead, it's initialized at program startup
  time.

version(DigitalMars){
   auto reg(alias var)(){
                return(regex(var));
                pragma(msg,"regex used");
   }
}

version(LDC){
// reg!() is an alias method, which can check which kind of parameter it got
  auto reg(alias var)(){
       static if (__traits(compiles, {enum ctfeFmt = var;}) ){
                // "Promotion" to compile time value
                enum ctfeReg =  var ;
                pragma(msg, "ctRegex used");
                return(ctRegex!ctfeReg);

       }else{
                return(regex(var));
                pragma(msg,"regex used");
                }
       }
}

So when compiling with DMD my reg!("....") expression is using the runtime version. When compiling with LDC (for release) I use the ctRegex version, if possible. The (alias var) combined with the check if the var is known at compile time:
__traits(compiles, {enum ctfeFmt = var;}

I have to admit that the idea was mine, but the crafting only with the help of forum members!

// Function to mark all ocurences of the word offshore within html bold.
string markoffshore(string to_mark){
return to_mark.replaceAll(reg!(r"([oO]ffshore)"),"<b>$1</b>");
}


Reply via email to