www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - string from C function

reply Andy Valencia <dont spam.me> writes:
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
May 07
parent reply Dejan Lekic <dejan.lekic gmail.com> writes:
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
May 07
next sibling parent Mike Parker <aldacron gmail.com> writes:
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)`.
May 07
prev sibling parent reply Andy Valencia <dont spam.me> writes:
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... ```d 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...
May 07
parent reply Mike Parker <aldacron gmail.com> writes:
On Wednesday, 7 May 2025 at 22:47:35 UTC, Andy Valencia wrote:

 So about fromStringz...

 ```d
 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`
May 07
parent reply Andy Valencia <dont spam.me> writes:
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
May 07
parent Python <python python.com> writes:
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
May 08