String: strncmp, strncpy
This commit is contained in:
parent
8316c9f6ae
commit
e9f2eef252
2 changed files with 21 additions and 3 deletions
|
@ -178,3 +178,20 @@ size_t strnlen(const char *s, size_t maxlen) {
|
|||
}
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -31,13 +31,14 @@ size_t strlen(const char *s);
|
|||
/** Return length of string `s`, up to a max of `maxlen` bytes */
|
||||
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:
|
||||
/*
|
||||
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 *strncpy(char *dst, const char *src, size_t n);
|
||||
|
||||
char *strchr(const char *s, int c);
|
||||
char *strrchr(const char *s, int c);
|
||||
|
|
Loading…
Add table
Reference in a new issue