Write UTF-8 bytes directly to stack buffer

Chris Piker chris at hoopjump.com
Sun Mar 13 07:55:01 UTC 2022


On Thursday, 10 March 2022 at 17:59:33 UTC, H. S. Teoh wrote:
> Probably what you're looking for is std.format.formattedWrite. 
> For example:
>
> ```d
> import std;
> void main() {
> 	ubyte[65536] buf;
> 	char[] usable_buf = cast(char[]) buf[];
> 	usable_buf.formattedWrite!"Blah %d blah %s"(123, "Это UTF-8 
> строка.");
> 	auto used = buf.length - usable_buf.length;
> 	writefln("%(%02X %)", buf[0 .. used]);
> }
> ```

Hey thanks!  That does work with recent versions of dmd+phobos, 
but doesn't work in gdc-10.  For some reason it produces this 
error:

```d
error: static assert  "Cannot put a const(char)[] into a char[]."
```

Is there a work around involving `.representation` as alluded to 
in this 
[thread](https://forum.dlang.org/post/zmehmpithifbgfuefchv@forum.dlang.org) ?

To get around the issue I built gdc-11.2 from source code at the 
GNU site but the old version of phobos is still included, so no 
dice.





More information about the Digitalmars-d-learn mailing list