efi-string: Add startswith8()

startswith() from string-util-fundamental.h is defined for sd_char
which is char16_t, so let's add an implementation for char as well.
This commit is contained in:
Daan De Meyer
2023-02-22 16:55:07 +01:00
parent bbef5a9617
commit ad36d31ea5
3 changed files with 29 additions and 0 deletions
+15
View File
@@ -212,6 +212,21 @@ char16_t *xstrn8_to_16(const char *str8, size_t n) {
return str16;
}
char *startswith8(const char *s, const char *prefix) {
size_t l;
assert(prefix);
if (!s)
return NULL;
l = strlen8(prefix);
if (!strneq8(s, prefix, l))
return NULL;
return (char*) s + l;
}
static bool efi_fnmatch_prefix(const char16_t *p, const char16_t *h, const char16_t **ret_p, const char16_t **ret_h) {
assert(p);
assert(h);
+2
View File
@@ -101,6 +101,8 @@ static inline char16_t *xstr8_to_16(const char *str8) {
return xstrn8_to_16(str8, strlen8(str8));
}
char *startswith8(const char *s, const char *prefix);
bool efi_fnmatch(const char16_t *pattern, const char16_t *haystack);
bool parse_number8(const char *s, uint64_t *ret_u, const char **ret_tail);
+12
View File
@@ -355,6 +355,18 @@ TEST(xstrn8_to_16) {
free(s);
}
TEST(startswith8) {
assert_se(streq8(startswith8("", ""), ""));
assert_se(streq8(startswith8("x", ""), "x"));
assert_se(!startswith8("", "x"));
assert_se(!startswith8("", "xxxxxxxx"));
assert_se(streq8(startswith8("xxx", "x"), "xx"));
assert_se(streq8(startswith8("xxx", "xx"), "x"));
assert_se(streq8(startswith8("xxx", "xxx"), ""));
assert_se(!startswith8("xxx", "xxxx"));
assert_se(!startswith8(NULL, ""));
}
#define TEST_FNMATCH_ONE(pattern, haystack, expect) \
({ \
assert_se(fnmatch(pattern, haystack, 0) == (expect ? 0 : FNM_NOMATCH)); \