Comment #0 by bearophile_hugs — 2014-07-06T13:13:39Z
sformat uses the given buffer, so this should compile:
void main() @nogc {
import std.string: sformat;
char[50] arr;
sformat(arr, "%d", 100);
}
If the given buffer is not large enough, sformat throws an exception. I think throwing this exception should not require a GC memory allocation at run-time.
Comment #1 by bearophile_hugs — 2014-07-11T09:32:09Z
One of my intended usages for this function is to generate messages for exceptions in @nogc code. Chris Cain comments:
> It's just stack-allocated this way isn't possible since the
> exception can be thrown into a scope above your stack allocated
> chars.
Comment #2 by timothee.cour2 — 2016-01-10T08:08:29Z
Comment #3 by TeddyBear12311 — 2016-07-14T00:18:09Z
Same problem ;/
Comment #4 by lodovico — 2016-07-14T13:34:11Z
The current sformat signature is:
char[] sformat(Char, Args...)(char[] buf, in Char[] fmt, Args args)
{
// something that uses encode(...)
}
It should become:
char[] sformat(Char, Args...)(char[] buf, in Char[] fmt, Args args)
{
return sformat!(UseReplacementDchar.no)(buf, fmt, args);
}
char[] sformat(UseReplacementDchar urd, Char, Args...)(char[] buf, in Char[] fmt, Args args)
{
// something that uses encode!urd(...)
}
So that current behaviour does not change and one can opt-in for the @nogc version.
Comment #5 by lodovico — 2016-07-14T13:46:42Z
Also, the current use of enforce inside sformat (which a comment states shall be removed) should be converted to an assert or something else that does not throw.
Comment #6 by robert.schadek — 2024-12-01T16:21:46Z