dChar Error

matheus matheus at gmail.com
Fri Dec 30 21:54:38 UTC 2022


On Friday, 30 December 2022 at 15:28:05 UTC, Salih Dincer wrote:
> ... In this case, std.conv.to can be used for mutable dchars, 
> right? For example, is this solution the right approach?
>
> ```d
> auto toDchar(S)(inout S str) {
>   import std.conv : to;
>   return str.to!(dchar[]);
> }
>
> void main() {
>   auto str3 = "ÜÇ ON "d;
>   auto str4 = "BİR İKİ BEŞ "d.dup;
>   auto str5 = "DÖRT ALTI YEDİ ".toDchar;
>
>   //str5.fun(5);
> }
> ```

Unfortunately I can't say because I'm not a skilled D programmer, 
I use mostly as a C on steroids.

But yes I think it will generate a copy (mutable) based on this 
test:

void main(){
     import std.stdio;
     import std.conv;

     auto str1 = "BİR İKİ BEŞ ";
     auto str2 = str1;
     auto str3 = str2.to!(dchar[]);

     writeln(str1, ", ", str1.ptr);
     writeln(str2, ", ", str2.ptr);
     writeln(str3, ", ", str3.ptr);
     str3[0] = 'A';
     writeln(str3, ", ", str3.ptr);

}

It prints:

BİR İKİ BEŞ , 5641226D8200
BİR İKİ BEŞ , 5641226D8200
BİR İKİ BEŞ , 7FB466EAE000
AİR İKİ BEŞ , 7FB466EAE000

So for str2 = str1 it is just a case of passing the reference, 
and both are pointing to the same address, while in the case of: 
"str3 = str2.to!(dchar[]);", the address is different, and 
accepts changing its content (str3[0] = 'A').

In the docs: https://dlang.org/phobos/std_conv.html#to

     "String to string conversion works for any two string types 
having (char, wchar, dchar) character widths and any combination 
of qualifiers (mutable, const, or immutable)."

But I couldn't find if the target will be mutable, but I think it 
will be, unless explicitly otherwise with a cast I believe.

Anyway I would wait and see if someone more skilled could shed a 
light.

Matheus.


More information about the Digitalmars-d-learn mailing list