Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
builtin: Fix hex format of ascii
  • Loading branch information
corona10 committed Jul 28, 2019
commit 4ee80a3d42a0d080fc7f06e94a15468653afb9d6
3 changes: 2 additions & 1 deletion tests/snippets/builtin_ascii.py
Original file line number Diff line number Diff line change
@@ -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'"
9 changes: 6 additions & 3 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
corona10 marked this conversation as resolved.
Outdated
} else if (c as i64) < 0x10000 {
Comment thread
corona10 marked this conversation as resolved.
Outdated
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)
Expand Down