Write UTF-8 bytes directly to stack buffer

Chris Piker chris at hoopjump.com
Thu Mar 10 17:39:34 UTC 2022


Hi D

There are quite a few string, array and range functions in phobos 
so I'm getting confused as to the right way to encode string data 
as UTF-8 directly into a stack buffer while keeping track of the 
write point.

I have some output packets I'm building up in a tight loop.  For 
speed I'm using the a priori knowledge that output packets will 
never be larger then 64K.   So what's the best way to do this:

```d
ubyte[65536] buf;
ubyte[] usable_buf = buf;

   // part of some tight loop, how to create function writef_utf8 ?
   foreach(input_thing; things){
      usable_buf.writef_utf8!"format str"(input_thing.fieldA, 
input_thing.fieldB);
   }

size_t used = buf.length - usable_buf.length;
stdout.write(buf[0.. used]);

```


More information about the Digitalmars-d-learn mailing list