2021-05-09 17:34:00 +02:00
|
|
|
|
#include <string.h>
|
2020-09-17 19:27:01 +02:00
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
|
/*
|
|
|
|
|
** The strlen() function calculates the length of the string pointed to by s,
|
|
|
|
|
** excluding the termi‐nating 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;
|
2020-10-21 22:26:47 +02:00
|
|
|
|
while (str[++i] != '\0') ;
|
2020-09-17 19:27:01 +02:00
|
|
|
|
return (i);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
|
/*
|
|
|
|
|
** 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;
|
2020-10-21 22:26:47 +02:00
|
|
|
|
while (str[++i] != '\0' && (size_t)i < maxlen) ;
|
2020-09-17 19:27:01 +02:00
|
|
|
|
return (i);
|
|
|
|
|
}
|