Sfoglia il codice sorgente

remove trailing newline

blueloveTH 7 mesi fa
parent
commit
b7abcf19b1
3 ha cambiato i file con 14 aggiunte e 2 eliminazioni
  1. 2 2
      include/pocketpy/config.h
  2. 10 0
      src/common/sourcedata.c
  3. 2 0
      tests/66_eval.py

+ 2 - 2
include/pocketpy/config.h

@@ -1,10 +1,10 @@
 #pragma once
 // clang-format off
 
-#define PK_VERSION				"2.1.0"
+#define PK_VERSION				"2.1.1"
 #define PK_VERSION_MAJOR            2
 #define PK_VERSION_MINOR            1
-#define PK_VERSION_PATCH            0
+#define PK_VERSION_PATCH            1
 
 /*************** feature settings ***************/
 #ifndef PK_ENABLE_OS                // can be overridden by cmake

+ 10 - 0
src/common/sourcedata.c

@@ -24,6 +24,16 @@ static void SourceData__ctor(struct SourceData* self,
         source++;
     }
     self->source = c11_sbuf__submit(&ss);
+    // remove trailing newline
+    int last_index = self->source->size - 1;
+    while(last_index >= 0 && isspace(self->source->data[last_index])) {
+        last_index--;
+    }
+    if(last_index >= 0) {
+        self->source->size = last_index + 1;
+        self->source->data[last_index + 1] = '\0';
+    }
+
     self->is_dynamic = is_dynamic;
     c11_vector__push(const char*, &self->line_starts, self->source->data);
 }

+ 2 - 0
tests/66_eval.py

@@ -74,3 +74,5 @@ exec(code, {'x': 42, 'res': res})
 assert res == [42, 42]
 assert x == 33
 
+# test removing trailing newlines
+assert eval('[1, 2, 3]\n  \n') == [1, 2, 3]