blueloveTH 2025-07-03 18:04:36 +08:00
parent b6cefdeedc
commit d5a511ad7c
4 changed files with 26 additions and 1 deletions

View File

@ -67,6 +67,7 @@ c11_string* c11_sv__replace2(c11_sv self, c11_sv old, c11_sv new_);
c11_vector /* T=c11_sv */ c11_sv__split(c11_sv self, char sep);
c11_vector /* T=c11_sv */ c11_sv__split2(c11_sv self, c11_sv sep);
c11_vector /* T=c11_sv */ c11_sv__splitwhitespace(c11_sv self);
// misc
int c11__unicode_index_to_byte(const char* data, int i);

View File

@ -2,6 +2,7 @@
#include "pocketpy/common/sstream.h"
#include "pocketpy/common/utils.h"
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
@ -188,6 +189,26 @@ uint64_t c11_sv__hash(c11_sv self) {
return hash;
}
c11_vector /* T=c11_sv */ c11_sv__splitwhitespace(c11_sv self) {
c11_vector retval;
c11_vector__ctor(&retval, sizeof(c11_sv));
const char* data = self.data;
int i = 0;
for(int j = 0; j < self.size; j++) {
if(isspace(data[j])) {
assert(j >= i);
c11_sv tmp = {data + i, j - i};
c11_vector__push(c11_sv, &retval, tmp);
i = j + 1;
}
}
if(i <= self.size) {
c11_sv tmp = {data + i, self.size - i};
c11_vector__push(c11_sv, &retval, tmp);
}
return retval;
}
c11_vector /* T=c11_sv */ c11_sv__split(c11_sv self, char sep) {
c11_vector retval;
c11_vector__ctor(&retval, sizeof(c11_sv));

View File

@ -327,7 +327,7 @@ static bool str_split(int argc, py_Ref argv) {
if(argc > 2) return TypeError("split() takes at most 2 arguments");
if(argc == 1) {
// sep = None
res = c11_sv__split(self, ' ');
res = c11_sv__splitwhitespace(self);
discard_empty = true;
}
if(argc == 2) {

View File

@ -57,6 +57,9 @@ assert 'aa bb cccc'.split('cc') == ['aa bb ', '', '']
assert '.a.b.'.split('.') == ['', 'a', 'b', '']
assert '.a...b.'.split('.') == ['', 'a', '', '', 'b', '']
# https://github.com/pocketpy/pocketpy/issues/378
assert "a b \n c\td".split() == ['a', 'b', 'c', 'd']
try:
'a'.split('')
exit(1)