What type does byGrapheme() return?
H. S. Teoh
hsteoh at quickfur.ath.cx
Mon Dec 30 23:09:58 UTC 2019
On Sun, Dec 29, 2019 at 01:19:09PM +0100, Robert M. Münch via Digitalmars-d-learn wrote:
> On 2019-12-27 19:44:59 +0000, H. S. Teoh said:
[...]
> > If you want to add/delete/change graphemes, what you *really* want
> > is to use an array of Graphemes:
> >
> > Grapheme[] editableGraphs;
> >
> > You can then splice it, insert stuff, delete stuff, whatever.
> >
> > When you're done with it, convert it back to string with something
> > like this:
> >
> > string result = editableGraphs.map!(g => g[]).joiner.to!string;
>
> I played around with this approach...
>
> string r1 = "Robert M. Münch";
> // Code-Units = 16
> // Code-Points = 15
> // Graphemes = 15
>
> Grapheme[] gr1 = r1.byGrapheme.array;
> writeln(" Text = ", gr1.map!(g => g[]).joiner.to!string);
> // Text = obert M. Münch
> writeln("wText = ", gr1.map!(g => g[]).joiner.to!wstring);
> // wText = obert M. Münch
> writeln("dText = ", gr1.map!(g => g[]).joiner.to!dstring);
> // dText = obert M. Münch
>
> Why is the first letter missing? Is this a bug?
[...]
I suspect there's a scope-related bug/issue somewhere here. I did some
experiments and discovered that using foreach to iterate over a
Grapheme[] is OK, but somehow when using Grapheme[] with .map to slice
over each one, I get random UTF-8 encoding errors and missing
characters.
I suspect the cause is that whatever Grapheme.opSlice returns is going
out-of-scope when used with .map, that's why it's malfunctioning. The
last time I looked at the Grapheme code, there's a bunch of
memory-related stuff involving dtors that's *probably* the cause of this
problem.
Please file a bug for this.
T
--
Life is complex. It consists of real and imaginary parts. -- YHL
More information about the Digitalmars-d-learn
mailing list