builtin: Fix hex format of ascii

This commit is contained in:
Dong-hee Na
2019-07-29 01:23:40 +09:00
parent 50dd93db1e
commit 4ee80a3d42
2 changed files with 8 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
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'"

View File

@@ -64,10 +64,13 @@ fn builtin_ascii(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
let mut ascii = String::new();
for c in repr.value.chars() {
if c.is_ascii() {
ascii.push(c)
} else {
ascii.push(c);
} else if (c as i64) < 0x10000 {
let hex = format!("\\u{:x}", c as i64);
ascii.push_str(&hex)
ascii.push_str(&hex);
} else {
let hex = format!("\\U{:08x}", c as i64);
ascii.push_str(&hex);
}
}
Ok(ascii)