This commit is contained in:
blueloveTH 2023-05-17 13:47:35 +08:00
parent c9926cdcea
commit d204c67d62
3 changed files with 18 additions and 13 deletions

View File

@ -1,5 +1,5 @@
clang++ -pg -O2 -std=c++17 -fno-rtti -stdlib=libc++ -Wall -o pocketpy src/main.cpp
time ./pocketpy benchmarks/sort.py
time ./pocketpy benchmarks/primes.py
mv benchmarks/gmon.out .
gprof pocketpy gmon.out > gprof.txt
rm gmon.out

View File

@ -389,19 +389,9 @@ inline void init_builtins(VM* _vm) {
});
_vm->bind_method<1>("str", "__getitem__", [](VM* vm, ArgsView args) {
const Str& self = _CAST(Str&, args[0]);
if(is_type(args[1], vm->tp_slice)){
const Slice& s = _CAST(Slice&, args[1]);
int start, stop, step;
vm->parse_int_slice(s, self.u8_length(), start, stop, step);
return VAR(self.u8_slice(start, stop, step));
}
int index = CAST(int, args[1]);
index = vm->normalized_index(index, self.u8_length());
return VAR(self.u8_getitem(index));
return PyStrGetItem(vm, args[0], args[1]);
});
_vm->_type_info("str")->m__getitem__ = PyStrGetItem;
_vm->bind_method<1>("str", "__gt__", [](VM* vm, ArgsView args) {
const Str& self = _CAST(Str&, args[0]);

View File

@ -1263,4 +1263,19 @@ inline PyObject* PyListSetItem(VM* vm, PyObject* obj, PyObject* index, PyObject*
return vm->None;
}
inline PyObject* PyStrGetItem(VM* vm, PyObject* obj, PyObject* index){
const Str& self = _CAST(Str&, obj);
if(is_type(index, vm->tp_slice)){
const Slice& s = _CAST(Slice&, index);
int start, stop, step;
vm->parse_int_slice(s, self.u8_length(), start, stop, step);
return VAR(self.u8_slice(start, stop, step));
}
int i = CAST(int, index);
i = vm->normalized_index(i, self.u8_length());
return VAR(self.u8_getitem(i));
}
} // namespace pkpy