String.toCodePoints
func String.toCodePoints(s: string): Array[int]String.toCodePoints returns the scalar value of each code point of s.
Details
- Returns one
intper code point, in order.""gives[]. - Each value is from
0to0x10FFFFand never a surrogate. - It counts code points, as
String.lengthdoes, not bytes and not what a reader sees as one character.
Basic examples
String.toCodePoints("ant") // [97, 110, 116]
String.toCodePoints("é") // [233]Call it through . on a string:
"Hi".toCodePoints() // [72, 105]Scope
The empty string gives an empty array:
String.toCodePoints("") // []Escapes, control characters, and U+FFFD are code points like any other:
String.toCodePoints("\t\n") // [9, 10]
String.toCodePoints("\u{0}") // [0]
String.toCodePoints("\u{FFFD}") // [65533]
String.toCodePoints("\u{10FFFF}") // [1114111]A code point outside the Basic Multilingual Plane is one value, though UTF-8 stores it in four bytes:
String.toCodePoints("🐜") // [128028]
"🐜".toBytes().length() // 4What a reader sees as one character can be several code points. A letter with a combining accent is two, and a family emoji is three people joined by U+200D, the zero-width joiner:
String.toCodePoints("e\u{301}") // [101, 769]
String.toCodePoints("👨👩👧") // [128104, 8205, 128105, 8205, 128103]In a pipe, the string is the first argument:
"abc" | String.toCodePoints() | Array.map({ x - 96 }) // [1, 2, 3]The argument can be named s:
String.toCodePoints(s: "OK") // [79, 75]A function value works wherever a func(string): Array[int] is expected:
let points: func(string): Array[int] = String.toCodePoints
["a", "bc"].map(points) // [[97], [98, 99]]Applications
Tell whether a string is plain ASCII:
func isAscii(s: string): bool {
s.toCodePoints().every({ x < 128 })
}
isAscii("ant") // true
isAscii("añt") // falseCount the UTF-16 code units of a string, which is what JavaScript's length reports: a code point above 0xFFFF takes two:
func utf16Length(s: string): int {
s.toCodePoints().map({ if x > 0xFFFF { 2 } else { 1 } }).sum() ?? 0
}
utf16Length("ant") // 3
utf16Length("ant 🐜") // 6Print each code point of a string in U+ notation:
let codes = "Añ🐜".toCodePoints().map({
"U+" + String.fromInt(x, 16).toUpper().padStart(4, "0")
})
println(String.join(codes, " "))U+0041 U+00F1 U+1F41CProperties and relations
String.fromCodePoints undoes it, and the array has one value per code point, as String.length counts them:
for s in ["", "ant", "héllo 日本", "e\u{301}", "🐜\u{0}"] {
let points = s.toCodePoints()
assert(String.fromCodePoints(points) == s)
assert(len(points) == s.length())
}It is String.codePoint of each of String.chars, and of each value a for loop yields:
let s = "Ant 🐜 é"
assert(s.toCodePoints() == s.chars().map(String.codePoint))
let points: Array[int] = []
for c in s {
points.push(c.codePoint())
}
assert(points == s.toCodePoints())Every value is a scalar value:
for n in "a\u{0}é日🐜\u{FFFD}\u{10FFFF}".toCodePoints() {
assert(n >= 0 && n <= 0x10FFFF && (n < 0xD800 || n > 0xDFFF))
}Possible issues
The values are code points, not bytes. Use String.toBytes for the UTF-8 encoding:
String.toCodePoints("é") // [233]
"é".toBytes().length() // 2Two spellings of the same text can give different code points. Bring both to one form with String.normalize before you compare them:
"\u{E9}".toCodePoints() == "e\u{301}".toCodePoints() // false
"e\u{301}".normalize("NFC").toCodePoints() == "\u{E9}".toCodePoints() // trueTo split text into what a reader sees as characters, use String.graphemes instead:
len(String.toCodePoints("👍🏽")) // 2
len(String.graphemes("👍🏽")) // 1The argument is a string; passing Bytes is a compile error. Decode the bytes with String.fromBytes first:
let points = String.toCodePoints(String.toBytes("a"))
// error: cannot use Bytes where string is expectedSee also
String.fromCodePoints · String.codePoint · String.fromCodePoint · String.chars · String.graphemes · String.toBytes · String.length · String.normalize
