Rune strings. Like in Go.

jfondren julian.fondren at gmail.com
Thu Sep 30 23:16:09 UTC 2021


On Thursday, 30 September 2021 at 22:47:25 UTC, Alexey wrote:
> Go's rune string, being converted to byte array like so 
> `[]byte(string_var)` and saved to file - results in UTF-8, 
> while dchar is UTF-32.

You're explicitly asking for a byte cast and instead of a byte 
cast you get a reencoding? You might prefer that because it's 
familiar, but that's a really confusing thing to do. 
`std.string.representation` meanwhile turns a `dchar[]` into an 
`uint[]`.

And to get UTF-8 in a file, just write the string:

```d
import std;

void write() {
     string noel = "no\u0308el";
     dstring dstr = noel.toUTF32;

     File("a", "w").writeln(noel);
     File("b", "w").writeln(dstr);
}

void main() {
     write;
     ubyte[] fromA = cast(ubyte[]) read("a");
     ubyte[] fromB = cast(ubyte[]) read("b");

     assert(fromA == fromB);
     writeln(fromA, "\n", fromB);
}
```

output:

```
[110, 111, 204, 136, 101, 108, 10]
[110, 111, 204, 136, 101, 108, 10]
```


More information about the Digitalmars-d mailing list