blueloveTH преди 1 година
родител
ревизия
874d3a0b88
променени са 6 файла, в които са добавени 79 реда и са изтрити 34 реда
  1. 9 0
      include/pocketpy/common/str.h
  2. 22 2
      include/pocketpy/common/str.hpp
  3. 1 0
      include/pocketpy/common/vector.h
  4. 38 1
      src/common/str.c
  5. 0 31
      src/common/str.cpp
  6. 9 0
      src/common/vector.c

+ 9 - 0
include/pocketpy/common/str.h

@@ -5,6 +5,13 @@ extern "C" {
 #endif
 
 #include <stdbool.h>
+#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
 }

+ 22 - 2
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<std::string_view> split(const Str& sep) const;
-    vector<std::string_view> split(char sep) const;
+    vector<std::string_view> split(const Str& sep) const{
+        c11_array/* T=c11_string */ res = pkpy_Str__split2(this, &sep);
+        vector<std::string_view> 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<std::string_view> split(char sep) const{
+        c11_array/* T=c11_string */ res = pkpy_Str__split(this, sep);
+        vector<std::string_view> 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);

+ 1 - 0
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;

+ 38 - 1
src/common/str.c

@@ -1,5 +1,4 @@
 #include "pocketpy/common/str.h"
-#include "pocketpy/common/vector.h"
 #include "pocketpy/common/utils.h"
 
 #include <assert.h>
@@ -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);
+}

+ 0 - 31
src/common/str.cpp

@@ -23,37 +23,6 @@ Str::Str(pair<char*, int> detached) {
     assert(_ptr[size] == '\0');
 }
 
-vector<std::string_view> Str::split(const Str& sep) const {
-    vector<std::string_view> 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<std::string_view> Str::split(char sep) const {
-    vector<std::string_view> 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<std::string_view, uint16_t>& _interned() {
     static std::map<std::string_view, uint16_t> interned;
     return interned;

+ 9 - 0
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;
+}