String: strncmp, strncpy

This commit is contained in:
Imbus 2025-09-08 00:45:41 +02:00
parent 8316c9f6ae
commit e9f2eef252
2 changed files with 21 additions and 3 deletions

View file

@ -178,3 +178,20 @@ size_t strnlen(const char *s, size_t maxlen) {
} }
return len; return len;
} }
int strncmp(const char *p, const char *q, u32 n) {
while (n > 0 && *p && *p == *q) n--, p++, q++;
if (n == 0)
return 0;
return (u8)*p - (u8)*q;
}
char *strncpy(char *s, const char *t, int n) {
char *os;
os = s;
while (n-- > 0 && (*s++ = *t++) != 0) {
}
while (n-- > 0) *s++ = 0;
return os;
}

View file

@ -31,13 +31,14 @@ size_t strlen(const char *s);
/** Return length of string `s`, up to a max of `maxlen` bytes */ /** Return length of string `s`, up to a max of `maxlen` bytes */
size_t strnlen(const char *s, size_t maxlen); size_t strnlen(const char *s, size_t maxlen);
int strncmp(const char *p, const char *q, u32 n);
char *strncpy(char *s, const char *t, int n);
// TODO: These: // TODO: These:
/* /*
int strcmp(const char *s1, const char *s2); int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *strcpy(char *dst, const char *src); char *strcpy(char *dst, const char *src);
char *strncpy(char *dst, const char *src, size_t n);
char *strchr(const char *s, int c); char *strchr(const char *s, int c);
char *strrchr(const char *s, int c); char *strrchr(const char *s, int c);