From 4c511e943131bf1035f7d5e405c5ba798cda73a8 Mon Sep 17 00:00:00 2001 From: szdytom Date: Thu, 20 Jun 2024 14:39:31 +0800 Subject: [PATCH] add startswith for c11_string --- include/pocketpy/common/str.h | 3 +++ src/common/str.c | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/include/pocketpy/common/str.h b/include/pocketpy/common/str.h index ac75d29c..c4cc2ff7 100644 --- a/include/pocketpy/common/str.h +++ b/include/pocketpy/common/str.h @@ -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__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{ int size; bool is_ascii; diff --git a/src/common/str.c b/src/common/str.c index ff6fec7d..4a59edd9 100644 --- a/src/common/str.c +++ b/src/common/str.c @@ -252,6 +252,14 @@ int c11_string__cmp3(c11_string self, const char *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){ return pkpy_Str__cmp2(self, pkpy_Str__data(other), other->size); }