From 874d3a0b88e2ee32a52b368430e3d4a7e8ab20e3 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 11 Jun 2024 13:37:28 +0800 Subject: [PATCH] complete string move --- include/pocketpy/common/str.h | 9 ++++++++ include/pocketpy/common/str.hpp | 24 ++++++++++++++++++-- include/pocketpy/common/vector.h | 1 + src/common/str.c | 39 +++++++++++++++++++++++++++++++- src/common/str.cpp | 31 ------------------------- src/common/vector.c | 9 ++++++++ 6 files changed, 79 insertions(+), 34 deletions(-) diff --git a/include/pocketpy/common/str.h b/include/pocketpy/common/str.h index 26d8e0a0..7673948a 100644 --- a/include/pocketpy/common/str.h +++ b/include/pocketpy/common/str.h @@ -5,6 +5,13 @@ extern "C" { #endif #include +#include "pocketpy/common/vector.h" + +/* string_view */ +typedef struct c11_string{ + const char* data; + int size; +} c11_string; typedef struct pkpy_Str{ int size; @@ -49,6 +56,8 @@ int pkpy_Str__unicode_index_to_byte(const pkpy_Str* self, int i); int pkpy_Str__byte_index_to_unicode(const pkpy_Str* self, int n); int pkpy_Str__index(const pkpy_Str* self, const pkpy_Str* sub, int start); int pkpy_Str__count(const pkpy_Str* self, const pkpy_Str* sub); +c11_array/* T=c11_string */ pkpy_Str__split(const pkpy_Str* self, char sep); +c11_array/* T=c11_string */ pkpy_Str__split2(const pkpy_Str* self, const pkpy_Str* sep); #ifdef __cplusplus } diff --git a/include/pocketpy/common/str.hpp b/include/pocketpy/common/str.hpp index 65497159..414d3f5a 100644 --- a/include/pocketpy/common/str.hpp +++ b/include/pocketpy/common/str.hpp @@ -2,6 +2,7 @@ #include "pocketpy/common/utils.h" #include "pocketpy/common/memorypool.hpp" +#include "pocketpy/common/vector.h" #include "pocketpy/common/vector.hpp" #include "pocketpy/common/str.h" @@ -190,8 +191,27 @@ struct Str: pkpy_Str { return pkpy_Str__escape(this, quote); } - vector split(const Str& sep) const; - vector split(char sep) const; + vector split(const Str& sep) const{ + c11_array/* T=c11_string */ res = pkpy_Str__split2(this, &sep); + vector retval(res.count); + for(int i = 0; i < res.count; i++){ + c11_string tmp = c11__getitem(c11_string, &res, i); + retval[i] = std::string_view(tmp.data, tmp.size); + } + c11_array__dtor(&res); + return retval; + } + + vector split(char sep) const{ + c11_array/* T=c11_string */ res = pkpy_Str__split(this, sep); + vector retval(res.count); + for(int i = 0; i < res.count; i++){ + c11_string tmp = c11__getitem(c11_string, &res, i); + retval[i] = std::string_view(tmp.data, tmp.size); + } + c11_array__dtor(&res); + return retval; + } int index(const Str& sub, int start = 0) const{ return pkpy_Str__index(this, &sub, start); diff --git a/include/pocketpy/common/vector.h b/include/pocketpy/common/vector.h index 0c5d0bb7..4cb99c25 100644 --- a/include/pocketpy/common/vector.h +++ b/include/pocketpy/common/vector.h @@ -27,6 +27,7 @@ void c11_vector__dtor(c11_vector* self); c11_vector c11_vector__copy(const c11_vector* self); void* c11_vector__at(c11_vector* self, int index); void c11_vector__reserve(c11_vector* self, int capacity); +c11_array c11_vector__as_array(c11_vector* self); #define c11__getitem(T, self, index) ((T*)(self)->data)[index] #define c11__setitem(T, self, index, value) ((T*)(self)->data)[index] = value; diff --git a/src/common/str.c b/src/common/str.c index 26df5ae4..e369c1da 100644 --- a/src/common/str.c +++ b/src/common/str.c @@ -1,5 +1,4 @@ #include "pocketpy/common/str.h" -#include "pocketpy/common/vector.h" #include "pocketpy/common/utils.h" #include @@ -352,3 +351,41 @@ int pkpy_Str__count(const pkpy_Str *self, const pkpy_Str *sub){ return cnt; } +c11_array/* T=c11_string */ pkpy_Str__split(const pkpy_Str *self, char sep){ + c11_vector retval; + c11_vector__ctor(&retval, sizeof(c11_string)); + const char* data = pkpy_Str__data(self); + int i = 0; + for(int j = 0; j < self->size; j++) { + if(data[j] == sep) { + if(j > i){ + c11_string tmp = {data + i, j - i}; + c11_vector__push(c11_string, &retval, tmp); + } + i = j + 1; + continue; + } + } + if(self->size > i){ + c11_string tmp = {data + i, self->size - i}; + c11_vector__push(c11_string, &retval, tmp); + } + return c11_vector__as_array(&retval); +} + +c11_array/* T=c11_string */ pkpy_Str__split2(const pkpy_Str *self, const pkpy_Str *sep){ + c11_vector retval; + c11_vector__ctor(&retval, sizeof(c11_string)); + int start = 0; + const char* data = pkpy_Str__data(self); + while(true) { + int i = pkpy_Str__index(self, sep, start); + if(i == -1) break; + c11_string tmp = {data + start, i - start}; + if(tmp.size != 0) c11_vector__push(c11_string, &retval, tmp); + start = i + sep->size; + } + c11_string tmp = {data + start, self->size - start}; + if(tmp.size != 0) c11_vector__push(c11_string, &retval, tmp); + return c11_vector__as_array(&retval); +} diff --git a/src/common/str.cpp b/src/common/str.cpp index 7f3124da..f9f14fab 100644 --- a/src/common/str.cpp +++ b/src/common/str.cpp @@ -23,37 +23,6 @@ Str::Str(pair detached) { assert(_ptr[size] == '\0'); } -vector Str::split(const Str& sep) const { - vector result; - std::string_view tmp; - int start = 0; - while(true) { - int i = index(sep, start); - if(i == -1) break; - tmp = sv().substr(start, i - start); - if(!tmp.empty()) result.push_back(tmp); - start = i + sep.size; - } - tmp = sv().substr(start, size - start); - if(!tmp.empty()) result.push_back(tmp); - return result; -} - -vector Str::split(char sep) const { - vector result; - const char* data = pkpy_Str__data(this); - int i = 0; - for(int j = 0; j < size; j++) { - if(data[j] == sep) { - if(j > i) result.emplace_back(data + i, j - i); - i = j + 1; - continue; - } - } - if(size > i) result.emplace_back(data + i, size - i); - return result; -} - static std::map& _interned() { static std::map interned; return interned; diff --git a/src/common/vector.c b/src/common/vector.c index 46d83ea5..92137c58 100644 --- a/src/common/vector.c +++ b/src/common/vector.c @@ -59,3 +59,12 @@ void c11_vector__reserve(c11_vector* self, int capacity){ self->capacity = capacity; self->data = realloc(self->data, self->elem_size * self->capacity); } + +c11_array c11_vector__as_array(c11_vector* self){ + c11_array retval = { + .data = self->data, + .count = self->count, + .elem_size = self->elem_size, + }; + return retval; +}