STUDIO HAZE · topaz
Topaz source
Open by house policy. Topaz is our own language.
Topaz 5.20 · 56 files
compiler/crates/topaz_self_frontend/topaz/emitter/rust_text.tpz
Topaz · 865 B · Original file
1
// Rust source text encoding, independent of the IR transport.2
const HEX_DIGITS = "0123456789abcdef"4
function hex(value: int) -> string {5
if value < 16 {6
HEX_DIGITS.slice(value, value + 1)7
} else {8
hex(value / 16) + HEX_DIGITS.slice(value % 16, value % 16 + 1)9
}10
}12
// A Rust string literal whose value is exactly `text`.13
export function rustStr(text: string) -> string {14
let mut out: Array<string> = ["\""]15
for scalar in text.scalars() {16
let code = scalar.codePointAt(0) ?? 017
if scalar == "\\" {18
out.push("\\\\")19
} else if scalar == "\"" {20
out.push("\\\"")21
} else if scalar == "\n" {22
out.push("\\n")23
} else if scalar == "\t" {24
out.push("\\t")25
} else if code < 32 || code == 127 {26
out.push("\\u\{{hex(code)}\}")27
} else {28
out.push(scalar)29
}30
}31
out.push("\"")32
out.join("")33
}SHA256 e9a9321e837a86b517b5fac136fd51712bd1a303b75a76714467744598b1867e