String.fromCodePoints
func String.fromCodePoints(points: Array[int]): string | nullString.fromCodePoints returns the string of the code points in points, or null when any is not a Unicode scalar value.
Details
- An empty array gives
"". - Returns
nullwhen any value is not a Unicode scalar value: a negative number, a number above0x10FFFF, or a surrogate. There is no partial string and no replacement character.
Basic examples
String.fromCodePoints([97, 110, 116]) // "ant"
String.fromCodePoints([0x1F41C]) // "🐜"
String.fromCodePoints([0xD800]) // nullScope
An empty array gives the empty string:
String.fromCodePoints([]) // ""Every scalar value counts, from 0 up to 0x10FFFF, control characters and U+FFFD among them:
String.fromCodePoints([0]) == "\u{0}" // true
String.fromCodePoints([9, 10]) == "\t\n" // true
String.fromCodePoints([0x10FFFF]) == "\u{10FFFF}" // true
String.fromCodePoints([0xFFFD]) == "\u{FFFD}" // trueThe values just around the surrogate range are scalar values; the surrogates are not:
String.fromCodePoints([0xD7FF]) == "\u{D7FF}" // true
String.fromCodePoints([0xDFFF]) // null
String.fromCodePoints([0xE000]) == "\u{E000}" // trueValues out of range give null, wherever they sit in the array:
String.fromCodePoints([-1]) // null
String.fromCodePoints([0x110000]) // null
String.fromCodePoints([97, 98, -1]) // nullA combining mark is a code point of its own, so e and U+0301 give a string of two code points that reads as one character:
let s = String.fromCodePoints([101, 0x301]) ?? ""
s.length() // 2
s.graphemes().length() // 1In a pipe, the array is the first argument:
[72, 105] | String.fromCodePoints() // "Hi"A function value works wherever a func(Array[int]): string | null is expected:
let decode: func(Array[int]): string | null = String.fromCodePoints
decode([79, 75]) // "OK"Named arguments
Name the argument points:
String.fromCodePoints(points: [65, 66]) // "AB"Applications
Build the lower-case Latin alphabet from a range of code points:
String.fromCodePoints(Array.range(97, 123)) // "abcdefghijklmnopqrstuvwxyz"Rotate lower-case Latin letters by 13 with ROT13, working on the code points and leaving the rest as it is:
func rot13(s: string): string {
let points = s.toCodePoints().map({
if x >= 97 && x <= 122 { (x - 97 + 13) % 26 + 97 } else { x }
})
String.fromCodePoints(points) ?? s
}
println(rot13("hello, ant"))
println(rot13(rot13("hello, ant")))uryyb, nag
hello, antDecode hexadecimal code points from untrusted input, and reject the input when any is not a scalar value:
func decode(hex: string): string | null {
let points = hex.fields().map({ x.toInt(16) ?? -1 })
String.fromCodePoints(points)
}
println(decode("41 F1 1F41C") ?? "invalid")
println(decode("41 D800") ?? "invalid")
println(decode("41 zz") ?? "invalid")Añ🐜
invalid
invalidProperties and relations
It undoes String.toCodePoints:
for s in ["", "ant", "héllo 日本", "e\u{301}", "🐜\u{0}"] {
assert(String.fromCodePoints(s.toCodePoints()) == s)
}For one value, it agrees with String.fromCodePoint:
for n in [-1, 0, 65, 0xD800, 0xE000, 0x1F41C, 0x10FFFF, 0x110000] {
assert(String.fromCodePoints([n]) == String.fromCodePoint(n))
}A result that is not null has one code point per value:
let points = [0x41, 0xE9, 0x65E5, 0x1F41C]
let s = String.fromCodePoints(points)
assert(s != null && s.length() == len(points))It is the String.concat of each value's one-code-point string:
let points = [97, 0x301, 0x1F41C]
let parts = points.map({ String.fromCodePoint(x) ?? "" })
assert(String.fromCodePoints(points) == String.concat(parts))Possible issues
The result may be null, so using it where a string is expected is a compile error. Supply a fallback with ??, or narrow it with if:
let s: string = String.fromCodePoints([97])
// error: cannot use string | null where string is expectedOne bad value makes the whole result null. To keep the valid ones, filter the array first:
let points = [72, 0xD800, 105, -5]
String.fromCodePoints(points) // null
String.fromCodePoints(points.filter({ String.fromCodePoint(x) != null })) // "Hi"A UTF-16 surrogate pair, as JavaScript's String.fromCharCode takes it, is not a code point. Each half is a surrogate, so the pair gives null. Pass the scalar value itself:
String.fromCodePoints([0xD83D, 0xDC1C]) // null
String.fromCodePoints([0x1F41C]) // "🐜"The argument is an array, not a list of values:
let s = String.fromCodePoints(97, 98) // error: String.fromCodePoints takes 1 argument, got 2The array is the subject of a call through ., and a value reaches its own type's namespace, so points.fromCodePoints() looks in Array and is a compile error. Use a pipe instead:
let s = [97, 98].fromCodePoints() // error: Array[int] has no field or function fromCodePointsThe result is not normalized. A letter and a combining accent differ from the one-code-point letter until String.normalize brings both to one form:
let s = String.fromCodePoints([101, 0x301]) ?? ""
s == "\u{E9}" // false
s.normalize("NFC") == "\u{E9}" // trueSee also
String.toCodePoints · String.fromCodePoint · String.codePoint · String.chars · String.concat · String.fromBytes · String.normalize
