|
|
@@ -7,11 +7,11 @@
|
|
|
#include <ctype.h>
|
|
|
#include <stdio.h>
|
|
|
|
|
|
-void pkpy_Str__ctor(pkpy_Str *self, const char *data){
|
|
|
- pkpy_Str__ctor2(self, data, strlen(data));
|
|
|
+void py_Str__ctor(py_Str *self, const char *data){
|
|
|
+ py_Str__ctor2(self, data, strlen(data));
|
|
|
}
|
|
|
|
|
|
-void pkpy_Str__ctor2(pkpy_Str *self, const char *data, int size){
|
|
|
+void py_Str__ctor2(py_Str *self, const char *data, int size){
|
|
|
self->size = size;
|
|
|
self->is_ascii = c11__isascii(data, size);
|
|
|
self->is_sso = size < sizeof(self->_inlined);
|
|
|
@@ -26,7 +26,7 @@ void pkpy_Str__ctor2(pkpy_Str *self, const char *data, int size){
|
|
|
p[size] = '\0';
|
|
|
}
|
|
|
|
|
|
-void pkpy_Str__dtor(pkpy_Str *self){
|
|
|
+void py_Str__dtor(py_Str *self){
|
|
|
if(!self->is_sso){
|
|
|
free(self->_ptr);
|
|
|
self->is_sso = true;
|
|
|
@@ -34,8 +34,8 @@ void pkpy_Str__dtor(pkpy_Str *self){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__copy(const pkpy_Str *self){
|
|
|
- pkpy_Str retval = *self;
|
|
|
+py_Str py_Str__copy(const py_Str *self){
|
|
|
+ py_Str retval = *self;
|
|
|
if(!self->is_sso){
|
|
|
retval._ptr = (char*)malloc(self->size + 1);
|
|
|
// '\0' is copied
|
|
|
@@ -44,8 +44,8 @@ pkpy_Str pkpy_Str__copy(const pkpy_Str *self){
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__concat(const pkpy_Str *self, const pkpy_Str *other){
|
|
|
- pkpy_Str retval = {
|
|
|
+py_Str py_Str__concat(const py_Str *self, const py_Str *other){
|
|
|
+ py_Str retval = {
|
|
|
.size = self->size + other->size,
|
|
|
.is_ascii = self->is_ascii && other->is_ascii,
|
|
|
.is_sso = self->size + other->size < sizeof(retval._inlined),
|
|
|
@@ -57,56 +57,56 @@ pkpy_Str pkpy_Str__concat(const pkpy_Str *self, const pkpy_Str *other){
|
|
|
retval._ptr = (char*)malloc(retval.size + 1);
|
|
|
p = retval._ptr;
|
|
|
}
|
|
|
- memcpy(p, pkpy_Str__data(self), self->size);
|
|
|
- memcpy(p + self->size, pkpy_Str__data(other), other->size);
|
|
|
+ memcpy(p, py_Str__data(self), self->size);
|
|
|
+ memcpy(p + self->size, py_Str__data(other), other->size);
|
|
|
p[retval.size] = '\0';
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__concat2(const pkpy_Str *self, const char *other, int size){
|
|
|
- pkpy_Str tmp;
|
|
|
- pkpy_Str__ctor2(&tmp, other, size);
|
|
|
- pkpy_Str retval = pkpy_Str__concat(self, &tmp);
|
|
|
- pkpy_Str__dtor(&tmp);
|
|
|
+py_Str py_Str__concat2(const py_Str *self, const char *other, int size){
|
|
|
+ py_Str tmp;
|
|
|
+ py_Str__ctor2(&tmp, other, size);
|
|
|
+ py_Str retval = py_Str__concat(self, &tmp);
|
|
|
+ py_Str__dtor(&tmp);
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__slice(const pkpy_Str *self, int start){
|
|
|
- return pkpy_Str__slice2(self, start, self->size);
|
|
|
+py_Str py_Str__slice(const py_Str *self, int start){
|
|
|
+ return py_Str__slice2(self, start, self->size);
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__slice2(const pkpy_Str *self, int start, int stop){
|
|
|
- pkpy_Str retval;
|
|
|
+py_Str py_Str__slice2(const py_Str *self, int start, int stop){
|
|
|
+ py_Str retval;
|
|
|
if(stop < start) stop = start;
|
|
|
- pkpy_Str__ctor2(&retval, pkpy_Str__data(self) + start, stop - start);
|
|
|
+ py_Str__ctor2(&retval, py_Str__data(self) + start, stop - start);
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__lower(const pkpy_Str *self){
|
|
|
- pkpy_Str retval = pkpy_Str__copy(self);
|
|
|
- char* p = (char*)pkpy_Str__data(&retval);
|
|
|
+py_Str py_Str__lower(const py_Str *self){
|
|
|
+ py_Str retval = py_Str__copy(self);
|
|
|
+ char* p = (char*)py_Str__data(&retval);
|
|
|
for(int i = 0; i < retval.size; i++){
|
|
|
if('A' <= p[i] && p[i] <= 'Z') p[i] += 32;
|
|
|
}
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__upper(const pkpy_Str *self){
|
|
|
- pkpy_Str retval = pkpy_Str__copy(self);
|
|
|
- char* p = (char*)pkpy_Str__data(&retval);
|
|
|
+py_Str py_Str__upper(const py_Str *self){
|
|
|
+ py_Str retval = py_Str__copy(self);
|
|
|
+ char* p = (char*)py_Str__data(&retval);
|
|
|
for(int i = 0; i < retval.size; i++){
|
|
|
if('a' <= p[i] && p[i] <= 'z') p[i] -= 32;
|
|
|
}
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__escape(const pkpy_Str* self, char quote){
|
|
|
+py_Str py_Str__escape(const py_Str* self, char quote){
|
|
|
assert(quote == '"' || quote == '\'');
|
|
|
c11_vector buffer;
|
|
|
c11_vector__ctor(&buffer, sizeof(char));
|
|
|
c11_vector__reserve(&buffer, self->size);
|
|
|
c11_vector__push(char, &buffer, quote);
|
|
|
- const char* data = pkpy_Str__data(self);
|
|
|
+ const char* data = py_Str__data(self);
|
|
|
for(int i = 0; i < self->size; i++) {
|
|
|
char c = data[i];
|
|
|
switch(c) {
|
|
|
@@ -147,7 +147,7 @@ pkpy_Str pkpy_Str__escape(const pkpy_Str* self, char quote){
|
|
|
}
|
|
|
c11_vector__push(char, &buffer, quote);
|
|
|
c11_vector__push(char, &buffer, '\0');
|
|
|
- pkpy_Str retval = {
|
|
|
+ py_Str retval = {
|
|
|
.size = buffer.count - 1,
|
|
|
.is_ascii = self->is_ascii,
|
|
|
.is_sso = false,
|
|
|
@@ -156,8 +156,8 @@ 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);
|
|
|
+py_Str py_Str__strip(const py_Str *self, bool left, bool right){
|
|
|
+ const char* data = py_Str__data(self);
|
|
|
if(self->is_ascii) {
|
|
|
int L = 0;
|
|
|
int R = self->size;
|
|
|
@@ -169,67 +169,67 @@ pkpy_Str pkpy_Str__strip(const pkpy_Str *self, bool left, bool 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);
|
|
|
+ return py_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);
|
|
|
+ py_Str tmp;
|
|
|
+ py_Str__ctor(&tmp, " \t\n\r");
|
|
|
+ py_Str retval = py_Str__strip2(self, left, right, &tmp);
|
|
|
+ py_Str__dtor(&tmp);
|
|
|
return retval;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__strip2(const pkpy_Str *self, bool left, bool right, const pkpy_Str *chars){
|
|
|
+py_Str py_Str__strip2(const py_Str *self, bool left, bool right, const py_Str *chars){
|
|
|
int L = 0;
|
|
|
- int R = pkpy_Str__u8_length(self);
|
|
|
+ int R = py_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);
|
|
|
+ py_Str tmp = py_Str__u8_getitem(self, L);
|
|
|
+ bool found = py_Str__index(chars, &tmp, 0) != -1;
|
|
|
+ py_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);
|
|
|
+ py_Str tmp = py_Str__u8_getitem(self, R - 1);
|
|
|
+ bool found = py_Str__index(chars, &tmp, 0) != -1;
|
|
|
+ py_Str__dtor(&tmp);
|
|
|
if(!found) break;
|
|
|
R--;
|
|
|
}
|
|
|
}
|
|
|
- return pkpy_Str__u8_slice(self, L, R, 1);
|
|
|
+ return py_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);
|
|
|
- char* p = (char*)pkpy_Str__data(&retval);
|
|
|
+py_Str py_Str__replace(const py_Str *self, char old, char new_){
|
|
|
+ py_Str retval = py_Str__copy(self);
|
|
|
+ char* p = (char*)py_Str__data(&retval);
|
|
|
for(int i = 0; i < retval.size; i++){
|
|
|
if(p[i] == old) p[i] = new_;
|
|
|
}
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__replace2(const pkpy_Str *self, const pkpy_Str *old, const pkpy_Str *new_){
|
|
|
+py_Str py_Str__replace2(const py_Str *self, const py_Str *old, const py_Str *new_){
|
|
|
c11_vector buffer;
|
|
|
c11_vector__ctor(&buffer, sizeof(char));
|
|
|
int start = 0;
|
|
|
while(true) {
|
|
|
- int i = pkpy_Str__index(self, old, start);
|
|
|
+ int i = py_Str__index(self, old, start);
|
|
|
if(i == -1) break;
|
|
|
- pkpy_Str tmp = pkpy_Str__slice2(self, start, i);
|
|
|
- c11_vector__extend(char, &buffer, pkpy_Str__data(&tmp), tmp.size);
|
|
|
- pkpy_Str__dtor(&tmp);
|
|
|
- c11_vector__extend(char, &buffer, pkpy_Str__data(new_), new_->size);
|
|
|
+ py_Str tmp = py_Str__slice2(self, start, i);
|
|
|
+ c11_vector__extend(char, &buffer, py_Str__data(&tmp), tmp.size);
|
|
|
+ py_Str__dtor(&tmp);
|
|
|
+ c11_vector__extend(char, &buffer, py_Str__data(new_), new_->size);
|
|
|
start = i + old->size;
|
|
|
}
|
|
|
- pkpy_Str tmp = pkpy_Str__slice2(self, start, self->size);
|
|
|
- c11_vector__extend(char, &buffer, pkpy_Str__data(&tmp), tmp.size);
|
|
|
- pkpy_Str__dtor(&tmp);
|
|
|
+ py_Str tmp = py_Str__slice2(self, start, self->size);
|
|
|
+ c11_vector__extend(char, &buffer, py_Str__data(&tmp), tmp.size);
|
|
|
+ py_Str__dtor(&tmp);
|
|
|
c11_vector__push(char, &buffer, '\0');
|
|
|
- pkpy_Str retval = {
|
|
|
+ py_Str retval = {
|
|
|
.size = buffer.count - 1,
|
|
|
.is_ascii = self->is_ascii && old->is_ascii && new_->is_ascii,
|
|
|
.is_sso = false,
|
|
|
@@ -252,47 +252,47 @@ int c11_string__cmp3(c11_string self, const char *other){
|
|
|
return c11_string__cmp2(self, other, strlen(other));
|
|
|
}
|
|
|
|
|
|
-int pkpy_Str__cmp(const pkpy_Str *self, const pkpy_Str *other){
|
|
|
- return pkpy_Str__cmp2(self, pkpy_Str__data(other), other->size);
|
|
|
+int py_Str__cmp(const py_Str *self, const py_Str *other){
|
|
|
+ return py_Str__cmp2(self, py_Str__data(other), other->size);
|
|
|
}
|
|
|
|
|
|
-int pkpy_Str__cmp2(const pkpy_Str *self, const char *other, int size){
|
|
|
- int res = strncmp(pkpy_Str__data(self), other, PK_MIN(self->size, size));
|
|
|
+int py_Str__cmp2(const py_Str *self, const char *other, int size){
|
|
|
+ int res = strncmp(py_Str__data(self), other, PK_MIN(self->size, size));
|
|
|
if(res != 0) return res;
|
|
|
return self->size - size;
|
|
|
}
|
|
|
|
|
|
-int pkpy_Str__cmp3(const pkpy_Str *self, const char *other){
|
|
|
- return strcmp(pkpy_Str__data(self), other);
|
|
|
+int py_Str__cmp3(const py_Str *self, const char *other){
|
|
|
+ return strcmp(py_Str__data(self), other);
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__u8_getitem(const pkpy_Str *self, int i){
|
|
|
- i = pkpy_Str__unicode_index_to_byte(self, i);
|
|
|
- int size = c11__u8_header(pkpy_Str__data(self)[i], false);
|
|
|
- return pkpy_Str__slice2(self, i, i + size);
|
|
|
+py_Str py_Str__u8_getitem(const py_Str *self, int i){
|
|
|
+ i = py_Str__unicode_index_to_byte(self, i);
|
|
|
+ int size = c11__u8_header(py_Str__data(self)[i], false);
|
|
|
+ return py_Str__slice2(self, i, i + size);
|
|
|
}
|
|
|
|
|
|
-pkpy_Str pkpy_Str__u8_slice(const pkpy_Str *self, int start, int stop, int step){
|
|
|
+py_Str py_Str__u8_slice(const py_Str *self, int start, int stop, int step){
|
|
|
c11_vector buffer;
|
|
|
c11_vector__ctor(&buffer, sizeof(char));
|
|
|
assert(step != 0);
|
|
|
if(self->is_ascii){
|
|
|
- const char* p = pkpy_Str__data(self);
|
|
|
+ const char* p = py_Str__data(self);
|
|
|
for (int i=start; step>0 ? i<stop : i>stop; i+=step) {
|
|
|
c11_vector__push(char, &buffer, p[i]);
|
|
|
}
|
|
|
}else{
|
|
|
for (int i=start; step>0 ? i<stop : i>stop; i+=step) {
|
|
|
- pkpy_Str unicode = pkpy_Str__u8_getitem(self, i);
|
|
|
- const char* p = pkpy_Str__data(&unicode);
|
|
|
+ py_Str unicode = py_Str__u8_getitem(self, i);
|
|
|
+ const char* p = py_Str__data(&unicode);
|
|
|
for(int j = 0; j < unicode.size; j++){
|
|
|
c11_vector__push(char, &buffer, p[j]);
|
|
|
}
|
|
|
- pkpy_Str__dtor(&unicode);
|
|
|
+ py_Str__dtor(&unicode);
|
|
|
}
|
|
|
}
|
|
|
c11_vector__push(char, &buffer, '\0');
|
|
|
- pkpy_Str retval = {
|
|
|
+ py_Str retval = {
|
|
|
.size = buffer.count - 1,
|
|
|
.is_ascii = self->is_ascii,
|
|
|
.is_sso = false,
|
|
|
@@ -301,13 +301,13 @@ pkpy_Str pkpy_Str__u8_slice(const pkpy_Str *self, int start, int stop, int step)
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-int pkpy_Str__u8_length(const pkpy_Str *self){
|
|
|
- return pkpy_Str__byte_index_to_unicode(self, self->size);
|
|
|
+int py_Str__u8_length(const py_Str *self){
|
|
|
+ return py_Str__byte_index_to_unicode(self, self->size);
|
|
|
}
|
|
|
|
|
|
-int pkpy_Str__unicode_index_to_byte(const pkpy_Str* self, int i) {
|
|
|
+int py_Str__unicode_index_to_byte(const py_Str* self, int i) {
|
|
|
if(self->is_ascii) return i;
|
|
|
- const char* p = pkpy_Str__data(self);
|
|
|
+ const char* p = py_Str__data(self);
|
|
|
int j = 0;
|
|
|
while(i > 0) {
|
|
|
j += c11__u8_header(p[j], false);
|
|
|
@@ -316,9 +316,9 @@ int pkpy_Str__unicode_index_to_byte(const pkpy_Str* self, int i) {
|
|
|
return j;
|
|
|
}
|
|
|
|
|
|
-int pkpy_Str__byte_index_to_unicode(const pkpy_Str* self, int n) {
|
|
|
+int py_Str__byte_index_to_unicode(const py_Str* self, int n) {
|
|
|
if(self->is_ascii) return n;
|
|
|
- const char* p = pkpy_Str__data(self);
|
|
|
+ const char* p = py_Str__data(self);
|
|
|
int cnt = 0;
|
|
|
for(int i = 0; i < n; i++) {
|
|
|
if((p[i] & 0xC0) != 0x80) cnt++;
|
|
|
@@ -326,11 +326,11 @@ int pkpy_Str__byte_index_to_unicode(const pkpy_Str* self, int n) {
|
|
|
return cnt;
|
|
|
}
|
|
|
|
|
|
-int pkpy_Str__index(const pkpy_Str *self, const pkpy_Str *sub, int start){
|
|
|
+int py_Str__index(const py_Str *self, const py_Str *sub, int start){
|
|
|
if(sub->size == 0) return start;
|
|
|
int max_end = self->size - sub->size;
|
|
|
- const char* self_data = pkpy_Str__data(self);
|
|
|
- const char* sub_data = pkpy_Str__data(sub);
|
|
|
+ const char* self_data = py_Str__data(self);
|
|
|
+ const char* sub_data = py_Str__data(sub);
|
|
|
for(int i=start; i<=max_end; i++){
|
|
|
int res = memcmp(self_data + i, sub_data, sub->size);
|
|
|
if(res == 0) return i;
|
|
|
@@ -338,12 +338,12 @@ int pkpy_Str__index(const pkpy_Str *self, const pkpy_Str *sub, int start){
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
-int pkpy_Str__count(const pkpy_Str *self, const pkpy_Str *sub){
|
|
|
+int py_Str__count(const py_Str *self, const py_Str *sub){
|
|
|
if(sub->size == 0) return self->size + 1;
|
|
|
int cnt = 0;
|
|
|
int start = 0;
|
|
|
while(true) {
|
|
|
- int i = pkpy_Str__index(self, sub, start);
|
|
|
+ int i = py_Str__index(self, sub, start);
|
|
|
if(i == -1) break;
|
|
|
cnt++;
|
|
|
start = i + sub->size;
|
|
|
@@ -351,10 +351,10 @@ int pkpy_Str__count(const pkpy_Str *self, const pkpy_Str *sub){
|
|
|
return cnt;
|
|
|
}
|
|
|
|
|
|
-c11_vector/* T=c11_string */ pkpy_Str__split(const pkpy_Str *self, char sep){
|
|
|
+c11_vector/* T=c11_string */ py_Str__split(const py_Str *self, char sep){
|
|
|
c11_vector retval;
|
|
|
c11_vector__ctor(&retval, sizeof(c11_string));
|
|
|
- const char* data = pkpy_Str__data(self);
|
|
|
+ const char* data = py_Str__data(self);
|
|
|
int i = 0;
|
|
|
for(int j = 0; j < self->size; j++) {
|
|
|
if(data[j] == sep) {
|
|
|
@@ -373,13 +373,13 @@ c11_vector/* T=c11_string */ pkpy_Str__split(const pkpy_Str *self, char sep){
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-c11_vector/* T=c11_string */ pkpy_Str__split2(const pkpy_Str *self, const pkpy_Str *sep){
|
|
|
+c11_vector/* T=c11_string */ py_Str__split2(const py_Str *self, const py_Str *sep){
|
|
|
c11_vector retval;
|
|
|
c11_vector__ctor(&retval, sizeof(c11_string));
|
|
|
int start = 0;
|
|
|
- const char* data = pkpy_Str__data(self);
|
|
|
+ const char* data = py_Str__data(self);
|
|
|
while(true) {
|
|
|
- int i = pkpy_Str__index(self, sep, start);
|
|
|
+ int i = py_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);
|