move strip

This commit is contained in:
blueloveTH 2024-06-11 12:52:22 +08:00
parent 07e07831c3
commit 6a70f535b3
4 changed files with 54 additions and 74 deletions

View File

@ -36,8 +36,8 @@ pkpy_Str pkpy_Str__slice2(const pkpy_Str* self, int start, int stop);
pkpy_Str pkpy_Str__lower(const pkpy_Str* self);
pkpy_Str pkpy_Str__upper(const pkpy_Str* self);
pkpy_Str pkpy_Str__escape(const pkpy_Str* self, char quote);
// pkpy_Str pkpy_Str__strip(const pkpy_Str* self, bool left, bool right);
// pkpy_Str pkpy_Str__strip2(const pkpy_Str* self, bool left, bool right, const pkpy_Str* chars);
pkpy_Str pkpy_Str__strip(const pkpy_Str* self, bool left, bool right);
pkpy_Str pkpy_Str__strip2(const pkpy_Str* self, bool left, bool right, const pkpy_Str* chars);
pkpy_Str pkpy_Str__replace(const pkpy_Str* self, char old, char new_);
pkpy_Str pkpy_Str__replace2(const pkpy_Str* self, const pkpy_Str* old, const pkpy_Str* new_);
pkpy_Str pkpy_Str__u8_getitem(const pkpy_Str* self, int i);

View File

@ -161,8 +161,13 @@ struct Str: pkpy_Str {
return pkpy_Str__slice(this, start);
}
Str strip(bool left, bool right, const Str& chars) const;
Str strip(bool left = true, bool right = true) const;
Str strip(bool left, bool right, const Str& chars) const{
return pkpy_Str__strip2(this, left, right, &chars);
}
Str strip(bool left = true, bool right = true) const{
return pkpy_Str__strip(this, left, right);
}
Str lstrip() const { return strip(true, false); }

View File

@ -175,44 +175,52 @@ pkpy_Str pkpy_Str__escape(const pkpy_Str* self, char quote){
return retval;
}
// pkpy_Str pkpy_Str__strip(const pkpy_Str *self, bool left, bool right){
// const char* data = pkpy_Str__data(self);
// if(self->is_ascii) {
// int L = 0;
// int R = self->size;
// if(left) {
// while(L < R && (data[L] == ' ' || data[L] == '\t' || data[L] == '\n' || data[L] == '\r'))
// L++;
// }
// if(right) {
// while(L < R && (data[R - 1] == ' ' || data[R - 1] == '\t' || data[R - 1] == '\n' || data[R - 1] == '\r'))
// R--;
// }
// return pkpy_Str__substr2(self, L, R - L);
// } else {
// pkpy_Str tmp;
// pkpy_Str__ctor(&tmp, " \t\n\r");
// pkpy_Str retval = pkpy_Str__strip2(self, left, right, &tmp);
// pkpy_Str__dtor(&tmp);
// return retval;
// }
// }
pkpy_Str pkpy_Str__strip(const pkpy_Str *self, bool left, bool right){
const char* data = pkpy_Str__data(self);
if(self->is_ascii) {
int L = 0;
int R = self->size;
if(left) {
while(L < R && (data[L] == ' ' || data[L] == '\t' || data[L] == '\n' || data[L] == '\r'))
L++;
}
if(right) {
while(L < R && (data[R - 1] == ' ' || data[R - 1] == '\t' || data[R - 1] == '\n' || data[R - 1] == '\r'))
R--;
}
return pkpy_Str__slice2(self, L, R);
} else {
pkpy_Str tmp;
pkpy_Str__ctor(&tmp, " \t\n\r");
pkpy_Str retval = pkpy_Str__strip2(self, left, right, &tmp);
pkpy_Str__dtor(&tmp);
return retval;
}
}
// pkpy_Str pkpy_Str__strip2(const pkpy_Str *self, bool left, bool right, const pkpy_Str *chars){
// int L = 0;
// int R = pkpy_Str__u8_length(self);
// pkpy_Str tmp;
// if(left) {
// tmp = pkpy_Str__u8_getitem(self, L);
// while(L < R && chars.index(u8_getitem(L)) != -1)
// L++;
// }
// if(right) {
// while(L < R && chars.index(u8_getitem(R - 1)) != -1)
// R--;
// }
// return pkpy_Str__u8_slice(self, L, R, 1);
// }
pkpy_Str pkpy_Str__strip2(const pkpy_Str *self, bool left, bool right, const pkpy_Str *chars){
int L = 0;
int R = pkpy_Str__u8_length(self);
if(left) {
while(L < R){
pkpy_Str tmp = pkpy_Str__u8_getitem(self, L);
bool found = pkpy_Str__index(chars, &tmp, 0) != -1;
pkpy_Str__dtor(&tmp);
if(!found) break;
L++;
}
}
if(right) {
while(L < R){
pkpy_Str tmp = pkpy_Str__u8_getitem(self, R - 1);
bool found = pkpy_Str__index(chars, &tmp, 0) != -1;
pkpy_Str__dtor(&tmp);
if(!found) break;
R--;
}
}
return pkpy_Str__u8_slice(self, L, R, 1);
}
pkpy_Str pkpy_Str__replace(const pkpy_Str *self, char old, char new_){
pkpy_Str retval = pkpy_Str__copy(self);

View File

@ -23,39 +23,6 @@ Str::Str(pair<char*, int> detached) {
assert(_ptr[size] == '\0');
}
Str Str::strip(bool left, bool right, const Str& chars) const {
int L = 0;
int R = u8_length();
if(left) {
while(L < R && chars.index(u8_getitem(L)) != -1)
L++;
}
if(right) {
while(L < R && chars.index(u8_getitem(R - 1)) != -1)
R--;
}
return u8_slice(L, R, 1);
}
Str Str::strip(bool left, bool right) const {
const char* data = pkpy_Str__data(this);
if(is_ascii) {
int L = 0;
int R = size;
if(left) {
while(L < R && (data[L] == ' ' || data[L] == '\t' || data[L] == '\n' || data[L] == '\r'))
L++;
}
if(right) {
while(L < R && (data[R - 1] == ' ' || data[R - 1] == '\t' || data[R - 1] == '\n' || data[R - 1] == '\r'))
R--;
}
return slice(L, R);
} else {
return strip(left, right, " \t\n\r");
}
}
vector<std::string_view> Str::split(const Str& sep) const {
vector<std::string_view> result;
std::string_view tmp;