diff options
Diffstat (limited to 'lume-legacy/src/lib/string.cc')
| -rw-r--r-- | lume-legacy/src/lib/string.cc | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/lume-legacy/src/lib/string.cc b/lume-legacy/src/lib/string.cc new file mode 100644 index 0000000..e8eccb3 --- /dev/null +++ b/lume-legacy/src/lib/string.cc @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2026 Richard QIn + */ +#include "lib/string.h" + +void *memset(void *dst, int c, uint n) +{ + char *cdst = (char *)dst; + for (uint i = 0; i < n; i++) + cdst[i] = c; + return dst; +} + +int memcmp(const void *v1, const void *v2, uint n) +{ + const uchar *s1 = (const uchar *)v1; + const uchar *s2 = (const uchar *)v2; + while (n-- > 0) + { + if (*s1 != *s2) + return *s1 - *s2; + s1++, s2++; + } + return 0; +} + +void *memmove(void *dst, const void *src, uint n) +{ + const char *s = (const char *)src; + char *d = (char *)dst; + if (s < d && s + n > d) + { + s += n; + d += n; + while (n-- > 0) + *--d = *--s; + } + else + { + while (n-- > 0) + *d++ = *s++; + } + return dst; +} + +void *memcpy(void *dst, const void *src, uint n) +{ + return memmove(dst, src, n); +} + +void strcpy(char *dst, const char *src) +{ + while (*src) + *dst++ = *src++; + *dst = 0; +} + +int strlen(const char *s) +{ + int n = 0; + while (s[n]) + n++; + return n; +} + +int strcmp(const char *s1, const char *s2) +{ + while (*s1 && (*s1 == *s2)) + { + s1++; + s2++; + } + return *(const unsigned char *)s1 - *(const unsigned char *)s2; +} + +int strcasecmp(const char *s1, const char *s2) +{ + while (*s1) + { + if(*s1 == *s2 || *s1 + 32 == *s2 || *s1 == *s2 + 32){ + s1++; + s2++; + } + else break; + } + return *(const unsigned char *)s1 - *(const unsigned char *)s2; +}
\ No newline at end of file |
