Implement strncmp
This commit is contained in:
parent
fa86208a82
commit
a8aeb1a3c5
@ -12,6 +12,7 @@ char *strchr(const char *s, int c);
|
||||
int strcmp(const char *s1, const char *s2);
|
||||
char *strcpy(char *to, const char *from);
|
||||
size_t strlen(const char *str);
|
||||
int strncmp(const char *s1, const char *s2, size_t len);
|
||||
char *strncpy(char *to, const char *from, size_t len);
|
||||
|
||||
#endif /* __STRING_H__ */
|
||||
|
@ -53,6 +53,27 @@ strcmp(const char *s1, const char *s2)
|
||||
return (*(const uint8_t *)s1 - *(const uint8_t *)(s2 - 1));
|
||||
}
|
||||
|
||||
int
|
||||
strncmp(const char *s1, const char *s2, size_t len)
|
||||
{
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
while (*s1 == *s2) {
|
||||
if (*s1 == 0)
|
||||
return 0;
|
||||
|
||||
s1++;
|
||||
s2++;
|
||||
|
||||
len--;
|
||||
if (len == 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (*(const uint8_t *)s1 - *(const uint8_t *)s2);
|
||||
}
|
||||
|
||||
size_t
|
||||
strlen(const char *str)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user