String.fromCodePoint
func String.fromCodePoint(n: int): string | nullString.fromCodePoint returns the one-code-point string for n, or null when n is not a Unicode scalar value.
Details
- Returns
nullwhennis not a Unicode scalar value: a negative number, a number above0x10FFFF, or a surrogate,0xD800to0xDFFF.
Basic examples
String.fromCodePoint(65) // "A"
String.fromCodePoint(233) // "é"
String.fromCodePoint(0x1F41C) // "🐜"A number that is not a scalar value gives null:
String.fromCodePoint(0xD800) // nullScope
The valid range runs from 0 to 0x10FFFF, with a gap for the surrogates. Each side of each edge:
String.fromCodePoint(-1) // null
String.fromCodePoint(0) // "\u{0}"
String.fromCodePoint(0xD7FF) // "\u{d7ff}"
String.fromCodePoint(0xDFFF) // null
String.fromCodePoint(0xE000) // "\u{e000}"
String.fromCodePoint(0x10FFFF) // "\u{10ffff}"
String.fromCodePoint(0x110000) // nullThe result for a control character is that character, shown here by its escape:
String.fromCodePoint(10) == "\n" // true
String.fromCodePoint(9) // "\t"A combining mark is a code point on its own, even though it draws nothing by itself:
String.fromCodePoint(0x301)?.length() // 1The parameter is named n:
String.fromCodePoint(n: 90) // "Z"In a pipe, the number is the first argument:
0x41 | String.fromCodePoint() // "A"As a function value, it maps an array of numbers to an array of nullable strings:
[72, 105, -1].map(String.fromCodePoint) // ["H", "i", null]Applications
Build the lower-case Latin alphabet from the code point of "a":
let a = "a".codePoint()
let letters = Iter.range(0, 26) | Iter.map({ String.fromCodePoint(a + x) ?? "" }) | Iter.toArray()
letters | String.join("") // "abcdefghijklmnopqrstuvwxyz"Read a code point written as U+ and hexadecimal digits, and reject one that names no character:
func parseUPlus(s: string): string | null {
if !s.startsWith("U+") {
return null
}
let n = s.slice(2, s.length()).toInt(16)
if n == null {
return null
}
String.fromCodePoint(n)
}
parseUPlus("U+1F41C") // "🐜"
parseUPlus("U+00E9") // "é"
parseUPlus("U+D800") // null
parseUPlus("U+zz") // nullReplace a value that is not a scalar value with U+FFFD, the replacement character:
func toText(points: Array[int]): string {
points.map({ String.fromCodePoint(x) ?? "\u{FFFD}" }) | String.join("")
}
toText([65, 0xD800, 110, 116]) // "A�nt"Properties and relations
The result of a scalar value is one code point long, and String.codePoint turns it back into n:
for n in [0, 65, 233, 0xD7FF, 0xE000, 0x1F41C, 0x10FFFF] {
let c = String.fromCodePoint(n)
assert(c != null && c.length() == 1 && c.codePoint() == n)
}It agrees with the \u{...} escape, and with String.fromCodePoints on a one-element array:
for n in [0x41, 0xE9, 0x1F41C] {
assert(String.fromCodePoint(n) == String.fromCodePoints([n]))
}
assert(String.fromCodePoint(0x1F41C) == "\u{1F41C}")
assert(String.fromCodePoint(0xDC00) == String.fromCodePoints([0xDC00]))Possible issues
The result may be null, so passing it where a string is expected is a compile error:
let s: string = String.fromCodePoint(65)
// error: cannot use string | null where string is expectedGive a default with ??, or test against null first.
String.fromCodePoint is a function of String, not of int, so it cannot be called through . on a number:
let n = 97
println(n.fromCodePoint()) // error: int has no field or function fromCodePointCall it as String.fromCodePoint(n), or in a pipe as n | String.fromCodePoint().
A code point held in another integer type, such as a byte read from Bytes, is not an int:
let b = byte(65)
String.fromCodePoint(b) // error: cannot use byte where int is expectedConvert it first: String.fromCodePoint(int(b)).
Code point 0 is a string of one character, not the empty string:
String.fromCodePoint(0) == "" // false
String.fromCodePoint(0)?.length() // 1One code point is not always one character as a reader sees it. A combining accent joins the letter before it into one grapheme:
let e = "e" + (String.fromCodePoint(0x301) ?? "")
e.length() // 2
e.graphemes().length() // 1See also
String.codePoint · String.fromCodePoints · String.toCodePoints · String.chars · String.graphemes · String.fromBytes
