d - Execute compile time-compiled regex at compile time -
i compile error when try compile code:
import std.regex; enum truth = "baba".matchfirst(ctregex!`[ab]+$`) ? true : false; void main() {}
/usr/local/cellar/dmd/2.067.1/include/d2/std/regex/package.d(671): error: malloc cannot interpreted @ compile time, because has no available source code
how around this?
you can't, regex can compiled @ compile time, not run. you'll have write match other way, maybe combination of indexof or other simpler functions. (the reason isn't because regex complicated, because internally calls malloc
efficiency, not supported @ compile time since external c function.)
understanding ctregex
needs explanation phobos' regular expression engine. works in 2 steps: given regular expression, first compiles bytecode, match on string, runs code.
in ordinary regex
, both steps happen @ runtime. when construct regex object, compiles bytecode. then, when match on string, runs code.
with ctregex
, first part happens @ compile time, second part still happens @ runtime. compiles regex bytecode when d compiled... shoves bytecode through rest of d compiler optimized native code. that's benefit can offer. (btw difference doesn't matter, should benchmark input strings see 1 better.)
normally, ctfe (compile-time function evaluation) means runtime code can run @ compile time too, if source available. that's not case malloc
why error says that.
so answer depend on regex is. you'll want simplify , rewrite in terms of other string and/or std.algorithm functions.
...or rewrite std.regex source code rid of malloc calls. replace them new
, if(_ctfe)
start. i've tried doing before though, , unless code has been refactored since then, you'll hit problem: runtime performance, std.regex uses things unions, aren't supported in ctfe... you'd have rewrite bunch of , careful not compromise runtime performance in process.
probably easier handle specific case other functions.
Comments
Post a Comment