Just another example of missing string interpolation

Imperatorn johan_forsberg_86 at hotmail.com
Fri Oct 20 07:27:19 UTC 2023


On Friday, 20 October 2023 at 07:14:45 UTC, Walter Bright wrote:
> On 10/18/2023 7:47 PM, Adam D Ruppe wrote:
>> [...]
>
> Thanks for the example. I thought I'd show the equivalent with 
> DIP1027. It's a bit crude, and I didn't bother with the details 
> of html generation, but this shows the method and can be 
> compiled and run:
>
> ```
> import std.conv;
>
> string makeHtmlFromTemplate(Parts...)(const char[] fmt, Parts 
> parts)
> {
>     string html;
>     size_t i;
>
>     void advance()
>     {
>         while (i < fmt.length)
>         {
>             if (fmt[i] == '%' && i + 1 < fmt.length && fmt[i + 
> 1] == 's')
>             {
>                 i += 2;
>                 return;
>             }
>             html ~= fmt[i];
>             ++i;
>         }
>     }
>
>     advance();
>     foreach (part; parts)
>     {
>         html ~= to!string(part); // or htmlEntityEncode
>         advance();
>     }
>     return html;
> }
>
> void main()
> {
>     import std.stdio;
>     string abc = "def";
>     string html = makeHtmlFromTemplate("format %s %s sss", abc, 
> 73);
>          // tuple generated by DIP1027 
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>          // from i"format $abc $(73) sss"
>     writeln(html);
> }
> ```
> which prints:
>
> format def 73 sss
>
> This code can be adapted to do whatever output schema one 
> wants. Compare the tuple generated by DIP1027:
>
> ```
> tuple("format %s %s sss", abc, 73)
> ```
> and with YAIDIP:
> ```
> tuple(.object.imported!"core.interpolation".InterpolationHeader!("", "format ", "", abc, "", 73, "", " sss", ""), "", "format ", "", abc, "", 73, "", " sss", "")
> ```


Whatever happens, can we please either merge the new dip or your 
dip?

Please?

Whatever dip is the simplest and performs the best should win.

Just so we get somewhere. The community is begging you all to 
settle for something.

Maybe a "winner" could be chosen emperically instead?

Write some examples and run.

Measure the performance. Fastest that is also currect wins.

Easy 😍


More information about the Digitalmars-d mailing list