Explain grapheme clusters and Persian text normalization
Explain grapheme clusters and Persian text normalization
A grapheme cluster approximates a user-perceived character and can contain multiple code points. Byte length and rune count cannot reliably count graphemes. Unicode normalization (such as NFC) addresses canonical equivalence; application-specific Persian search normalization is a separate policy. Go string equality compares bytes and performs neither automatically.
package main
import ( "fmt" "unicode/utf8")
func main() { a, b := "é", "e\u0301" fmt.Println(a == b, utf8.RuneCountInString(a), utf8.RuneCountInString(b))}The two spellings are canonically equivalent but byte-distinct; both commonly appear as one perceived character. Full grapheme segmentation and Unicode NFC are not provided as general APIs by these standard-library packages. Use an appropriately versioned and tested Unicode library when required. This lesson implements neither algorithm.
Expected output:
false 1 2A narrow Persian search-key policy
package main
import ( "fmt" "strings")
func main() { key := strings.NewReplacer("\u064A", "\u06CC", "\u0643", "\u06A9") a, b := "\u0643\u064A", "\u06A9\u06CC" fmt.Println(a == b, key.Replace(a) == key.Replace(b)) fmt.Printf("%U\n", []rune(key.Replace("می\u200cروم")))}This explicit search policy folds Arabic Yeh (U+064A) to Persian Yeh (U+06CC) and Arabic Kaf (U+0643) to Persian Kaf (U+06A9). It is not NFC or a complete Persian normalizer, and can merge distinct original values. It preserves zero-width non-joiner U+200C (Persian half-space). Keep original display text; do not automatically remove diacritics or join controls or use these folded keys as unique security identifiers.
Expected output:
false true[U+0645 U+06CC U+200C U+0631 U+0648 U+0645]Try it locally
Apply only the two stated replacements to "كتاب" and check equality with "کتاب". Print whether applying the policy twice preserves the result.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Strings and Unicode 09: Explain grapheme clusters and Persian text normalization
Check the lesson objective before moving on.
Does rune count always equal grapheme count?
Is mapping Arabic Kaf to Persian Kaf equivalent to NFC?