Util: Add startswith

This commit is contained in:
Vicki Pfau 2017-03-05 17:22:24 -08:00
parent 000f232c58
commit 726986e447
2 changed files with 10 additions and 0 deletions

View File

@ -21,6 +21,7 @@ char* strdup(const char* str);
char* strnrstr(const char* restrict s1, const char* restrict s2, size_t len); char* strnrstr(const char* restrict s1, const char* restrict s2, size_t len);
bool endswith(const char* restrict s1, const char* restrict end); bool endswith(const char* restrict s1, const char* restrict end);
bool startswith(const char* restrict s1, const char* restrict start);
size_t toUtf8(uint32_t unichar, char* buffer); size_t toUtf8(uint32_t unichar, char* buffer);
int utfcmp(const uint16_t* utf16, const char* utf8, size_t utf16Length, size_t utf8Length); int utfcmp(const uint16_t* utf16, const char* utf8, size_t utf16Length, size_t utf8Length);

View File

@ -48,6 +48,15 @@ bool endswith(const char* restrict s1, const char* restrict end) {
return strcmp(&s1[len - endLen], end) == 0; return strcmp(&s1[len - endLen], end) == 0;
} }
bool startswith(const char* restrict s1, const char* restrict start) {
size_t len = strlen(s1);
size_t startLen = strlen(start);
if (len < startLen) {
return false;
}
return strncmp(s1, start, startLen) == 0;
}
uint32_t utf16Char(const uint16_t** unicode, size_t* length) { uint32_t utf16Char(const uint16_t** unicode, size_t* length) {
if (*length < 2) { if (*length < 2) {
*length = 0; *length = 0;