add PK_SLICE_LOOP

This commit is contained in:
blueloveTH 2024-02-20 02:13:33 +08:00
parent 3534492bb6
commit 38dceac61b
3 changed files with 5 additions and 3 deletions

View File

@ -234,4 +234,6 @@ inline const char* kPlatformStrings[] = {
"unknown" // 6
};
#define PK_SLICE_LOOP(i, start, stop, step) for(int i=start; step>0?i<stop:i>stop; i+=step)
} // namespace pkpy

View File

@ -20,7 +20,7 @@ PyObject* PyArrayGetItem(VM* vm, PyObject* _0, PyObject* _1){
int start, stop, step;
vm->parse_int_slice(s, self.size(), start, stop, step);
List new_list;
for(int i=start; step>0?i<stop:i>stop; i+=step) new_list.push_back(self[i]);
PK_SLICE_LOOP(i, start, stop, step) new_list.push_back(self[i]);
return VAR(T(std::move(new_list)));
}
vm->TypeError("indices must be integers or slices");

View File

@ -334,9 +334,9 @@ int utf8len(unsigned char c, bool suppress){
Str Str::u8_slice(int start, int stop, int step) const{
SStream ss;
if(is_ascii){
for(int i=start; step>0?i<stop:i>stop; i+=step) ss << data[i];
PK_SLICE_LOOP(i, start, stop, step) ss << data[i];
}else{
for(int i=start; step>0?i<stop:i>stop; i+=step) ss << u8_getitem(i);
PK_SLICE_LOOP(i, start, stop, step) ss << u8_getitem(i);
}
return ss.str();
}