Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/15/21 2:10 AM, rempas wrote: So when I'm doing something like the following: `string name = "John";` Then what's the actual type of the literal `"John"`? In the chapter [Calling C functions](https://dlang.org/spec/interfaceToC.html#calling_c_functions) in the "Interfacing with C" page,

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread Ali Çehreli via Digitalmars-d-learn
Lot's of great information and pointers already. I will try from another angle. :) On 8/14/21 11:10 PM, rempas wrote: > So when I'm doing something like the following: `string name = "John";` > Then what's the actual type of the literal `"John"`? As you say and as the code shows, there are

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 09:06:14 UTC, Mike Parker wrote: The D `string` is an alias for `immutable(char)[]`, immutable contents of a mutable array reference (`immutable(char[])` would mean the array reference is also immutable). You don't want to assign that to a `char*`, because then

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:11:39 UTC, rempas wrote: I mean that in C, we can assign a string literal into a `char*` and also a `const char*` type without getting a compilation error while in D, we can only assign it to a `const char*` type. I suppose that's because of C doing explicit

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 09:01:17 UTC, jfondren wrote: They don't do the same thing. toStringz always copies, always GC-allocates, and always NUL-terminates. `cast(char*)` only does what you want in the case that you're applying it a string literal. But in that case you shouldn't cast, you

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:56:07 UTC, rempas wrote: On Sunday, 15 August 2021 at 08:53:50 UTC, Tejas wrote: External C libraries expect strings to be null terminated, so if you do use `.dup`, use `.toStringz` as well. Yeah, yeah I got that. My question is, if I should avoid

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:53:50 UTC, Tejas wrote: External C libraries expect strings to be null terminated, so if you do use `.dup`, use `.toStringz` as well. Yeah, yeah I got that. My question is, if I should avoid `cast(char*)` and use `.toStringz` while both do the exact same

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread Tejas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:51:19 UTC, rempas wrote: On Sunday, 15 August 2021 at 08:47:39 UTC, jfondren wrote: dup() isn't aware of the NUL since that's outside the slice of the string. It only copies the chars in "John". You can use toStringz to ensure NUL termination:

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:47:39 UTC, jfondren wrote: dup() isn't aware of the NUL since that's outside the slice of the string. It only copies the chars in "John". You can use toStringz to ensure NUL termination: https://dlang.org/phobos/std_string.html#.toStringz Is there something

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:11:39 UTC, rempas wrote: On Sunday, 15 August 2021 at 07:43:59 UTC, jfondren wrote: ```d unittest { char* s = "John".dup.ptr; s[0] = 'X'; // no segfaults assert(s[0..4] == "Xohn"); // ok } ``` Well, that one didn't worked out really well for me.

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:47:27 UTC, jfondren wrote: On Sunday, 15 August 2021 at 07:43:59 UTC, jfondren wrote: On Sunday, 15 August 2021 at 06:10:53 UTC, rempas wrote: ```d unittest { char* s = "John".dup.ptr; s[0] = 'X'; // no segfaults assert(s[0..4] == "Xohn"); // ok } ```

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:17:47 UTC, rikki cattermole wrote: pragma is a set of commands to the compiler that may be compiler specific. In the case of the msg command, it tells the compiler to output a message to stdout during compilation. Thanks man!

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rikki cattermole via Digitalmars-d-learn
On 15/08/2021 8:11 PM, rempas wrote: Still don't know what "pragma" does but thank you. pragma is a set of commands to the compiler that may be compiler specific. In the case of the msg command, it tells the compiler to output a message to stdout during compilation.

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:43:59 UTC, jfondren wrote: ```d unittest { pragma(msg, typeof("John")); // string pragma(msg, is(typeof("John") == immutable(char)[])); // true } ``` Still don't know what "pragma" does but thank you. ```d void zerort(string s) {

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:43:59 UTC, jfondren wrote: On Sunday, 15 August 2021 at 06:10:53 UTC, rempas wrote: ```d unittest { char* s = "John".dup.ptr; s[0] = 'X'; // no segfaults assert(s[0..4] == "Xohn"); // ok } ``` So am I going to have an extra runtime cost having to

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 06:10:53 UTC, rempas wrote: So when I'm doing something like the following: `string name = "John";` Then what's the actual type of the literal `"John"`? ```d unittest { pragma(msg, typeof("John")); // string pragma(msg, is(typeof("John") ==