String.isGraphic
func String.isGraphic(s: string): boolString.isGraphic reports whether every code point of s is in L, M, N, P, S, or Zs. It is false on "".
Details
truewhen every code point ofsis a letter, mark, number, punctuation, symbol, or space separator (Zs);falseon"".- Every space separator counts, such as the no-break space U+00A0.
String.isPrintadmits only the space U+0020. - Control and format characters, private use, unassigned code points, and the line and paragraph separators are not graphic.
Basic examples
String.isGraphic("Hello, world!") // true
String.isGraphic("a\tb") // false
String.isGraphic("🐜") // trueCall it through .:
"x = 1 + 2".isGraphic() // trueScope
The empty string has no code points, so it is not all graphic:
"".isGraphic() // falseEvery code point in the classes L, M, N, P, and S is graphic, whatever its script: letters, marks, numbers, punctuation, and symbols:
"中文".isGraphic() // true
"\u{301}".isGraphic() // true
"½Ⅻ٣".isGraphic() // true
"«»¿".isGraphic() // true
"۩+".isGraphic() // trueOf the separators, only the space separators are graphic. The line separator U+2028 is not:
" ".isGraphic() // true
"\u{A0}".isGraphic() // true
"\u{3000}".isGraphic() // true
"\u{2028}".isGraphic() // falseControl, format, private-use, and unassigned code points are not graphic:
"\n".isGraphic() // false
"\u{200B}".isGraphic() // false
"\u{E000}".isGraphic() // false
"\u{378}".isGraphic() // falseA letter with a combining accent is two code points, a letter and a mark, and both are graphic:
"e\u{301}".isGraphic() // trueA flag is two regional indicator symbols, category So, so it is graphic too:
"🇨🇭".isGraphic() // trueA string built with String.fromCodePoint works as a literal one does:
let nbsp = String.fromCodePoint(0xA0) ?? ""
nbsp.isGraphic() // trueTest each code point of a string with String.chars in a pipe:
"a\u{200B}b\n" | String.chars() | Array.map({ x.isGraphic() }) // [true, false, true, false]Applications
Accept a label only when it is one line of text with no control or format characters and not only spaces:
func validLabel(s: string): bool {
s.isGraphic() && s.trim() != ""
}
for s in ["Inbox", "In\nbox", "In\u{200B}box", " "] {
println(validLabel(s))
}true
false
false
falseReport each code point that is not graphic, with its position and its category:
type Bad = { index: int, category: string }
func badCodePoints(s: string): Array[Bad] {
let out: Array[Bad] = []
var i = 0
for c in s {
if !c.isGraphic() {
out.push(Bad{ index: i, category: c.category() })
}
i += 1
}
out
}
for b in badCodePoints("ok\t\u{AD}fine\u{2028}") {
println("${b.index}: ${b.category}")
}2: Cc
3: Cf
8: ZlReplace every code point that is not graphic with U+FFFD, the replacement character:
func visible(s: string): string {
String.concat(s.chars().map({ if x.isGraphic() { x } else { "\u{FFFD}" } }))
}
visible("bell\u{7} tab\t") // "bell� tab�"Properties and relations
String.isGraphic(s) is s not empty and every code point in L, M, N, P, S, or Zs:
for s in ["", "a", "a b", "\u{A0}", "\n", "x\u{AD}", "\u{2028}", "🐜!", "\u{E000}"] {
let chars = s.chars()
let all = !chars.isEmpty() &&
chars.every({
let k = x.category()
["L", "M", "N", "P", "S"].contains(k.slice(0, 1)) || k == "Zs"
})
assert(s.isGraphic() == all)
}Every printable string is graphic. The converse fails only on the space separators other than U+0020:
for s in ["a", " ", "\u{A0}", "\u{3000}", "\t", "€", "\u{301}"] {
if s.isPrint() {
assert(s.isGraphic())
}
let differ = s.isGraphic() != s.isPrint()
assert(differ == (s.category() == "Zs" && s != " "))
}For two non-empty strings, the concatenation is graphic exactly when both parts are:
for a in ["a", "\n", "\u{A0}"] {
for b in ["1", "\u{200D}", "!"] {
assert(String.isGraphic(a + b) == (a.isGraphic() && b.isGraphic()))
}
}Possible issues
String.isGraphic is not a test for visible ink. A string of spaces, no-break spaces, or a lone combining mark is graphic:
" ".isGraphic() // true
"\u{A0}".isGraphic() // true
"\u{301}".isGraphic() // trueAn emoji joined from several, such as a family, holds the zero-width joiner U+200D. The joiner is a format character, so the whole emoji is not graphic:
"👨👩👧".isGraphic() // false
"\u{200D}".category() // "Cf"A graphic string can still render with escapes. A string inside a container shows every code point that String.isPrint rejects as \u{...}, and the no-break space is one of them:
["a\u{A0}b"] // ["a\u{a0}b"]
"a\u{A0}b".isGraphic() // trueString.isGraphic tests that the whole string is graphic, not that it holds a graphic code point:
"a\n".isGraphic() // false
"a\n".chars().some({ x.isGraphic() }) // trueA code point that a Unicode version newer than String.unicodeVersion assigns, such as a newly added emoji, is unassigned (Cn) here, so String.isGraphic returns false for it. U+0378 is unassigned in the tables in use:
String.unicodeVersion // "17.0.0"
"\u{378}".category() // "Cn"
"\u{378}".isGraphic() // falseSee also
String.isPrint · String.isControl · String.category · String.isSpace · String.isLetter · String.isMark · String.chars · String.graphemes
