What type does byGrapheme() return?

Robert M. Münch robert.muench at saphirion.com
Sun Dec 29 12:19:09 UTC 2019

On 2019-12-27 19:44:59 +0000, H. S. Teoh said:

> Since graphemes are variable-length in terms of code points, you can't
> exactly *edit* a range of graphemes -- you can't replace a 1-codepoint
> grapheme with a 6-codepoint grapheme, for example, since there's no
> space in the underlying string to store that.

Hi, my idea was that when I use a grapheme range, it will abstract away 
that graphemes consist of different sized code-points. And the docs at 
https://dlang.org/phobos/std_uni.html#byGrapheme show an example using 
this kind of range:

auto gText = text.byGrapheme;


But maybe I need to get a better understanding of the ranges stuff too...

> 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?

Robert M. Münch
smarter | better | faster

More information about the Digitalmars-d-learn mailing list