Skip to content

Commit 5e3d22a

Browse files
committed
Convert ord
1 parent b67ee2e commit 5e3d22a

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

vm/src/builtins.rs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::obj::objcode::PyCodeRef;
1616
use crate::obj::objdict::PyDictRef;
1717
use crate::obj::objint::{self, PyIntRef};
1818
use crate::obj::objiter;
19-
use crate::obj::objstr::{self, PyString, PyStringRef};
19+
use crate::obj::objstr::{PyString, PyStringRef};
2020
use crate::obj::objtype::{self, PyClassRef};
2121
#[cfg(feature = "rustpython-compiler")]
2222
use rustpython_compiler::compile;
@@ -499,40 +499,34 @@ fn builtin_oct(number: PyIntRef, vm: &VirtualMachine) -> PyResult {
499499
Ok(vm.new_str(s))
500500
}
501501

502-
fn builtin_ord(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
503-
arg_check!(vm, args, required = [(string, None)]);
504-
if objtype::isinstance(string, &vm.ctx.str_type()) {
505-
let string = objstr::borrow_value(string);
506-
let string_len = string.chars().count();
507-
if string_len != 1 {
508-
return Err(vm.new_type_error(format!(
509-
"ord() expected a character, but string of length {} found",
510-
string_len
511-
)));
512-
}
513-
match string.chars().next() {
514-
Some(character) => Ok(vm.context().new_int(character as i32)),
515-
None => Err(vm.new_type_error(
516-
"ord() could not guess the integer representing this character".to_string(),
517-
)),
502+
fn builtin_ord(string: Either<PyByteInner, PyStringRef>, vm: &VirtualMachine) -> PyResult {
503+
match string {
504+
Either::A(bytes) => {
505+
let bytes_len = bytes.elements.len();
506+
if bytes_len != 1 {
507+
return Err(vm.new_type_error(format!(
508+
"ord() expected a character, but string of length {} found",
509+
bytes_len
510+
)));
511+
}
512+
Ok(vm.context().new_int(bytes.elements[0]))
518513
}
519-
} else if objtype::isinstance(string, &vm.ctx.bytearray_type())
520-
|| objtype::isinstance(string, &vm.ctx.bytes_type())
521-
{
522-
let inner = PyByteInner::try_from_object(vm, string.clone()).unwrap();
523-
let bytes_len = inner.elements.len();
524-
if bytes_len != 1 {
525-
return Err(vm.new_type_error(format!(
526-
"ord() expected a character, but string of length {} found",
527-
bytes_len
528-
)));
514+
Either::B(string) => {
515+
let string = string.as_str();
516+
let string_len = string.chars().count();
517+
if string_len != 1 {
518+
return Err(vm.new_type_error(format!(
519+
"ord() expected a character, but string of length {} found",
520+
string_len
521+
)));
522+
}
523+
match string.chars().next() {
524+
Some(character) => Ok(vm.context().new_int(character as i32)),
525+
None => Err(vm.new_type_error(
526+
"ord() could not guess the integer representing this character".to_string(),
527+
)),
528+
}
529529
}
530-
Ok(vm.context().new_int(inner.elements[0]))
531-
} else {
532-
Err(vm.new_type_error(format!(
533-
"ord() expected a string, bytes or bytearray, but found {}",
534-
string.class().name
535-
)))
536530
}
537531
}
538532

0 commit comments

Comments
 (0)