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.
  • dChar Error Salih Dincer via Digitalmars-d-learn
    • Re: dChar E... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
      • Re: dCh... Salih Dincer via Digitalmars-d-learn
        • Re:... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
    • Re: dChar E... novice2 via Digitalmars-d-learn
      • Re: dCh... Salih Dincer via Digitalmars-d-learn
        • Re:... matheus via Digitalmars-d-learn
          • ... Salih Dincer via Digitalmars-d-learn
            • ... matheus via Digitalmars-d-learn
              • ... Ali Çehreli via Digitalmars-d-learn
                • ... matheus via Digitalmars-d-learn
                • ... Salih Dincer via Digitalmars-d-learn
                • ... Salih Dincer via Digitalmars-d-learn
                • ... Ali Çehreli via Digitalmars-d-learn
                • ... Salih Dincer via Digitalmars-d-learn

Reply via email to