Merge pull request #1188 from corona10/hex_format

builtin: Fix hex format of ascii
This commit is contained in:
Windel Bouwman
2019-07-31 13:20:09 +02:00
committed by GitHub
2 changed files with 10 additions and 2 deletions

View File

@@ -1,4 +1,7 @@
assert ascii('hello world') == "'hello world'"
assert ascii('안녕 세상') == "'\\uc548\\ub155 \\uc138\\uc0c1'"
assert ascii('안녕 RustPython') == "'\\uc548\\ub155 RustPython'"
assert ascii(5) == '5'
assert ascii(5) == '5'
assert ascii(chr(0x10001)) == "'\\U00010001'"
assert ascii(chr(0x9999)) == "'\\u9999'"
assert ascii(chr(0x0A)) == "'\\n'"

View File

@@ -66,7 +66,12 @@ fn builtin_ascii(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
if c.is_ascii() {
ascii.push(c)
} else {
let hex = format!("\\u{:x}", c as i64);
let c = c as i64;
let hex = if c < 0x10000 {
format!("\\u{:04x}", c)
} else {
format!("\\U{:08x}", c)
};
ascii.push_str(&hex)
}
}