Grapheme

A structure designed to effectively pack $(CHARACTERS) of a $(CLUSTER).

Grapheme has value semantics so 2 copies of a Grapheme always refer to distinct objects. In most actual scenarios a Grapheme fits on the stack and avoids memory allocation overhead for all but quite long clusters.

Constructors

this
this(C[] chars)
this(Input seq)

Ctor

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Postblit

A postblit is present on this object, but not explicitly documented in the source.

Members

Functions

opIndex
dchar opIndex(size_t index)

Gets a $(CODEPOINT) at the given index in this cluster.

opIndexAssign
void opIndexAssign(dchar ch, size_t index)

Writes a $(CODEPOINT) ch at given index in this cluster.

opOpAssign
ref opOpAssign(dchar ch)

Append $(CHARACTER) ch to this grapheme. Warning: Use of this facility may invalidate grapheme cluster, see also valid.

opOpAssign
ref opOpAssign(Input inp)

Append all $(CHARACTERS) from the input range inp to this Grapheme.

opSlice
SliceOverIndexed!Grapheme opSlice(size_t a, size_t b)
SliceOverIndexed!Grapheme opSlice()

Random-access range over Grapheme's $(CHARACTERS).

Properties

length
size_t length [@property getter]

Grapheme cluster length in $(CODEPOINTS).

valid
bool valid [@property getter]

True if this object contains valid extended grapheme cluster. Decoding primitives of this module always return a valid Grapheme.

Examples

1 import std.algorithm.comparison : equal;
2 import std.algorithm.iteration : filter;
3 import std.range : isRandomAccessRange;
4 
5 string bold = "ku\u0308hn";
6 
7 // note that decodeGrapheme takes parameter by ref
8 auto first = decodeGrapheme(bold);
9 
10 assert(first.length == 1);
11 assert(first[0] == 'k');
12 
13 // the next grapheme is 2 characters long
14 auto wideOne = decodeGrapheme(bold);
15 // slicing a grapheme yields a random-access range of dchar
16 assert(wideOne[].equal("u\u0308"));
17 assert(wideOne.length == 2);
18 static assert(isRandomAccessRange!(typeof(wideOne[])));
19 
20 // all of the usual range manipulation is possible
21 assert(wideOne[].filter!isMark().equal("\u0308"));
22 
23 auto g = Grapheme("A");
24 assert(g.valid);
25 g ~= '\u0301';
26 assert(g[].equal("A\u0301"));
27 assert(g.valid);
28 g ~= "B";
29 // not a valid grapheme cluster anymore
30 assert(!g.valid);
31 // still could be useful though
32 assert(g[].equal("A\u0301B"));

See Also

Meta

Suggestion Box / Bug Report