Grapheme cluster

A grapheme cluster is what a reader sees as a single character, however many Unicode code points it takes to write. A flag, an emoji with a skin tone, a family emoji and a letter with a separate accent mark are all one grapheme made of several code points. Unicode Standard Annex 29 defines where one cluster ends and the next begins.

Computers store text as code points, numbered entries in the Unicode table. Readers see characters. Most of the time one maps to one, which hides the cases where they do not.

Where one character is several

An accented letter can be stored as the letter followed by a combining accent. A flag is two regional indicator letters side by side. An emoji with a skin tone is the emoji followed by a modifier, and a family emoji is several people joined by invisible zero width joiners. Each of these displays as one character and is stored as two or more code points.

Unicode Standard Annex 29 sets out the rules that group code points into grapheme clusters, the units a reader would call characters.

Why it matters for text tools

Any operation that walks through text one code point at a time can split a cluster. Reversing a string that way separates an accent from its letter and turns a flag into a different flag. Cutting text at a fixed length can leave half an emoji at the end. Counting can report a number nobody can see.

Working in grapheme clusters avoids all three. Text is split into clusters first, then reversed, cut or counted, so each visible character stays intact.

Line endings too

A Windows line ending is two characters, carriage return then line feed, and UAX 29 treats the pair as one cluster. A careful reversal keeps them in that order, so the result still has proper line breaks.

Frequently asked questions

Why does reversing text break emoji?

Because a simple reversal flips code points, not graphemes. A flag is two regional letters; reversed, they spell a different flag or none. Reversing by grapheme cluster keeps each visible character whole.

Why do character counts disagree?

Programs count different things. Some count bytes, some count UTF-16 units, some count code points and some count graphemes. A single family emoji can be 1, 7 or 11 depending on which.

Do browsers know about graphemes?

Yes. Intl.Segmenter splits text into grapheme clusters following UAX 29, so a page can count or reverse what readers actually see.