View source code
Display the source code in std/uni.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aBugzilla 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 usinglocal clone.

Function std.uni.popGrapheme

Reads one full grapheme cluster from an input range of dchar inp, but doesn't return it. Instead returns the number of code units read. This differs from number of code points read only if input is an autodecodable string.

size_t popGrapheme(Input)(
  ref Input inp
)
if (isInputRange!Input && is(immutable(ElementType!Input) == immutable(dchar)));

Note

This function modifies inp and thus inp must be an L-value.

Example

// Two Union Jacks of the Great Britain in each
string s = "\U0001F1EC\U0001F1E7\U0001F1EC\U0001F1E7";
wstring ws = "\U0001F1EC\U0001F1E7\U0001F1EC\U0001F1E7";
dstring ds = "\U0001F1EC\U0001F1E7\U0001F1EC\U0001F1E7";

// String pop length in code units, not points.
writeln(s.popGrapheme()); // 8
writeln(ws.popGrapheme()); // 4
writeln(ds.popGrapheme()); // 2

writeln(s); // "\U0001F1EC\U0001F1E7"
writeln(ws); // "\U0001F1EC\U0001F1E7"
writeln(ds); // "\U0001F1EC\U0001F1E7"

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

// Also works for non-random access ranges as long as the
// character type is 32-bit.
auto testPiece = "\r\nhello!"d.filter!(x => !x.isAlpha);
// Windows-style line ending is two code points in a single grapheme.
writeln(testPiece.popGrapheme()); // 2
assert(testPiece.equal("!"d));

Authors

Dmitry Olshansky

License

Boost License 1.0.