On Wednesday, January 29, 2020 12:16:29 AM MST Ferhat Kurtulmuş via Digitalmars-d-learn wrote: > On Wednesday, 29 January 2020 at 06:53:15 UTC, Jonathan M Davis > > wrote: > > On Tuesday, January 28, 2020 10:17:03 PM MST Marcone via > > > > Digitalmars-d-learn wrote: > >> [...] > > > > Of course it is. string is immutable(char)[], and the > > characters are in UTF-8. immutable(wchar)[] would would be > > UTF-16. Even casting between those two types would result in > > nonsense, because UTF-8 and UTF-16 are different encodings. > > Casting between array or pointer types basically causes one > > type to be interpreted as the other. It doesn't convert the > > underlying data in any fashion. Also, strings aren't > > null-terminated in D, so having a pointer to a random string > > could result in a buffer overflow when you try to iterate > > through the string via pointer as is typical in C code. D code > > just uses the length property of the string. > > > > [...] > > + Just a reminder that string literals are null-terminated.
Yes, but unless you're using them directly, it doesn't really matter. Their null character is one past their end and thus is not actually part of the string itself as far as the type system is concerned. So, something as simple as str ~ "foo" would mean that you weren't dealing with a null-terminated string. You can do something like printf("answer: %d\n", 42); but if you mutate the string at all or create a new string from it, then you're not dealing with a string with a null-terminator one past its end anymore. Certainly, converting a string to wstring is not going to result in the wstring being null-terminated without a null terminator being explicitly appended to it. Ultimately, that null-terminator one past the end of string literals is pretty much just useful for being able to pass string literals directly to C functions without having to explicitly put a null terminator on their end. - Jonathan M Davis