Need help with movement from C to D
safety0ff via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 5 02:04:27 PDT 2014
On Monday, 5 May 2014 at 03:57:54 UTC, Andrey wrote:
>
> A similar D code is, as far as I know,
>
> type.field.offsetof
>
> Is there an any way to make a corresponding D template?
What you've written is the specific syntax for offsetof in D.
If the intent is to create a template so that you can simply
find/replace offsetof(type,field) with
offsetoftemplate!(type,field) then I think it is easier to create
a sed script - better yet a D program - for replacing the C macro
with D code.
Example program:
import std.array;
import std.file;
import std.regex;
import std.string;
int main(string[] args)
{
if (args.length < 2)
return -1;
auto regex = ctRegex!(`offsetof\(([^,]+),([^)]+)\)`);
auto sink = appender!(char[])();
foreach (filename; args[1..$])
{
auto text = readText(filename);
sink.reserve(text.length);
replaceAllInto!(cap =>
cap[1].strip~"."~cap[2].strip~".offsetof")(sink, text, regex);
write(filename, sink.data);
sink.clear();
}
return 0;
}
More information about the Digitalmars-d-learn
mailing list