[SDK:CRT] Implement more string routines

This commit is contained in:
2024-08-08 16:41:45 -04:00
parent 120277161c
commit b57dcc8078
4 changed files with 176 additions and 0 deletions

View File

@@ -43,3 +43,87 @@ strnlen (
return len - sizeof(char);
}
int
strcmp (
const char* s1,
const char* s2
)
{
while (*s1 == *s2) {
if (*s1 == '\0') {
return 0;
}
s1++;
s2++;
}
return *(unsigned char *)s1 - *(unsigned char*)s2;
}
int
strncmp (
const char* s1,
const char* s2,
size_t n
)
{
while (n > 0) {
if (*s1 != *s2) {
return *(unsigned char *)s1 - *(unsigned char*)s2;
}
if (*s1 == '\0') {
return 0;
}
n--;
s1++;
s2++;
}
return 0;
}
char *
strchr (
const char *s,
int c
)
{
while (*s != (char)c) {
if (!*s) {
return NULL;
}
s++;
}
return (char *)s;
}
char *
strstr (
const char *haystack,
const char *needle
)
{
const char *ptr = haystack;
if (!*needle) {
return (char *)haystack;
}
while ((ptr = strchr(ptr, *needle)) != NULL) {
if (strcmp(ptr, needle) == 0) {
return (char *)ptr;
}
}
return NULL;
}