print ubyte[] as (ascii) string

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Jan 7 20:08:00 UTC 2022


On Fri, Jan 07, 2022 at 07:54:28PM +0000, eugene via Digitalmars-d-learn wrote:
[...]
> * Does .until() make a copy of original string? And GC then will take
> care of it?

No, it's one of the lazy range functions that lazily evaluates the
string and does not allocate. 


> * So many ways to do simple C printf("%s", (char*)buf)... I am feeling
> like Buridan's ass

D strings are different from C strings.  Although D *can* handle C
strings, it should not be surprising there's a bit of friction.

The simplest way to handle a C string from D is just to use
.fromStringz:

	import std.string : fromStringz;
	char *buf = some_c_function();
	writeln(buf.fromStringz);

Note that fromStringz is @nogc, since it only takes a slice of the C
string and does not copy anything. So it should be good even for
GC-phobic code.


T

-- 
What do you get if you drop a piano down a mineshaft? A flat minor.


More information about the Digitalmars-d-learn mailing list