digitalmars.D.learn - string from C function
- Andy Valencia (8/8) May 07 I feel dumb, because this can't be as elusive as it feels.
- Dejan Lekic (4/7) May 07 https://dlang.org/library/std/string/from_stringz.html
- Mike Parker (4/5) May 07 Given that `fromStringz` returns a slice of the original string,
- Andy Valencia (14/15) May 07 So about fromStringz...
- Mike Parker (9/22) May 07 `fromStringz` is giving you a slice of a `char*`, typed `char[]`.
- Andy Valencia (7/18) May 07 Thank you. I want to work in strings, so the first one's not an
- Python (3/10) May 08 Yes, it'safe to assume:
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
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
On Wednesday, 7 May 2025 at 22:28:32 UTC, Dejan Lekic wrote:https://dlang.org/library/std/string/from_stringz.htmlGiven 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
On Wednesday, 7 May 2025 at 22:28:32 UTC, Dejan Lekic wrote:https://dlang.org/library/std/string/from_stringz.htmlSo 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
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
On Thursday, 8 May 2025 at 00:53:20 UTC, Mike Parker 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. Andytst44.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`
May 07
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. AndyYes, it'safe to assume: https://github.com/dlang/phobos/blob/12bcacca2a590942e1b917daaa8732a509b2ef30/std/conv.d#L1087
May 08