Just another example of missing string interpolation
bachmeier
no at spam.net
Thu Oct 12 16:31:39 UTC 2023
On Thursday, 12 October 2023 at 13:39:46 UTC, Andrea Fontana
wrote:
> Real life example. Render a string like this:
>
> ```
> \x1b[2K\r\x1b[1mProgress:\x1b[0m 50% \x1b[1m\tSpeed:\x1b[0m
> 15.5 KB/s
> ```
>
> now (error prone and difficult to debug, try to code it!):
>
>
> ```
> stderr.write(format("%s\r%sProgress:%s %5.1f%% %s\tSpeed:%s
> %6.1f %s%s", clear, white, clear, progress, white, clear,
> curSpeed, unit));
> ```
>
> (or with string concat, good luck!)
>
> vs:
>
> ```
> stderr.write("${clear}\r{$white}Progress:${clear}${progress}%
> \t${white}Speed:${clear} ${curSpeed} ${unit}");
> ```
>
> Andrea
I agree that string interpolation needs to be added to the
language (it's 2023) but for the benefit of an outsider reading
your message, there are better options than calling `format`. I
use a generalization of this:
```
string interp(string s, string[string] subs) {
foreach(k; subs.keys) {
s = s.replace("${" ~ k ~ "}", subs[k]);
}
return s;
}
```
For your example, I'd call this
```
std.file.write("foo.txt",
interp("${clear}2K\r${white}Progress:${clear}${progress}%
\t${white}Speed:${clear} ${curSpeed} ${unit}",
["clear": "\x1b[", "white": "\x1b[1m",
"progress": "0m 50", "curSpeed": "0m 15.5",
"unit": "KB/s"]));
```
Would be much better to have this in the language, so I wouldn't
have to specify all the substitutions, but a lot better than
using `format` IMO.
More information about the Digitalmars-d
mailing list