libc: add strnlen implementation

This is standard function and useful for application writers.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
This commit is contained in:
Timo Teräs 2018-09-26 13:02:41 +03:00 committed by Andrew Boie
parent c69081d775
commit 55dc481a15
2 changed files with 20 additions and 0 deletions

View File

@ -22,6 +22,7 @@ extern char *strncpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s,
extern char *strchr(const char *s, int c);
extern char *strrchr(const char *s, int c);
extern size_t strlen(const char *s);
extern size_t strnlen(const char *s, size_t maxlen);
extern int strcmp(const char *s1, const char *s2);
extern int strncmp(const char *s1, const char *s2, size_t n);
extern char *strcat(char *_MLIBC_RESTRICT dest,

View File

@ -116,6 +116,25 @@ size_t strlen(const char *s)
return n;
}
/**
*
* @brief Get fixed-size string length
*
* @return number of bytes in fixed-size string <s>
*/
size_t strnlen(const char *s, size_t maxlen)
{
size_t n = 0;
while (*s != '\0' && n < maxlen) {
s++;
n++;
}
return n;
}
/**
*
* @brief Compare two strings