String.isPrint
func String.isPrint(s: string): boolString.isPrint reports whether every code point of s is in L, M, N, P, or S, or is the space U+0020. It is false on "".
Details
truewhen every code point ofsis a letter, mark, number, punctuation, symbol, or the space U+0020;falseon"".- U+0020 is the only space it accepts; the no-break space U+00A0 is not printable.
String.isGraphicaccepts every space separator. - Control characters such as tab and line feed, format characters, private use, and unassigned code points are not printable.
String.fromescapes them inside a quoted string.
Basic examples
String.isPrint("hello") // true
String.isPrint("hello world") // true
String.isPrint("a\tb") // falseCall it through .:
"Ant 1.0!".isPrint() // trueScope
The empty string has no code points, so it is not all printable:
String.isPrint("") // falseLetters, digits, punctuation, and symbols of any script are printable:
String.isPrint("日本語") // true
String.isPrint("½€!") // true
String.isPrint("Ⅻ + ٣") // trueA combining mark is printable, alone or after its base letter:
String.isPrint("e\u{301}") // true
String.isPrint("\u{301}") // trueThe space U+0020 is printable. The no-break space and the ideographic space are space separators too, but they are not:
String.isPrint(" ") // true
String.isPrint("\u{A0}") // false
String.isPrint("\u{3000}") // falseInvisible characters of other categories are not printable: a control character, a format character such as the zero-width space or a bidirectional override, a line separator, a private-use code point, and an unassigned one:
String.isPrint("\u{0}") // false
String.isPrint("\u{200B}") // false
String.isPrint("\u{202E}") // false
String.isPrint("\u{2028}") // false
String.isPrint("\u{E000}") // false
String.isPrint("\u{378}") // falseAn emoji is a symbol. An emoji joined from several holds the zero-width joiner U+200D, which is a format character:
String.isPrint("👍") // true
String.isPrint("👨👩👧") // falseTest each code point of a string with String.chars in a pipe:
"a b\n" | String.chars() | Array.map({ x.isPrint() }) // [true, true, true, false]Applications
Accept a display name only when every code point of it is printable and it has no space at either end:
func validName(name: string): bool {
name.isPrint() && name.trim() == name
}
validName("Queen Ant") // true
validName("ant\u{0}") // false
validName(" ant") // falseRemove the code points that are not printable from untrusted text, such as before writing it to a log:
func clean(s: string): string {
String.concat(s.chars().filter({ x.isPrint() }))
}
clean("pay\u{A0}100\u{200B}€\n") // "pay100€"Show the code point of every character that String.isPrint rejects, as U+ and its hexadecimal value:
func reveal(s: string): string {
let parts = s.chars().map({
if x.isPrint() {
x
} else {
let hex = String.fromInt(x.codePoint(), 16).toUpper()
"<U+${hex.padStart(4, "0")}>"
}
})
String.concat(parts)
}
println(reveal("pay\u{A0}100\u{200B}€\n"))pay<U+00A0>100<U+200B>€<U+000A>String.from writes a string inside a container with escapes for the code points that String.isPrint rejects, so it shows what a string holds:
println(String.from(["a\u{A0}b", "tab\there", "日本"]))["a\u{a0}b", "tab\there", "日本"]Properties and relations
String.isPrint(s) is s not empty and every code point in the categories L, M, N, P, or S, or the space:
func printable(c: string): bool {
c == " " || ["L", "M", "N", "P", "S"].contains(c.category().slice(0, 1))
}
for s in ["", "abc", "a b", "a\tb", "\u{A0}", "e\u{301}", "½", "\u{200D}", "\u{E000}"] {
let chars = s.chars()
assert(String.isPrint(s) == (!chars.isEmpty() && chars.every(printable)))
}A printable string is graphic. The two differ only on the space separators other than U+0020:
for s in ["abc", "a b", "€", "\u{A0}", "\u{3000}", "\u{2009}"] {
if String.isPrint(s) {
assert(String.isGraphic(s))
}
}
assert(String.isGraphic("\u{A0}") && !String.isPrint("\u{A0}"))No control character is printable:
for n in [0, 9, 10, 13, 27, 127, 133, 159] {
let c = String.fromCodePoint(n) ?? ""
assert(String.isControl(c) && !String.isPrint(c))
}Possible issues
A tab and a line break are not printable, so a text of several lines is not:
String.isPrint("one\ntwo") // false
String.isPrint("one two") // trueThe no-break space looks like a space, and a text pasted from a web page often holds it. It is not printable:
String.isPrint("10\u{A0}km") // false
String.isGraphic("10\u{A0}km") // trueAn emoji that looks like one character can be several code points joined by U+200D, and the joiner is not printable:
String.isPrint("👨👩👧") // false
"👨👩👧".chars().map({ x.isPrint() }) // [true, false, true, false, true]String.isPrint tests that the whole string is printable, not that it holds a printable character. To find out whether a string has one, test each code point:
String.isPrint("ok\n") // false
"ok\n".chars().some({ x.isPrint() }) // trueSee also
String.isGraphic · String.isControl · String.isSpace · String.category · String.chars · String.from
