digitalmars.D.learn - Converting from C const(dchar*) to dstring
- Danyal Zia (12/12) Jun 24 2014 Hi, I like to print the strings from a C function that returns
- Steven Schveighoffer (6/15) Jun 24 2014 const(dchar *)x = ...;
- Danyal Zia (6/10) Jun 24 2014 const(dchar)* x = "Hello\0";
- Chris Cain (16/28) Jun 24 2014 You can do what he said, but you'll have to write your own strlen
- Danyal Zia (3/18) Jun 24 2014 It works indeed, thanks a lot! Any chance of making it into std
- Justin Whear (2/13) Jun 24 2014 A dchar* strlen implementation: http://dpaste.dzfl.pl/a4053387d8a4
- Chris Cain (4/6) Jun 24 2014 strlen is only defined for char, not dchar:
- Steven Schveighoffer (3/8) Jun 24 2014 Right, I forgot about that.
Hi, I like to print the strings from a C function that returns const(dchar*), but I can't make the conversion to dstring. I can convert vice versa by: dstring text = "Hello"; const(dchar)* str = toUTFz!(const(dchar)*)(text); // passing it to C function prints Hello However, I don't have the idea how can I go the other way. I tried several methods such as using to!dstring, toUTF32 etc which compiles successfully however printing them gives address of them instead of text. Thanks, Danyal Zia
Jun 24 2014
On Tue, 24 Jun 2014 13:37:41 -0400, Danyal Zia <catofdanyal yahoo.com> wrote:Hi, I like to print the strings from a C function that returns const(dchar*), but I can't make the conversion to dstring. I can convert vice versa by: dstring text = "Hello"; const(dchar)* str = toUTFz!(const(dchar)*)(text); // passing it to C function prints Hello However, I don't have the idea how can I go the other way. I tried several methods such as using to!dstring, toUTF32 etc which compiles successfully however printing them gives address of them instead of text.const(dchar *)x = ...; // assuming 0 terminated dstring text = x[0..x.strlen].idup; -Steve
Jun 24 2014
On Tuesday, 24 June 2014 at 17:59:41 UTC, Steven Schveighoffer wrote:const(dchar *)x = ...; // assuming 0 terminated dstring text = x[0..x.strlen].idup; -Steveconst(dchar)* x = "Hello\0"; dstring text = x[0..x.strlen].idup; writeln(text); Error: no property 'strlen' for type 'const(dchar)*'
Jun 24 2014
On Tuesday, 24 June 2014 at 18:17:07 UTC, Danyal Zia wrote:On Tuesday, 24 June 2014 at 17:59:41 UTC, Steven Schveighoffer wrote:You can do what he said, but you'll have to write your own strlen function: something like: size_t strlen(in dchar* s) pure system nothrow { size_t pos = 0; dchar term = '\0'; while(s[pos] != term) ++pos; return pos; } const(dchar)* ds = "hello\0"; dstring text = ds[0..strlen(ds)].idup; writeln(text); worksconst(dchar *)x = ...; // assuming 0 terminated dstring text = x[0..x.strlen].idup; -Steveconst(dchar)* x = "Hello\0"; dstring text = x[0..x.strlen].idup; writeln(text); Error: no property 'strlen' for type 'const(dchar)*'
Jun 24 2014
On Tuesday, 24 June 2014 at 18:34:31 UTC, Chris Cain wrote:You can do what he said, but you'll have to write your own strlen function: something like: size_t strlen(in dchar* s) pure system nothrow { size_t pos = 0; dchar term = '\0'; while(s[pos] != term) ++pos; return pos; } const(dchar)* ds = "hello\0"; dstring text = ds[0..strlen(ds)].idup; writeln(text); worksIt works indeed, thanks a lot! Any chance of making it into std phobos?
Jun 24 2014
On Tue, 24 Jun 2014 18:17:06 +0000, Danyal Zia wrote:On Tuesday, 24 June 2014 at 17:59:41 UTC, Steven Schveighoffer wrote:A dchar* strlen implementation: http://dpaste.dzfl.pl/a4053387d8a4const(dchar *)x = ...; // assuming 0 terminated dstring text = x[0..x.strlen].idup; -Steveconst(dchar)* x = "Hello\0"; dstring text = x[0..x.strlen].idup; writeln(text); Error: no property 'strlen' for type 'const(dchar)*'
Jun 24 2014
On Tuesday, 24 June 2014 at 17:59:41 UTC, Steven Schveighoffer wrote:// assuming 0 terminated dstring text = x[0..x.strlen].idup;strlen is only defined for char, not dchar: https://github.com/D-Programming-Language/druntime/blob/master/src/core/stdc/string.d#L44
Jun 24 2014
On Tue, 24 Jun 2014 14:28:58 -0400, Chris Cain <zshazz gmail.com> wrote:On Tuesday, 24 June 2014 at 17:59:41 UTC, Steven Schveighoffer wrote:Right, I forgot about that. -Steve// assuming 0 terminated dstring text = x[0..x.strlen].idup;strlen is only defined for char, not dchar: https://github.com/D-Programming-Language/druntime/blob/master/src/core/stdc/string.d#L44
Jun 24 2014