boot: Add strcpy8/16

This commit is contained in:
Jan Janssen
2022-05-31 15:10:48 +02:00
parent fc178cf1f8
commit ef4d71ad7f
3 changed files with 52 additions and 0 deletions
+23
View File
@@ -98,3 +98,26 @@ int strcasecmp8(const char *s1, const char *s2) {
int strcasecmp16(const char16_t *s1, const char16_t *s2) {
return strncasecmp16(s1, s2, SIZE_MAX);
}
#define DEFINE_STRCPY(type, name) \
type *name(type * restrict dest, const type * restrict src) { \
assert(dest); \
type *ret = dest; \
\
if (!src) { \
*dest = '\0'; \
return ret; \
} \
\
while (*src) { \
*dest = *src; \
dest++; \
src++; \
} \
\
*dest = '\0'; \
return ret; \
}
DEFINE_STRCPY(char, strcpy8);
DEFINE_STRCPY(char16_t, strcpy16);
+3
View File
@@ -57,3 +57,6 @@ static inline bool strcaseeq8(const char *s1, const char *s2) {
static inline bool strcaseeq16(const char16_t *s1, const char16_t *s2) {
return strcasecmp16(s1, s2) == 0;
}
char *strcpy8(char * restrict dest, const char * restrict src);
char16_t *strcpy16(char16_t * restrict dest, const char16_t * restrict src);
+26
View File
@@ -174,4 +174,30 @@ TEST(strncasecmp16) {
assert_se(strncasecmp16((char16_t[]){ UINT16_MAX }, (char16_t[]){ 0 }, 1) > 0);
}
TEST(strcpy8) {
char buf[128];
assert_se(strcpy8(buf, "123") == buf);
assert_se(streq8(buf, "123"));
assert_se(strcpy8(buf, "") == buf);
assert_se(streq8(buf, ""));
assert_se(strcpy8(buf, "A") == buf);
assert_se(streq8(buf, "A"));
assert_se(strcpy8(buf, NULL) == buf);
assert_se(streq8(buf, ""));
}
TEST(strcpy16) {
char16_t buf[128];
assert_se(strcpy16(buf, u"123") == buf);
assert_se(streq16(buf, u"123"));
assert_se(strcpy16(buf, u"") == buf);
assert_se(streq16(buf, u""));
assert_se(strcpy16(buf, u"A") == buf);
assert_se(streq16(buf, u"A"));
assert_se(strcpy16(buf, NULL) == buf);
assert_se(streq16(buf, u""));
}
DEFINE_TEST_MAIN(LOG_INFO);