View source code
Display the source code in std/uni.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Struct std.uni.Grapheme

A structure designed to effectively pack characters of a grapheme cluster.

struct Grapheme ;

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

NameDescription
this () Ctor

Properties

NameTypeDescription
length[get] ulongGrapheme cluster length in code points.
valid[get] boolTrue if this object contains valid extended grapheme cluster. Decoding primitives of this module always return a valid Grapheme.

Methods

NameDescription
opIndex (index) Gets a code point at the given index in this cluster.
opIndexAssign (ch, index) Writes a code point ch at given index in this cluster.
opOpAssign (ch) Append character ch to this grapheme.
opOpAssign (inp) Append all characters from the input range inp to this Grapheme.
opSlice (a, b) Random-access range over Grapheme's characters.

See Also

decodeGrapheme, graphemeStride

Example

import std.algorithm.comparison : equal;
import std.algorithm.iteration : filter;
import std.range : isRandomAccessRange;

string bold = "ku\u0308hn";

// note that decodeGrapheme takes parameter by ref
auto first = decodeGrapheme(bold);

writeln(first.length); // 1
writeln(first[0]); // 'k'

// the next grapheme is 2 characters long
auto wideOne = decodeGrapheme(bold);
// slicing a grapheme yields a random-access range of dchar
assert(wideOne[].equal("u\u0308"));
writeln(wideOne.length); // 2
static assert(isRandomAccessRange!(typeof(wideOne[])));

// all of the usual range manipulation is possible
assert(wideOne[].filter!isMark().equal("\u0308"));

auto g = Grapheme("A");
assert(g.valid);
g ~= '\u0301';
assert(g[].equal("A\u0301"));
assert(g.valid);
g ~= "B";
// not a valid grapheme cluster anymore
assert(!g.valid);
// still could be useful though
assert(g[].equal("A\u0301B"));

Authors

Dmitry Olshansky

License

Boost License 1.0.