Implement strncmp

This commit is contained in:
Ali Mashtizadeh 2014-10-14 14:07:55 -07:00
parent fa86208a82
commit a8aeb1a3c5
2 changed files with 22 additions and 0 deletions

View File

@ -12,6 +12,7 @@ char *strchr(const char *s, int c);
int strcmp(const char *s1, const char *s2); int strcmp(const char *s1, const char *s2);
char *strcpy(char *to, const char *from); char *strcpy(char *to, const char *from);
size_t strlen(const char *str); 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); char *strncpy(char *to, const char *from, size_t len);
#endif /* __STRING_H__ */ #endif /* __STRING_H__ */

View File

@ -53,6 +53,27 @@ strcmp(const char *s1, const char *s2)
return (*(const uint8_t *)s1 - *(const uint8_t *)(s2 - 1)); 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 size_t
strlen(const char *str) strlen(const char *str)
{ {