dmd foreach loops throw exceptions on invalid UTF sequences, use replacementDchar instead

jfondren julian.fondren at gmail.com
Sat Nov 6 13:07:53 UTC 2021


On Saturday, 6 November 2021 at 06:17:55 UTC, Alexey wrote:
> ```D
> struct graphstring
> {
>     grapheme[] grapheme_elements;
> }
>
> struct grapheme
> {
>     dchar[] codepoints;
> }
>
> ```

std.uni.Grapheme is more complex than a dchar[] (it tries to 
avoid allocating and it owns the dchars) but it has .length and 
opIndex that work like dchar[] (but read the warning on opSlice)

A Grapheme[] you can get with just `s1.byGrapheme.array`.

Round-trip example from std.uni:

```d
@safe unittest {
     import std.array : array;
     import std.conv : text;
     import std.range : retro;
     import std.uni : byGrapheme, byCodePoint;

     string s = "noe\u0308l"; // noël

     // reverse it and convert the result to a string
     string reverse = s.byGrapheme
         .array
         .retro
         .byCodePoint
         .text;

     assert(reverse == "le\u0308on"); // lëon
}
```


More information about the Digitalmars-d mailing list