String.inScript
func String.inScript(c: string, script: string): boolString.inScript reports whether c has the Unicode Script property script, named in full as in Scripts.txt, such as "Latin" or "Common". It reads Script, not Script_Extensions. It panics unless c is one code point, or if script names no script.
Details
scriptis a full name from Unicode'sScripts.txt, such as"Latin","Han", or"Old_Italic". A short code such as"Latn", another case such as"latin", or an unknown name panics."Common"holds characters many scripts share: digits, punctuation, symbols, emoji."Inherited"holds shared marks, such as U+0301.- It reads Script, not Script_Extensions, so
ー, used by both Katakana and Hiragana, is in"Common". cmust be exactly one code point; anything else, including"", panics.
Basic examples
String.inScript("a", "Latin") // true
String.inScript("я", "Cyrillic") // true
String.inScript("я", "Latin") // falseCall it through . on a one-code-point string:
"中".inScript("Han") // trueScope
Letters of different writing systems:
"α".inScript("Greek") // true
"ا".inScript("Arabic") // true
"あ".inScript("Hiragana") // true
"ア".inScript("Katakana") // true
"Dž".inScript("Latin") // trueA script whose name has two words takes an underscore, as in Scripts.txt:
"𐌀".inScript("Old_Italic") // trueDigits, punctuation, white space, and emoji are in Common, whatever text they appear in:
"7".inScript("Common") // true
"1".inScript("Common") // true
"!".inScript("Common") // true
" ".inScript("Common") // true
"🐜".inScript("Common") // true
"7".inScript("Latin") // falseA combining mark that many scripts use, such as an accent, is in Inherited, not in the script of the letter it follows. A mark of one script is in that script:
"\u{301}".inScript("Inherited") // true
"\u{301}".inScript("Latin") // false
"\u{3099}".inScript("Inherited") // true
"\u{93F}".inScript("Devanagari") // trueAn unassigned code point and a private-use one are in Unknown:
"\u{378}".inScript("Unknown") // true
"\u{E000}".inScript("Unknown") // true
"\u{378}".inScript("Latin") // falsePass the arguments by name:
String.inScript(c: "b", script: "Latin") // trueTest each code point of a string, here in a pipe:
"Ab1" | String.chars() | Array.map({ x.inScript("Latin") }) // [true, true, false]Applications
Find a host name that mixes Latin and Cyrillic letters, a common trick for a look-alike domain:
func mixesScripts(host: string): bool {
var latin = false
var cyrillic = false
for c in host {
if c.inScript("Latin") {
latin = true
}
if c.inScript("Cyrillic") {
cyrillic = true
}
}
latin && cyrillic
}
println(mixesScripts("paypal.com"))
println(mixesScripts("pаypal.com"))false
trueName the scripts of a text, skipping the characters that every script shares:
let scripts = ["Latin", "Greek", "Cyrillic", "Han", "Hiragana", "Katakana"]
func scriptsOf(s: string): Array[string] {
let found: Array[string] = []
for c in s {
for name in scripts {
if c.inScript(name) && !found.contains(name) {
found.push(name)
}
}
}
found
}
println(scriptsOf("Tokyo 東京 とうきょう"))
println(scriptsOf("α + β = 3"))["Latin", "Han", "Hiragana"]
["Greek"]Keep only the Han characters of a mixed text:
String.concat("漢字 kanji かんじ".chars().filter({ x.inScript("Han") })) // "漢字"Properties and relations
Script has one value per code point, so each code point below is in exactly one of the listed scripts:
let names = ["Latin", "Cyrillic", "Han", "Common", "Inherited", "Unknown"]
for c in ["a", "я", "中", "7", "\u{301}", "\u{378}"] {
assert(names.filter({ c.inScript(x) }).length() == 1)
}An unassigned code point, with category "Cn", is in Unknown:
for c in ["\u{378}", String.fromCodePoint(0xFFFF) ?? ""] {
assert(c.category() == "Cn")
assert(c.inScript("Unknown"))
}Possible issues
The script name is exact. Another case or a four-letter code panics:
"a".inScript("latin") // panic: String.inScript needs the name of a script, got "latin"
"a".inScript("Latn") // panic: String.inScript needs the name of a script, got "Latn"A space in place of the underscore panics too:
"a".inScript("Old Italic") // panic: String.inScript needs the name of a script, got "Old Italic"c must be one code point. A word, the empty string, and a flag, which is two code points, panic:
"ab".inScript("Latin") // panic: String.inScript needs a string of one code point, got "ab"
"".inScript("Latin") // panic: String.inScript needs a string of one code point, got ""
"🇨🇭".inScript("Common") // panic: String.inScript needs a string of one code point, got "🇨🇭"A letter with an accent written as two code points, e and U+0301, looks like one character and panics too:
"e\u{301}".inScript("Latin") // panic: String.inScript needs a string of one code point, got "é"Compose it first with String.normalize(s, "NFC"), or test each code point with for c in s:
"e\u{301}".normalize("NFC").inScript("Latin") // trueA script that a later Unicode version adds panics until Ant's tables move to that version. String.unicodeVersion names the version they follow:
String.unicodeVersion // "17.0.0"Test a whole string with for c in s or String.chars, and decide what the characters in Common and Inherited count as:
"Привет!".chars().every({ x.inScript("Cyrillic") }) // false
"Привет!".chars().every({ x.inScript("Cyrillic") || x.inScript("Common") }) // trueThe test reads Script, not Script_Extensions. The Japanese long-vowel mark ー appears in Katakana and Hiragana words, yet its script is Common:
"ー".inScript("Katakana") // false
"ー".inScript("Common") // trueSee also
String.category · String.isLetter · String.chars · String.codePoint · String.normalize
