Explorar o código

add `PK_SLICE_LOOP`

blueloveTH %!s(int64=2) %!d(string=hai) anos
pai
achega
38dceac61b
Modificáronse 3 ficheiros con 5 adicións e 3 borrados
  1. 2 0
      include/pocketpy/common.h
  2. 1 1
      src/pocketpy.cpp
  3. 2 2
      src/str.cpp

+ 2 - 0
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?i<stop:i>stop; i+=step)
+
 } // namespace pkpy

+ 1 - 1
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?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");

+ 2 - 2
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?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();
     }