アルゴリズム

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 テキスト分割

テキストの境界を見つけるアルゴリズム:書記素クラスター・単語・文境界。カーソル移動・テキスト選択・テキスト処理に不可欠です。

Unicode 双方向アルゴリズム (UBA)

文字の双方向カテゴリと明示的な方向オーバーライドを使って、混在方向テキスト(例:英語+アラビア語)の表示順序を決定するアルゴリズム。

Unicode 正規化

Unicodeテキストを標準的な正規形に変換するプロセス。4つの形式:NFC(合成)、NFD(分解)、NFKC(互換合成)、NFKD(互換分解)。

Unicode 照合アルゴリズム (UCA)

基本文字 → アクセント → 大小文字 → タイブレーカーの多段階比較でUnicode文字列を比較・ソートする標準アルゴリズム。ロケールのカスタマイズが可能です。