|
|
@@ -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);
|
|
|
+}
|