fxlibc/src/libc/string/strlen.c

38 lines
814 B
C
Raw Normal View History

2021-05-09 17:34:00 +02:00
#include <string.h>
2020-09-17 19:27:01 +02:00
/*
** The strlen() function calculates the length of the string pointed to by s,
** excluding the terminating null byte ('\0').
**
** TODO: use quad-word access !
*/
2020-09-17 19:27:01 +02:00
size_t strlen(char const *str)
{
size_t i;
if (str == NULL)
return (0);
i = -1;
while (str[++i] != '\0') ;
2020-09-17 19:27:01 +02:00
return (i);
}
/*
** The strnlen() function returns the number of bytes in the string pointed to
** by s, excluding the terminating null byte ('\0'), but at most maxlen.
** In doing this, strnlen() looks only at the first maxlen characters in the
** string pointed to by s and never beyond s+maxlen.
**
** TODO: use quad-word access !
*/
2020-09-17 19:27:01 +02:00
size_t strnlen(char const *str, size_t maxlen)
{
size_t i;
if (str == NULL)
return (0);
i = -1;
while (str[++i] != '\0' && (size_t)i < maxlen) ;
2020-09-17 19:27:01 +02:00
return (i);
}