Thread overview
string from C function
1 day ago
Andy Valencia
1 day ago
Dejan Lekic
1 day ago
Mike Parker
1 day ago
Andy Valencia
1 day ago
Mike Parker
1 day ago
Andy Valencia
1 day ago
Python
1 day ago

I feel dumb, because this can't be as elusive as it feels.

I'm calling a C library function, and getting a char * (null terminated).

Now I want it to be a D string. What's the right way to do this? Performance and memory use are not important; I just want a simple and idiomatic way to get from point A to point B.

TIA,
Andy

1 day ago

On Wednesday, 7 May 2025 at 22:23:36 UTC, Andy Valencia wrote:

>

Now I want it to be a D string. What's the right way to do this? Performance and memory use are not important; I just want a simple and idiomatic way to get from point A to point B.

https://dlang.org/library/std/string/from_stringz.html

for the opposite see https://dlang.org/library/std/string/to_stringz.html

1 day ago

On Wednesday, 7 May 2025 at 22:28:32 UTC, Dejan Lekic wrote:

>

https://dlang.org/library/std/string/from_stringz.html

Given that fromStringz returns a slice of the original string, you can use std.conv.to to do it with an allocation when needed: to!string(str).

1 day ago

On Wednesday, 7 May 2025 at 22:28:32 UTC, Dejan Lekic wrote:

>

https://dlang.org/library/std/string/from_stringz.html

So about fromStringz...

import std.string : fromStringz;
import core.stdc.time : ctime;

void
main() {
    string s = fromStringz(ctime(null));
}

Like that?

tst44.d(6): Error: cannot implicitly convert expression fromStringz(ctime(null)) of type char[] to string

Still seeking...

1 day ago

On Wednesday, 7 May 2025 at 22:47:35 UTC, Andy Valencia wrote:

>

So about fromStringz...

import std.string : fromStringz;
import core.stdc.time : ctime;

void
main() {
    string s = fromStringz(ctime(null));
}

Like that?

tst44.d(6): Error: cannot implicitly convert expression fromStringz(ctime(null)) of type char[] to string

Still seeking...

fromStringz is giving you a slice of a char*, typed char[].

string is immutable(char)[], so you can't assign char[] to it.

You could:

  • change the type of s to char[]
  • call .idup on the array returned from fromStringz to allocate a string
  • use std.conv.to
1 day ago

On Thursday, 8 May 2025 at 00:53:20 UTC, Mike Parker wrote:

> >

tst44.d(6): Error: cannot implicitly convert expression fromStringz(ctime(null)) of type char[] to string
fromStringz is giving you a slice of a char*, typed char[].

string is immutable(char)[], so you can't assign char[] to it.

You could:

  • change the type of s to char[]
  • call .idup on the array returned from fromStringz to allocate a string
  • use std.conv.to

Thank you. I want to work in strings, so the first one's not an option. But both the second and third do the trick. Would you say the to!string would be the most idiomatic? It worked as "to!string(ctime(&t))", but is it safe to assume it's reliably dealing with null-terminated strings? It's the pithiest if so.

Andy

1 day ago

On Thursday, 8 May 2025 at 04:51:27 UTC, Andy Valencia wrote:

>

Thank you. I want to work in strings, so the first one's not an option. But both the second and third do the trick. Would you say the to!string would be the most idiomatic? It worked as "to!string(ctime(&t))", but is it safe to assume it's reliably dealing with null-terminated strings? It's the pithiest if so.

Andy

Yes, it'safe to assume:

https://github.com/dlang/phobos/blob/12bcacca2a590942e1b917daaa8732a509b2ef30/std/conv.d#L1087