Martin Tschierschke wrote:

i doubt so.
So my "solution" on how to get a short cut for using:

writeln(mixin(interp!"${name} you are app. ${age*365} days old"));
..

NEVER. EVER. USE. THE. FOLOWING. IN. YOUR. CODE.
NEVER!!!


import std.stdio;

mixin template usesexpand() {
        import std.format : format;
        string sexpand1(string s) () {
                char[] res;
                res.length = s.length+1024;
                auto rused = res.length;
                auto rpos = rused*0; // i really HAET `size_t`!
                void put(bool escape=true) (const(char)[] s...) {
                        foreach (char ch; s) {
                                static if (escape) {
                                        if (ch == '"' || ch == '\\') {
                                                if (rpos == rused) { rused += 
1024; res.length = rused; }
                                                res[rpos++] = '\\';
                                        }
                                }
                                if (rpos == rused) { rused += 1024; res.length 
= rused; }
                                res[rpos++] = ch;
                        }
                }

                char[] fmt;
                fmt.length = 256;
                auto fused = fmt.length;
                auto fpos = fused*0; // i really HAET `size_t`!
                void putfmt (const(char)[] s...) {
                        foreach (char ch; s) {
                                if (fpos == fused) { fused += 1024; fmt.length 
= fused; }
                                fmt[fpos++] = ch;
                        }
                }

                putfmt(`format("`);

                auto pos = rpos, epos = rpos;
                pos = 0;
                while (pos < s.length) {
                        epos = pos;
                        while (epos < s.length && s[epos] != '$') ++epos;
                        putfmt("%s");
                        put!false(`, "`);
                        if (epos+1 >= s.length || s[epos+1] != '{') {
                                put(s[pos..epos+1]);
                                put!false(`"`);
                                pos = epos+1;
                                continue;
                        }
                        put(s[pos..epos]);
                        put!false(`"`);
                        pos = epos+2;
                        while (epos < s.length && s[epos] != '}') ++epos;
                        if (epos > pos) {
                                putfmt("%s");
                                put(`, `);
                                put(s[pos..epos]);
                        }
                        pos = epos+1;
                }
                put(`)`);
                putfmt(`"`);
                return cast(string)fmt[0..fpos]~cast(string)res[0..rpos]; // it 
is safe to cast here
        }
        enum sexpandstr(string s) = sexpand1!s;
        enum sexpand(string s) = mixin(sexpand1!s);
        void echo(string s) () { writeln(mixin(sexpand1!s)); }
}

mixin usesexpand;
immutable n = 40;

enum nstr = sexpand!"n is ${n+2}";

void main () {
        mixin usesexpand;
        int a = 42/2;
        echo!"a is ${a*2}"; //writeln(mixin(sexpandstr!"a is ${a*2}"));
        writeln(nstr);
}

Reply via email to