From 38dceac61b43672eeed8b2a711ca7ed80c923936 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 20 Feb 2024 02:13:33 +0800 Subject: [PATCH] add `PK_SLICE_LOOP` --- include/pocketpy/common.h | 2 ++ src/pocketpy.cpp | 2 +- src/str.cpp | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index ac40d29c..3bf7b9fc 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -234,4 +234,6 @@ inline const char* kPlatformStrings[] = { "unknown" // 6 }; +#define PK_SLICE_LOOP(i, start, stop, step) for(int i=start; step>0?istop; i+=step) + } // namespace pkpy diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 5dcc0865..143c116a 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -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?istop; 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"); diff --git a/src/str.cpp b/src/str.cpp index f1042ef6..5d42cbf0 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -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?istop; i+=step) ss << data[i]; + PK_SLICE_LOOP(i, start, stop, step) ss << data[i]; }else{ - for(int i=start; step>0?istop; i+=step) ss << u8_getitem(i); + PK_SLICE_LOOP(i, start, stop, step) ss << u8_getitem(i); } return ss.str(); }