What type does byGrapheme() return?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Dec 27 19:44:59 UTC 2019


On Fri, Dec 27, 2019 at 06:26:58PM +0100, Robert M. Münch via Digitalmars-d-learn wrote:
> I love these documentation lines in the D docs:
> 
> 	auto byGrapheme(Range)(Range range)
> 
> How should I know what auto is? Why not write the explicit type so
> that I know what to expect? When declaring a variable as class/struct
> member I can't use auto but need the explicit type...
> 
> I used typeof() but that doesn't help a lot:
> 
> gText = [Grapheme(53, 0, 0, 72057594037927936, [83, ...., 1)]Result!string
> 
> I want to iterate a string byGrapheme so that I can add, delete,
> change graphemes.
[...]

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.

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;


T

-- 
The irony is that Bill Gates claims to be making a stable operating system and Linus Torvalds claims to be trying to take over the world. -- Anonymous


More information about the Digitalmars-d-learn mailing list