add startswith for c11_string

This commit is contained in:
方而静 2024-06-20 14:39:31 +08:00
parent dbd75615d9
commit 4c511e9431
2 changed files with 11 additions and 0 deletions

View File

@ -18,6 +18,9 @@ int c11_string__cmp(c11_string self, c11_string other);
int c11_string__cmp2(c11_string self, const char* other, int size); int c11_string__cmp2(c11_string self, const char* other, int size);
int c11_string__cmp3(c11_string self, const char* other); int c11_string__cmp3(c11_string self, const char* other);
bool c11_string_startswith(c11_string self, c11_string prefix);
bool c11_string_startswith_cstrn(c11_string self, const char* prefix, int size);
typedef struct pkpy_Str{ typedef struct pkpy_Str{
int size; int size;
bool is_ascii; bool is_ascii;

View File

@ -252,6 +252,14 @@ int c11_string__cmp3(c11_string self, const char *other){
return c11_string__cmp2(self, other, strlen(other)); return c11_string__cmp2(self, other, strlen(other));
} }
bool c11_string_startswith(c11_string self, c11_string prefix) {
return strncmp(self.data, prefix.data, PK_MIN(self.size, prefix.size)) == 0;
}
bool c11_string_startswith_cstrn(c11_string self, const char *prefix, int size) {
return strncmp(self.data, prefix, PK_MIN(self.size, size)) == 0;
}
int pkpy_Str__cmp(const pkpy_Str *self, const pkpy_Str *other){ int pkpy_Str__cmp(const pkpy_Str *self, const pkpy_Str *other){
return pkpy_Str__cmp2(self, pkpy_Str__data(other), other->size); return pkpy_Str__cmp2(self, pkpy_Str__data(other), other->size);
} }