算法

Grapheme Cluster Boundary

Rules (UAX#29) for determining where one user-perceived character ends and another begins. Critical for cursor movement, text selection, and correctly counting 'characters' in UI.

What is Grapheme Cluster Boundary (Grapheme Break)?

A grapheme cluster boundary is a position in a Unicode text string where one user-perceived character ends and the next begins. The rules that determine these positions are defined in Unicode Standard Annex #29 (UAX#29), titled Unicode Text Segmentation. The concept matters wherever software needs to move a text cursor, measure string length in visible characters, or break text at line wraps.

The term grapheme cluster distinguishes between code points (the atomic integer values Unicode assigns to each character) and the actual glyphs a user perceives. A single user-perceived character such as the letter é may be encoded as one code point (U+00E9, precomposed) or as two code points — e (U+0065) followed by the combining acute accent (U+0301). Both representations should be treated as a single grapheme cluster with a single boundary on each side.

Extended vs Legacy Grapheme Clusters

UAX#29 defines two types of grapheme clusters:

  • Legacy grapheme clusters — a simpler, older definition that always breaks before a spacing combining mark. This can produce incorrect results for some scripts.
  • Extended grapheme clusters — the modern, recommended definition. It handles Hangul syllable sequences, emoji modifier sequences, emoji ZWJ sequences, and regional indicator pairs correctly.

Almost all modern implementations use extended grapheme clusters. The distinction matters when you need to precisely count how many "characters" a user sees.

Boundary Rules by Script

Hangul syllables are a classic example where grapheme break rules are non-trivial. A Korean syllable like is composed of a leading jamo (ᄀ, U+1100), a vowel jamo (ᅡ, U+1161), and a trailing jamo (ᆨ, U+11A8). UAX#29 defines specific rules so that these three code points form a single grapheme cluster.

Emoji sequences add further complexity. The flag emoji for France 🇫🇷 is encoded as two Regional Indicator Symbol Letters — F (U+1F1EB) and R (U+1F1F7). UAX#29 keeps these paired as one cluster. Similarly, 👨‍👩‍👧 is a family ZWJ sequence of five code points joined by Zero Width Joiner (U+200D), forming one cluster.

Combining marks in scripts like Devanagari, Arabic, and Thai can stack multiple diacritics onto a base character. All combining marks that follow a base code point without an intervening boundary are included in the same grapheme cluster.

Cursor Movement Implications

When a user presses the arrow key in a text editor, the cursor should move by one grapheme cluster, not one code point. Moving by code points would leave the cursor stranded inside a multi-code-point grapheme, causing partial deletions and display artifacts. Platforms implement this via ICU's BreakIterator, Swift's String.Index, or Rust's unicode-segmentation crate.

# Python: count grapheme clusters (requires third-party library)
import grapheme
text = "\u0065\u0301"   # e + combining acute accent
len(text)               # 2 code points
grapheme.length(text)   # 1 grapheme cluster

Quick Facts

Property Value
Defining standard Unicode Standard Annex #29 (UAX#29)
Two cluster types Legacy grapheme clusters, Extended grapheme clusters
Recommended type Extended grapheme clusters
Complex scripts Hangul (jamo sequences), Indic (virama clusters)
Emoji cases Regional indicators, ZWJ sequences, modifier sequences
Key use cases Cursor movement, string length, line breaking
Implementation ICU BreakIterator, Intl.Segmenter (JS), unicode-segmentation (Rust)

相关术语

算法 中的更多内容

Case Folding

Mapping characters to a common case form for case-insensitive comparison. More comprehensive …

NFC (Canonical Composition)

规范化形式C:先分解再规范合成,生成最短形式,推荐用于数据存储和交换,是Web标准形式。

NFD (Canonical Decomposition)

规范化形式D:完全分解而不重新合成,macOS HFS+文件系统使用此形式。é(U+00E9)→ e + ◌́(U+0065 + U+0301)。

NFKC (Compatibility Composition)

规范化形式KC:兼容分解后再规范合成,合并视觉上相似的字符(fi→fi、²→2、Ⅳ→IV),用于标识符比较。

NFKD (Compatibility Decomposition)

规范化形式KD:兼容分解而不重新合成,是最激进的规范化方式,会丢失最多的格式信息。

String Comparison

Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …

Unicode 双向算法 (UBA)

利用字符双向类别和明确方向覆盖,确定混合方向文本(如英语+阿拉伯语)显示顺序的算法。

Unicode 换行算法

根据字符属性、CJK词边界和换行时机,确定文本可换至下一行位置的规则。

Unicode 排序算法 (UCA)

通过多级比较(基础字符→变音符号→大小写→决胜符)对Unicode字符串进行比较和排序的标准算法,支持区域设置自定义。

Unicode 文本分割

查找文本中各类边界的算法:字素簇、词和句子边界,对光标移动、文本选择和文本处理至关重要。