www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - nogc formattedWrite

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Is it currently possible to somehow do  nogc formatted output to 
string?

I'm currently using my `pure  nogc nothrow` array-container 
`CopyableArray` as

 safe pure /*TODO nothrow  nogc*/ unittest
{
     import std.format : formattedWrite;
     const x = "42";
     alias A = CopyableArray!(char);
     A a;
     a.formattedWrite!("x : %s")(x);
     assert(a == "x : 42");
}

but I can't tag the unittest as `nothrow  nogc` because of the 
call to `formattedWrite`. Is this because `formattedWrite` 
internally uses the GC for buffer allocations or because it may 
throw?

It would be nice to be able to formatted output in -betterC...
Oct 07 2017
next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Saturday, 7 October 2017 at 18:14:00 UTC, Nordlöw wrote:
 It would be nice to be able to formatted output in -betterC...
Agreed. If you know the size of the buffer, you can use sformat, which might be nogc, but I don't know if it's compatible with betterC. Also, you might check out Ocean, which might have nogc formatting. https://github.com/sociomantic-tsunami/ocean
Oct 07 2017
parent Igor <stojkovic.igor gmail.com> writes:
On Saturday, 7 October 2017 at 18:27:36 UTC, jmh530 wrote:
 On Saturday, 7 October 2017 at 18:14:00 UTC, Nordlöw wrote:
 It would be nice to be able to formatted output in -betterC...
Agreed. If you know the size of the buffer, you can use sformat, which might be nogc, but I don't know if it's compatible with betterC. Also, you might check out Ocean, which might have nogc formatting. https://github.com/sociomantic-tsunami/ocean
As far as I know sformat is still not nogc because it may throw an exception if buffer is not large enough, and throwing exceptions requires allocation.
Oct 09 2017
prev sibling parent Ilya Yaroshenko <ilyayaroshenko gmail.com> writes:
On Saturday, 7 October 2017 at 18:14:00 UTC, Nordlöw wrote:
 Is it currently possible to somehow do  nogc formatted output 
 to string?

 I'm currently using my `pure  nogc nothrow` array-container 
 `CopyableArray` as

  safe pure /*TODO nothrow  nogc*/ unittest
 {
     import std.format : formattedWrite;
     const x = "42";
     alias A = CopyableArray!(char);
     A a;
     a.formattedWrite!("x : %s")(x);
     assert(a == "x : 42");
 }

 but I can't tag the unittest as `nothrow  nogc` because of the 
 call to `formattedWrite`. Is this because `formattedWrite` 
 internally uses the GC for buffer allocations or because it may 
 throw?

 It would be nice to be able to formatted output in -betterC...
Phobos code is not nogc (can be fixed), but it will not nothrow. PRs with nogc formatting functionality are welcome in Mir Algorithm. Cheers, Ilya
Oct 09 2017