From b7abcf19b12714043c88ff6632ff09d6518ab893 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 5 Aug 2025 13:40:01 +0800 Subject: [PATCH] remove trailing newline --- include/pocketpy/config.h | 4 ++-- src/common/sourcedata.c | 10 ++++++++++ tests/66_eval.py | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index a6571453..53109114 100644 --- a/include/pocketpy/config.h +++ b/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 diff --git a/src/common/sourcedata.c b/src/common/sourcedata.c index 5cda19bb..c3a6c1eb 100644 --- a/src/common/sourcedata.c +++ b/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); } diff --git a/tests/66_eval.py b/tests/66_eval.py index 2a51c22a..93c3e7a5 100644 --- a/tests/66_eval.py +++ b/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] \ No newline at end of file