String.codePoint
func String.codePoint(c: string): intString.codePoint returns the scalar value of c. It panics unless c is one code point.
Details
- Panics unless
cholds exactly one code point:"", a longer string, and a character written with a combining accent all panic. - The result is at most
0x10FFFFand never a surrogate.
Basic examples
String.codePoint("A") // 65
String.codePoint("é") // 233
String.codePoint("🐜") // 128028Call it through . on a string:
"a".codePoint() // 97Scope
Control characters and escapes are code points like any other:
String.codePoint("\n") // 10
String.codePoint("\t") // 9
String.codePoint("\u{0}") // 0
String.codePoint("\u{10FFFF}") // 1114111A code point outside the Basic Multilingual Plane is still one code point. String.fromInt with base 16 shows it in hexadecimal:
String.codePoint("🐜") | String.fromInt(16) // "1f41c"The replacement character U+FFFD, which stands in for ill-formed UTF-8 that reached the program from outside, is an ordinary code point:
String.codePoint("\u{FFFD}") // 65533The parameter is named c:
String.codePoint(c: "Z") // 90In a pipe, the string is the first argument:
"A" | String.codePoint() // 65A function value works wherever a func(string): int is expected:
let code: func(string): int = String.codePoint
code("0") // 48
"ant" | String.chars() | Array.map(String.codePoint) // [97, 110, 116]Applications
Turn a decimal digit into its value by distance from "0":
func digit(c: string): int {
c.codePoint() - "0".codePoint()
}
"2026" | String.chars() | Array.map(digit) // [2, 0, 2, 6]Shift lower-case Latin letters for a Caesar cipher, and leave everything else as it is:
func shift(c: string, k: int): string {
let n = c.codePoint()
let a = "a".codePoint()
if n < a || n > "z".codePoint() {
return c
}
String.fromCodePoint((n - a + k) % 26 + a) ?? c
}
func caesar(s: string, k: int): string {
s.chars().map({ shift(x, k) }) | String.join("")
}
println(caesar("hello, ant", 3))khoor, dqwPrint each character of a string with its U+ code:
for c in "Añ🐜" {
let hex = c.codePoint() | String.fromInt(16) | String.toUpper() | String.padStart(4, "0")
println("${c} U+${hex}")
}A U+0041
ñ U+00F1
🐜 U+1F41CProperties and relations
String.codePoint of each character agrees with String.toCodePoints of the whole string, and String.fromCodePoint turns the value back into the character:
let s = "Ant 🐜 é"
assert(s.chars().map(String.codePoint) == s.toCodePoints())
for c in s {
assert(String.fromCodePoint(c.codePoint()) == c)
}For a scalar value n, String.fromCodePoint gives a character whose code point is n again:
for n in [0, 65, 233, 0xD7FF, 0xE000, 0x1F41C, 0x10FFFF] {
let c = String.fromCodePoint(n)
assert(c != null && c.codePoint() == n)
}Possible issues
The string must hold exactly one code point. The empty string and a longer string panic:
String.codePoint("") // panic: String.codePoint needs a string of one code point, got ""
String.codePoint("ab") // panic: String.codePoint needs a string of one code point, got "ab"What a reader sees as one character can be several code points. An emoji with a skin tone is two:
String.codePoint("👍🏽") // panic: String.codePoint needs a string of one code point, got "👍🏽"A letter with a combining accent is two code points as well. String.normalize with "NFC" composes it into one when Unicode has a single code point for it:
let e = "e\u{301}"
e.length() // 2
String.normalize(e, "NFC").codePoint() // 233The code point is not the UTF-8 encoding. Outside ASCII the two differ: "é" is code point 233 but two bytes. String.toBytes gives the bytes:
String.codePoint("é") // 233
String.toBytes("é") // Bytes(c3 a9)To get the code points of a longer string, use String.toCodePoints, or split it with String.chars first:
String.toCodePoints("ab") // [97, 98]See also
String.fromCodePoint · String.toCodePoints · String.fromCodePoints · String.chars · String.graphemes · String.category · String.normalize · String.toBytes
