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) |
คำศัพท์ที่เกี่ยวข้อง
เพิ่มเติมใน อัลกอริทึม
Mapping characters to a common case form for case-insensitive comparison. More comprehensive …
Normalization Form C: แยกส่วนแล้วรวมใหม่แบบ canonical ได้รูปแบบที่สั้นที่สุด แนะนำสำหรับการจัดเก็บและแลกเปลี่ยนข้อมูล เป็นรูปแบบมาตรฐานของเว็บ
Normalization Form D: แยกส่วนอย่างสมบูรณ์โดยไม่รวมใหม่ ใช้โดยระบบไฟล์ macOS HFS+ é (U+00E9) → e + …
Normalization Form KC: แยกส่วนแบบ compatibility แล้วรวมแบบ canonical รวมอักขระที่มีลักษณะคล้ายกัน (fi→fi, ²→2, Ⅳ→IV) ใช้สำหรับการเปรียบเทียบตัวระบุ
Normalization Form KD: แยกส่วนแบบ compatibility โดยไม่รวมใหม่ เป็นการ normalize ที่เข้มงวดที่สุด สูญเสียข้อมูลการจัดรูปแบบมากที่สุด
Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …
กระบวนการแปลงข้อความ Unicode เป็นรูปแบบ canonical มาตรฐาน มี 4 รูปแบบ: NFC (รวม), NFD (แยก), …
อักขระที่ถูกยกเว้นจากการรวมแบบ canonical (NFC) เพื่อป้องกันการแตกย่อยแบบ non-starter และรับประกันความเสถียรของอัลกอริทึม ระบุไว้ใน CompositionExclusions.txt
อัลกอริธึมสำหรับค้นหาขอบเขตในข้อความ: ขอบเขต grapheme cluster, คำ และประโยค มีความสำคัญสำหรับการเลื่อนเคอร์เซอร์ การเลือกข้อความ และการประมวลผลข้อความ
ตำแหน่งระหว่างคำตามกฎการแบ่งคำของ Unicode ไม่ใช่แค่การแบ่งตามช่องว่าง แต่รองรับ CJK (ไม่มีช่องว่าง) คำย่อ และตัวเลขอย่างถูกต้อง