String.normalize
func String.normalize(s: string, form: string): stringString.normalize returns s in the Unicode normalization form form, one of "NFC", "NFD", "NFKC", or "NFKD". It panics on any other form.
Details
formis"NFC","NFD","NFKC", or"NFKD", in upper case. Any other string panics."NFD"splits an accented letter into the letter and its combining marks;"NFC"joins them back where Unicode has one code point. TheKforms also replace compatibility characters, such asfiwithfi.==,String.foldCase, andString.equalFolddo not normalize. Normalize text that can arrive in either spelling before you compare it or use it as a key.
Basic examples
The letter é has two spellings: one code point, or an e followed by a combining acute accent. They look the same but are not ==. "NFC" brings the second to the first:
"\u{E9}" == "e\u{301}" // false
String.normalize("e\u{301}", "NFC") == "\u{E9}" // true"NFD" goes the other way and splits the accent from its letter:
String.normalize("\u{E9}", "NFD").toCodePoints() // [101, 769]Call it through . on the string:
"e\u{301}".normalize("NFC").length() // 1Scope
The empty string and plain ASCII come back unchanged in every form:
String.normalize("", "NFC") // ""
String.normalize("Ant 1.0", "NFKD") // "Ant 1.0"Each form, on a string with an accent and a ligature. The canonical forms keep the ligature fi; the compatibility forms replace it with fi:
let s = "fiancé"
s.normalize("NFC").length() // 5
s.normalize("NFD").length() // 6
s.normalize("NFKC").length() // 6
s.normalize("NFKD").length() // 7Compatibility characters include full-width letters and digits, superscripts, and Roman numerals:
String.normalize("Ant 123", "NFKC") // "Ant 123"
String.normalize("x²", "NFKC") // "x2"
String.normalize("Ⅻ", "NFKC") // "XII"Some characters have a canonical equivalent of their own, so even "NFC" replaces them. The Ångström sign U+212B becomes the letter Å, U+00C5:
String.normalize("\u{212B}", "NFC").codePoint() // 197"NFD" splits a Korean Hangul syllable into its three jamo, and "NFC" joins them again:
String.normalize("한", "NFD").length() // 3
"한".normalize("NFD").normalize("NFC") == "한" // true"NFC" puts several combining marks in a canonical order, so the same marks typed in a different order compare equal after normalizing:
let a = "q\u{307}\u{323}"
let b = "q\u{323}\u{307}"
a == b // false
a.normalize("NFC") == b.normalize("NFC") // trueIn a pipe, the left side is s:
"e\u{301}" | String.normalize("NFC") | String.length() // 1Name the arguments:
String.normalize(s: "e\u{301}", form: "NFC") == "\u{E9}" // trueApplications
Strip the accents from text: split them off with "NFD", then drop the code points that String.isMark accepts:
func stripAccents(s: string): string {
s
| String.normalize("NFD")
| String.chars()
| Array.filter({ !x.isMark() })
| String.join("")
}
stripAccents("Crème Brûlée") // "Creme Brulee"
stripAccents("Ångström") // "Angstrom"Key a map by normalized names, so a name typed with a combining accent finds the same entry:
let ages: Map[string, int] = Map{}
func key(name: string): string {
name.normalize("NFC")
}
ages[key("Zoë")] = 31
ages[key("Zoe\u{308}")] // 31Build a search key that ignores case, accents, and compatibility forms by combining "NFKD", String.isMark, and String.foldCase:
func searchKey(s: string): string {
let bare = s.normalize("NFKD").chars().filter({ !x.isMark() })
String.join(bare, "").foldCase()
}
searchKey("Café") == searchKey("CAFE") // true
searchKey("film") == searchKey("Film") // trueProperties and relations
Normalizing twice gives the same result as normalizing once, in every form:
let s = "fiancé Å 한"
for form in ["NFC", "NFD", "NFKC", "NFKD"] {
let once = s.normalize(form)
assert(once.normalize(form) == once)
}"NFC" of the "NFD" form is the "NFC" form, and the same holds for the compatibility forms:
let s = "fiancé Å 한"
assert(s.normalize("NFD").normalize("NFC") == s.normalize("NFC"))
assert(s.normalize("NFKD").normalize("NFKC") == s.normalize("NFKC"))Possible issues
The form is spelled in upper case. Any other spelling panics:
String.normalize("e\u{301}", "nfc") // panic: String.normalize needs the form NFC, NFD, NFKC or NFKD, got "nfc""NFC" does not always give one code point per character. Where Unicode has no precomposed character, the marks stay separate; count with String.graphemes instead:
let s = String.normalize("q\u{307}\u{323}", "NFC")
s.length() // 3
s.graphemes().length() // 1The compatibility forms make different strings equal: x² and x2 have the same "NFKC" form:
String.normalize("x²", "NFKC") == String.normalize("x2", "NFKC") // trueNormalization does not fold case. Combine it with String.foldCase to ignore both:
String.normalize("É", "NFC") == String.normalize("é", "NFC") // false
String.normalize("É", "NFC").foldCase() == String.normalize("e\u{301}", "NFC").foldCase() // trueString.equalFold does not normalize either; normalize both sides first:
String.equalFold("\u{C9}", "e\u{301}") // false
String.equalFold("\u{C9}".normalize("NFC"), "e\u{301}".normalize("NFC")) // trueSee also
String.foldCase · String.equalFold · String.isMark · String.graphemes · String.chars · String.toCodePoints · String.length
